fix file name collisions to only be affected in folders
This commit is contained in:
parent
e94ba201ce
commit
6c4ba328aa
2 changed files with 9 additions and 17 deletions
|
@ -13,15 +13,17 @@ function appendToFilename(filename, string) {
|
|||
return filename.substring(0, dotIndex) + string + filename.substring(dotIndex);
|
||||
}
|
||||
|
||||
function createUniqueName(name, files) {
|
||||
function createUniqueName(name, parentId, files) {
|
||||
const siblingFiles = files.find(file => file.id === parentId)
|
||||
.children.map(childFileId => files.find(file => file.id === childFileId));
|
||||
let testName = name;
|
||||
let index = 1;
|
||||
let existingName = files.find((file) => name === file.name);
|
||||
let existingName = siblingFiles.find((file) => name === file.name);
|
||||
|
||||
while (existingName) {
|
||||
testName = appendToFilename(name, `-${index}`);
|
||||
index++;
|
||||
existingName = files.find((file) => testName === file.name); // eslint-disable-line
|
||||
existingName = siblingFiles.find((file) => testName === file.name); // eslint-disable-line
|
||||
}
|
||||
return testName;
|
||||
}
|
||||
|
@ -79,7 +81,7 @@ export function createFile(formProps) {
|
|||
}
|
||||
if (state.project.id) {
|
||||
const postParams = {
|
||||
name: createUniqueName(formProps.name, state.files),
|
||||
name: createUniqueName(formProps.name, parentId, state.files),
|
||||
url: formProps.url,
|
||||
content: formProps.content || '',
|
||||
parentId,
|
||||
|
@ -110,7 +112,7 @@ export function createFile(formProps) {
|
|||
const id = objectID().toHexString();
|
||||
dispatch({
|
||||
type: ActionTypes.CREATE_FILE,
|
||||
name: createUniqueName(formProps.name, state.files),
|
||||
name: createUniqueName(formProps.name, parentId, state.files),
|
||||
id,
|
||||
_id: id,
|
||||
url: formProps.url,
|
||||
|
@ -138,7 +140,7 @@ export function createFolder(formProps) {
|
|||
}
|
||||
if (state.project.id) {
|
||||
const postParams = {
|
||||
name: createUniqueName(formProps.name, state.files),
|
||||
name: createUniqueName(formProps.name, parentId, state.files),
|
||||
content: '',
|
||||
children: [],
|
||||
parentId,
|
||||
|
@ -163,7 +165,7 @@ export function createFolder(formProps) {
|
|||
const id = objectID().toHexString();
|
||||
dispatch({
|
||||
type: ActionTypes.CREATE_FILE,
|
||||
name: createUniqueName(formProps.name, state.files),
|
||||
name: createUniqueName(formProps.name, parentId, state.files),
|
||||
id,
|
||||
_id: id,
|
||||
content: '',
|
||||
|
|
|
@ -5,7 +5,6 @@ import JSZip from 'jszip';
|
|||
import JSZipUtils from 'jszip-utils';
|
||||
import { saveAs } from 'file-saver';
|
||||
import { getBlobUrl } from './files';
|
||||
import async from 'async';
|
||||
|
||||
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
||||
|
||||
|
@ -67,15 +66,6 @@ export function saveProject() {
|
|||
error: response.data
|
||||
}));
|
||||
} else {
|
||||
// this might be unnecessary, but to prevent collisions in mongodb
|
||||
// formParams.files.map(file => {
|
||||
// if (file.name !== 'root') {
|
||||
// const newFile = Object.assign({}, file);
|
||||
// delete newFile.id;
|
||||
// return newFile;
|
||||
// }
|
||||
// return file;
|
||||
// });
|
||||
axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
|
||||
.then(response => {
|
||||
browserHistory.push(`/projects/${response.data.id}`);
|
||||
|
|
Loading…
Reference in a new issue