fix file name collisions to only be affected in folders

This commit is contained in:
catarak 2016-09-03 14:52:51 -04:00
parent e94ba201ce
commit 6c4ba328aa
2 changed files with 9 additions and 17 deletions

View file

@ -13,15 +13,17 @@ function appendToFilename(filename, string) {
return filename.substring(0, dotIndex) + string + filename.substring(dotIndex); 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 testName = name;
let index = 1; let index = 1;
let existingName = files.find((file) => name === file.name); let existingName = siblingFiles.find((file) => name === file.name);
while (existingName) { while (existingName) {
testName = appendToFilename(name, `-${index}`); testName = appendToFilename(name, `-${index}`);
index++; index++;
existingName = files.find((file) => testName === file.name); // eslint-disable-line existingName = siblingFiles.find((file) => testName === file.name); // eslint-disable-line
} }
return testName; return testName;
} }
@ -79,7 +81,7 @@ export function createFile(formProps) {
} }
if (state.project.id) { if (state.project.id) {
const postParams = { const postParams = {
name: createUniqueName(formProps.name, state.files), name: createUniqueName(formProps.name, parentId, state.files),
url: formProps.url, url: formProps.url,
content: formProps.content || '', content: formProps.content || '',
parentId, parentId,
@ -110,7 +112,7 @@ export function createFile(formProps) {
const id = objectID().toHexString(); const id = objectID().toHexString();
dispatch({ dispatch({
type: ActionTypes.CREATE_FILE, type: ActionTypes.CREATE_FILE,
name: createUniqueName(formProps.name, state.files), name: createUniqueName(formProps.name, parentId, state.files),
id, id,
_id: id, _id: id,
url: formProps.url, url: formProps.url,
@ -138,7 +140,7 @@ export function createFolder(formProps) {
} }
if (state.project.id) { if (state.project.id) {
const postParams = { const postParams = {
name: createUniqueName(formProps.name, state.files), name: createUniqueName(formProps.name, parentId, state.files),
content: '', content: '',
children: [], children: [],
parentId, parentId,
@ -163,7 +165,7 @@ export function createFolder(formProps) {
const id = objectID().toHexString(); const id = objectID().toHexString();
dispatch({ dispatch({
type: ActionTypes.CREATE_FILE, type: ActionTypes.CREATE_FILE,
name: createUniqueName(formProps.name, state.files), name: createUniqueName(formProps.name, parentId, state.files),
id, id,
_id: id, _id: id,
content: '', content: '',

View file

@ -5,7 +5,6 @@ import JSZip from 'jszip';
import JSZipUtils from 'jszip-utils'; import JSZipUtils from 'jszip-utils';
import { saveAs } from 'file-saver'; import { saveAs } from 'file-saver';
import { getBlobUrl } from './files'; import { getBlobUrl } from './files';
import async from 'async';
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';
@ -67,15 +66,6 @@ export function saveProject() {
error: response.data error: response.data
})); }));
} else { } 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 }) axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
.then(response => { .then(response => {
browserHistory.push(`/projects/${response.data.id}`); browserHistory.push(`/projects/${response.data.id}`);