From a640397052360aa68e921bc9fc611fc64183b897 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 12 Oct 2018 12:29:37 +0100 Subject: [PATCH] make project names unique for zip uploads and template imports --- .../Features/Project/ProjectDetailsHandler.coffee | 4 ++++ .../Features/Uploads/ProjectUploadManager.coffee | 13 ++++++++----- .../coffee/Uploads/ProjectUploadManagerTests.coffee | 2 ++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/services/web/app/coffee/Features/Project/ProjectDetailsHandler.coffee b/services/web/app/coffee/Features/Project/ProjectDetailsHandler.coffee index 20e8d2e6da..4f107aa5a4 100644 --- a/services/web/app/coffee/Features/Project/ProjectDetailsHandler.coffee +++ b/services/web/app/coffee/Features/Project/ProjectDetailsHandler.coffee @@ -67,6 +67,10 @@ module.exports = ProjectDetailsHandler = else return callback() + generateUniqueName: (user_id, name, callback = (error, newName) -> ) -> + timestamp = new Date().toISOString().replace(/T(\d+):(\d+):(\d+)\..*/,' $1$2$3') # strip out unwanted characters + ProjectDetailsHandler.ensureProjectNameIsUnique user_id, name, [" #{timestamp}"], callback + _addSuffixToProjectName: (name, suffix = '') -> # append the suffix and truncate the project title if needed truncatedLength = ProjectDetailsHandler.MAX_PROJECT_NAME_LENGTH - suffix.length diff --git a/services/web/app/coffee/Features/Uploads/ProjectUploadManager.coffee b/services/web/app/coffee/Features/Uploads/ProjectUploadManager.coffee index 127c6bc94f..4a1dee2d9b 100644 --- a/services/web/app/coffee/Features/Uploads/ProjectUploadManager.coffee +++ b/services/web/app/coffee/Features/Uploads/ProjectUploadManager.coffee @@ -4,16 +4,19 @@ ArchiveManager = require "./ArchiveManager" FileSystemImportManager = require "./FileSystemImportManager" ProjectCreationHandler = require "../Project/ProjectCreationHandler" ProjectRootDocManager = require "../Project/ProjectRootDocManager" +ProjectDetailsHandler = require "../Project/ProjectDetailsHandler" module.exports = ProjectUploadHandler = - createProjectFromZipArchive: (owner_id, name, zipPath, callback = (error, project) ->) -> - ProjectCreationHandler.createBlankProject owner_id, name, (error, project) => + createProjectFromZipArchive: (owner_id, proposedName, zipPath, callback = (error, project) ->) -> + ProjectDetailsHandler.generateUniqueName owner_id, proposedName, (error, name) => return callback(error) if error? - @insertZipArchiveIntoFolder owner_id, project._id, project.rootFolder[0]._id, zipPath, (error) -> + ProjectCreationHandler.createBlankProject owner_id, name, (error, project) => return callback(error) if error? - ProjectRootDocManager.setRootDocAutomatically project._id, (error) -> + @insertZipArchiveIntoFolder owner_id, project._id, project.rootFolder[0]._id, zipPath, (error) -> return callback(error) if error? - callback(error, project) + ProjectRootDocManager.setRootDocAutomatically project._id, (error) -> + return callback(error) if error? + callback(error, project) insertZipArchiveIntoFolder: (owner_id, project_id, folder_id, path, callback = (error) ->) -> destination = @_getDestinationDirectory path diff --git a/services/web/test/unit/coffee/Uploads/ProjectUploadManagerTests.coffee b/services/web/test/unit/coffee/Uploads/ProjectUploadManagerTests.coffee index d4571f0474..8edf700252 100644 --- a/services/web/test/unit/coffee/Uploads/ProjectUploadManagerTests.coffee +++ b/services/web/test/unit/coffee/Uploads/ProjectUploadManagerTests.coffee @@ -15,6 +15,7 @@ describe "ProjectUploadManager", -> "./ArchiveManager" : @ArchiveManager = {} "../Project/ProjectCreationHandler" : @ProjectCreationHandler = {} "../Project/ProjectRootDocManager" : @ProjectRootDocManager = {} + "../Project/ProjectDetailsHandler" : @ProjectDetailsHandler = {} "rimraf" : @rimraf = sinon.stub().callsArg(1) describe "createProjectFromZipArchive", -> @@ -26,6 +27,7 @@ describe "ProjectUploadManager", -> @project = _id: @project_id rootFolder: [ _id: @root_folder_id ] + @ProjectDetailsHandler.generateUniqueName = sinon.stub().callsArgWith(2, null, @name) @ProjectCreationHandler.createBlankProject = sinon.stub().callsArgWith(2, null, @project) @ProjectUploadManager.insertZipArchiveIntoFolder = sinon.stub().callsArg(4) @ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArg(1)