Remove fileInfo.name when unchanged

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2020-04-03 14:38:44 +02:00
parent 500c12f7c7
commit c69b6e8d4e
7 changed files with 60 additions and 30 deletions

View File

@ -1,7 +1,7 @@
import React, { useReducer, useEffect, useState } from 'react'; import React, { useReducer, useEffect, useState } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { request, generateSearchFromFilters, useGlobalContext } from 'strapi-helper-plugin'; import { request, generateSearchFromFilters, useGlobalContext } from 'strapi-helper-plugin';
import { get, isEmpty } from 'lodash'; import { clone, get, isEmpty, set } from 'lodash';
import axios from 'axios'; import axios from 'axios';
import pluginId from '../../pluginId'; import pluginId from '../../pluginId';
import { import {
@ -72,9 +72,9 @@ const InputModalStepperProvider = ({
return axios return axios
.get(file.fileURL, { .get(file.fileURL, {
headers: new Headers({ Origin: window.location.origin, mode: 'cors' }),
responseType: 'blob', responseType: 'blob',
cancelToken: source.token, cancelToken: source.token,
timeout: 30000,
}) })
.then(({ data }) => { .then(({ data }) => {
const createdFile = new File([data], file.fileURL, { const createdFile = new File([data], file.fileURL, {
@ -352,11 +352,19 @@ const InputModalStepperProvider = ({
}); });
const requests = filesToUpload.map( const requests = filesToUpload.map(
async ({ file, fileInfo, originalIndex, abortController }) => { async ({ file, fileInfo, originalIndex, originalName, abortController }) => {
const formData = new FormData(); const formData = new FormData();
const headers = {}; const headers = {};
const infos = clone(fileInfo);
if (originalName === infos.name) {
set(infos, 'name', null);
}
console.log(infos);
formData.append('files', file); formData.append('files', file);
formData.append('fileInfo', JSON.stringify(fileInfo)); formData.append('fileInfo', JSON.stringify(infos));
try { try {
const uploadedFile = await request( const uploadedFile = await request(

View File

@ -220,7 +220,7 @@ const reducer = (state, action) =>
...draftState.filesToUpload[index], ...draftState.filesToUpload[index],
isDownloading: false, isDownloading: false,
hasError: true, hasError: true,
errorMessage: draftState.filesToUpload[index].fileURL, errorMessage: draftState.filesToUpload[index].fileOriginalURL,
}; };
break; break;

View File

@ -1,7 +1,7 @@
import React, { useEffect, useState, useReducer, useRef } from 'react'; import React, { useEffect, useState, useReducer, useRef } from 'react';
import axios from 'axios'; import axios from 'axios';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { isEqual, isEmpty, get } from 'lodash'; import { isEqual, isEmpty, get, set } from 'lodash';
import { Modal, ModalFooter, PopUpWarning, useGlobalContext, request } from 'strapi-helper-plugin'; import { Modal, ModalFooter, PopUpWarning, useGlobalContext, request } from 'strapi-helper-plugin';
import { Button } from '@buffetjs/core'; import { Button } from '@buffetjs/core';
import pluginId from '../../pluginId'; import pluginId from '../../pluginId';
@ -85,10 +85,9 @@ const ModalStepper = ({
return axios return axios
.get(file.fileURL, { .get(file.fileURL, {
headers: new Headers({ Origin: window.location.origin, mode: 'cors' }),
responseType: 'blob', responseType: 'blob',
cancelToken: source.token, cancelToken: source.token,
// Should we add a timeout? timeout: 30000,
}) })
.then(({ data }) => { .then(({ data }) => {
const fileName = file.fileInfo.name; const fileName = file.fileInfo.name;
@ -356,9 +355,14 @@ const ModalStepper = ({
}); });
const requests = filesToUpload.map( const requests = filesToUpload.map(
async ({ file, fileInfo, originalIndex, abortController }) => { async ({ file, fileInfo, originalName, originalIndex, abortController }) => {
const formData = new FormData(); const formData = new FormData();
const headers = {}; const headers = {};
if (originalName === fileInfo.name) {
set(fileInfo, 'name', null);
}
formData.append('files', file); formData.append('files', file);
formData.append('fileInfo', JSON.stringify(fileInfo)); formData.append('fileInfo', JSON.stringify(fileInfo));

View File

@ -100,7 +100,7 @@ const reducer = (state, action) => {
return file return file
.update('isDownloading', () => false) .update('isDownloading', () => false)
.update('hasError', () => true) .update('hasError', () => true)
.update('errorMessage', () => file.get('fileURL')); .update('errorMessage', () => file.get('fileOriginalURL'));
} }
return file; return file;

View File

@ -18,26 +18,34 @@ const createNewFilesToDownloadArray = (filesURLArray, alreadyUploadedFiles) => {
return acc; return acc;
} }
const CancelToken = axios.CancelToken; try {
const abortController = new AbortController(); const url = new URL(current);
const source = CancelToken.source(); const name = decodeURIComponent(url.pathname.substring(url.pathname.lastIndexOf('/') + 1));
const CancelToken = axios.CancelToken;
const abortController = new AbortController();
const source = CancelToken.source();
acc.push({ acc.push({
abortController, abortController,
source, source,
file: null, file: null,
fileInfo: { fileInfo: {
alternativeText: '', alternativeText: '',
caption: '', caption: '',
name: current.substring(current.lastIndexOf('/') + 1), name,
}, },
fileURL: current, fileURL: url,
hasError: false, fileOriginalURL: current,
errorMessage: null, originalName: name,
isUploading: false, hasError: false,
isDownloading: true, errorMessage: null,
tempId: max + index, isUploading: false,
}); isDownloading: true,
tempId: max + index,
});
} catch (err) {
// invalid url
}
return acc; return acc;
}, []); }, []);

View File

@ -13,6 +13,7 @@ const createNewFilesToUploadArray = filesObject => {
}, },
hasError: false, hasError: false,
errorMessage: null, errorMessage: null,
originalName: currentFile.name,
isUploading: false, isUploading: false,
tempId: null, tempId: null,
}); });

View File

@ -13,7 +13,16 @@ const urlSchema = yup.object().shape({
message: '${path}', message: '${path}',
test(values) { test(values) {
const filtered = values.filter(val => { const filtered = values.filter(val => {
return !val.startsWith('http'); // return !val.startsWith('http');
try {
// eslint-disable-next-line no-new
new URL(val);
return false;
} catch (err) {
// invalid url
return true;
}
}); });
const filteredLength = filtered.length; const filteredLength = filtered.length;