create unique name if file already exists
This commit is contained in:
parent
a5db4ae9f6
commit
cb1e682994
1 changed files with 21 additions and 2 deletions
|
@ -4,6 +4,25 @@ import blobUtil from 'blob-util';
|
||||||
|
|
||||||
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
||||||
|
|
||||||
|
function appendToFilename(filename, string) {
|
||||||
|
const dotIndex = filename.lastIndexOf('.');
|
||||||
|
if (dotIndex === -1) return filename + string;
|
||||||
|
return filename.substring(0, dotIndex) + string + filename.substring(dotIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
function createUniqueName(name, files) {
|
||||||
|
let testName = name;
|
||||||
|
let index = 1;
|
||||||
|
let existingName = files.find((file) => name === file.name);
|
||||||
|
|
||||||
|
while (existingName) {
|
||||||
|
testName = appendToFilename(name, `-${index}`);
|
||||||
|
index++;
|
||||||
|
existingName = files.find((file) => testName === file.name); // eslint-disable-line
|
||||||
|
}
|
||||||
|
return testName;
|
||||||
|
}
|
||||||
|
|
||||||
export function updateFileContent(name, content) {
|
export function updateFileContent(name, content) {
|
||||||
return {
|
return {
|
||||||
type: ActionTypes.UPDATE_FILE_CONTENT,
|
type: ActionTypes.UPDATE_FILE_CONTENT,
|
||||||
|
@ -31,7 +50,7 @@ export function createFile(formProps) {
|
||||||
const state = getState();
|
const state = getState();
|
||||||
if (state.project.id) {
|
if (state.project.id) {
|
||||||
const postParams = {
|
const postParams = {
|
||||||
name: formProps.name,
|
name: createUniqueName(formProps.name, state.files),
|
||||||
url: formProps.url
|
url: formProps.url
|
||||||
};
|
};
|
||||||
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
|
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
|
||||||
|
@ -63,7 +82,7 @@ export function createFile(formProps) {
|
||||||
}
|
}
|
||||||
dispatch({
|
dispatch({
|
||||||
type: ActionTypes.CREATE_FILE,
|
type: ActionTypes.CREATE_FILE,
|
||||||
name: formProps.name,
|
name: createUniqueName(formProps.name, state.files),
|
||||||
id: `${maxFileId + 1}`,
|
id: `${maxFileId + 1}`,
|
||||||
url: formProps.url
|
url: formProps.url
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue