From cb1e682994f85e9e5d42127535b334283f7fd68a Mon Sep 17 00:00:00 2001 From: catarak Date: Wed, 20 Jul 2016 22:52:09 -0400 Subject: [PATCH] create unique name if file already exists --- client/modules/IDE/actions/files.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index a918e7a6..afdb1b36 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -4,6 +4,25 @@ import blobUtil from 'blob-util'; 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) { return { type: ActionTypes.UPDATE_FILE_CONTENT, @@ -31,7 +50,7 @@ export function createFile(formProps) { const state = getState(); if (state.project.id) { const postParams = { - name: formProps.name, + name: createUniqueName(formProps.name, state.files), url: formProps.url }; axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true }) @@ -63,7 +82,7 @@ export function createFile(formProps) { } dispatch({ type: ActionTypes.CREATE_FILE, - name: formProps.name, + name: createUniqueName(formProps.name, state.files), id: `${maxFileId + 1}`, url: formProps.url });