diff --git a/services/web/app/coffee/Features/Uploads/FileSystemImportManager.coffee b/services/web/app/coffee/Features/Uploads/FileSystemImportManager.coffee index 4f0c4dcc0b..c7e4fbe03a 100644 --- a/services/web/app/coffee/Features/Uploads/FileSystemImportManager.coffee +++ b/services/web/app/coffee/Features/Uploads/FileSystemImportManager.coffee @@ -6,14 +6,28 @@ EditorController = require "../Editor/EditorController" ProjectLocator = require "../Project/ProjectLocator" module.exports = FileSystemImportManager = - addDoc: (project_id, folder_id, name, path, replace, callback = (error, doc)-> )-> + addDoc: (user_id, project_id, folder_id, name, path, replace, callback = (error, doc)-> )-> fs.readFile path, "utf8", (error, content = "") -> return callback(error) if error? content = content.replace(/\r/g, "") lines = content.split("\n") - EditorController.addDocWithoutLock project_id, folder_id, name, lines, "upload", callback + if replace + ProjectLocator.findElement project_id: project_id, element_id: folder_id, type: "folder", (error, folder) -> + return callback(error) if error? + return callback(new Error("Couldn't find folder")) if !folder? + existingDoc = null + for doc in folder.docs + if doc.name == name + existingDoc = doc + break + if existingDoc? + EditorController.setDoc project_id, existingDoc._id, user_id, lines, "upload", callback + else + EditorController.addDocWithoutLock project_id, folder_id, name, lines, "upload", callback + else + EditorController.addDocWithoutLock project_id, folder_id, name, lines, "upload", callback - addFile: (project_id, folder_id, name, path, replace, callback = (error, file)-> )-> + addFile: (user_id, project_id, folder_id, name, path, replace, callback = (error, file)-> )-> if replace ProjectLocator.findElement project_id: project_id, element_id: folder_id, type: "folder", (error, folder) -> return callback(error) if error? @@ -30,14 +44,14 @@ module.exports = FileSystemImportManager = else EditorController.addFileWithoutLock project_id, folder_id, name, path, "upload", callback - addFolder: (project_id, folder_id, name, path, replace, callback = (error)-> ) -> + addFolder: (user_id, project_id, folder_id, name, path, replace, callback = (error)-> ) -> EditorController.addFolderWithoutLock project_id, folder_id, name, "upload", (error, new_folder) => return callback(error) if error? - @addFolderContents project_id, new_folder._id, path, replace, (error) -> + @addFolderContents user_id, project_id, new_folder._id, path, replace, (error) -> return callback(error) if error? callback null, new_folder - addFolderContents: (project_id, parent_folder_id, folderPath, replace, callback = (error)-> ) -> + addFolderContents: (user_id, project_id, parent_folder_id, folderPath, replace, callback = (error)-> ) -> fs.readdir folderPath, (error, entries = []) => return callback(error) if error? jobs = _.map entries, (entry) => @@ -45,21 +59,21 @@ module.exports = FileSystemImportManager = FileTypeManager.shouldIgnore entry, (error, ignore) => return callback(error) if error? if !ignore - @addEntity project_id, parent_folder_id, entry, "#{folderPath}/#{entry}", replace, callback + @addEntity user_id, project_id, parent_folder_id, entry, "#{folderPath}/#{entry}", replace, callback else callback() async.parallelLimit jobs, 5, callback - addEntity: (project_id, folder_id, name, path, replace, callback = (error, entity)-> ) -> + addEntity: (user_id, project_id, folder_id, name, path, replace, callback = (error, entity)-> ) -> FileTypeManager.isDirectory path, (error, isDirectory) => return callback(error) if error? if isDirectory - @addFolder project_id, folder_id, name, path, replace, callback + @addFolder user_id, project_id, folder_id, name, path, replace, callback else FileTypeManager.isBinary name, path, (error, isBinary) => return callback(error) if error? if isBinary - @addFile project_id, folder_id, name, path, replace, callback + @addFile user_id, project_id, folder_id, name, path, replace, callback else - @addDoc project_id, folder_id, name, path, replace, callback + @addDoc user_id, project_id, folder_id, name, path, replace, callback diff --git a/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee b/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee index 83a8db5c2d..d2b720e8dc 100644 --- a/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee +++ b/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee @@ -34,7 +34,8 @@ module.exports = ProjectUploadController = if !name? or name.length == 0 or name.length > 150 logger.err project_id:project_id, name:name, "bad name when trying to upload file" return res.send success: false - FileSystemImportManager.addEntity project_id, folder_id, name, path, true, (error, entity) -> + user_id = req.session.user._id + FileSystemImportManager.addEntity user_id, project_id, folder_id, name, path, true, (error, entity) -> fs.unlink path, -> timer.done() if error? diff --git a/services/web/app/coffee/Features/Uploads/ProjectUploadManager.coffee b/services/web/app/coffee/Features/Uploads/ProjectUploadManager.coffee index b5d108d21d..e4535231dd 100644 --- a/services/web/app/coffee/Features/Uploads/ProjectUploadManager.coffee +++ b/services/web/app/coffee/Features/Uploads/ProjectUploadManager.coffee @@ -9,17 +9,17 @@ module.exports = ProjectUploadHandler = createProjectFromZipArchive: (owner_id, name, zipPath, callback = (error, project) ->) -> ProjectCreationHandler.createBlankProject owner_id, name, (error, project) => return callback(error) if error? - @insertZipArchiveIntoFolder project._id, project.rootFolder[0]._id, zipPath, (error) -> + @insertZipArchiveIntoFolder owner_id, project._id, project.rootFolder[0]._id, zipPath, (error) -> return callback(error) if error? ProjectRootDocManager.setRootDocAutomatically project._id, (error) -> return callback(error) if error? callback(error, project) - insertZipArchiveIntoFolder: (project_id, folder_id, path, callback = (error) ->) -> + insertZipArchiveIntoFolder: (owner_id, project_id, folder_id, path, callback = (error) ->) -> destination = @_getDestinationDirectory path ArchiveManager.extractZipArchive path, destination, (error) -> return callback(error) if error? - FileSystemImportManager.addFolderContents project_id, folder_id, destination, false, (error) -> + FileSystemImportManager.addFolderContents owner_id, project_id, folder_id, destination, false, (error) -> return callback(error) if error? rimraf(destination, callback) diff --git a/services/web/test/UnitTests/coffee/Uploads/FileSystemImportManagerTests.coffee b/services/web/test/UnitTests/coffee/Uploads/FileSystemImportManagerTests.coffee index fa385f7261..441dd8163d 100644 --- a/services/web/test/UnitTests/coffee/Uploads/FileSystemImportManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Uploads/FileSystemImportManagerTests.coffee @@ -11,43 +11,94 @@ describe "FileSystemImportManager", -> @name = "test-file.tex" @path_on_disk = "/path/to/file/#{@name}" @replace = "replace-boolean-flag-mock" + @user_id = "mock-user-123" + @callback = sinon.stub() @FileSystemImportManager = SandboxedModule.require modulePath, requires: "fs" : @fs = {} "../Editor/EditorController": @EditorController = {} "./FileTypeManager": @FileTypeManager = {} "../Project/ProjectLocator": @ProjectLocator = {} - + describe "addDoc", -> beforeEach -> @docContent = "one\ntwo\nthree" @docLines = @docContent.split("\n") @fs.readFile = sinon.stub().callsArgWith(2, null, @docContent) - @EditorController.addDocWithoutLock = sinon.stub().callsArg(5) - @FileSystemImportManager.addDoc @project_id, @folder_id, @name, @path_on_disk, false, @callback - it "should read the file from disk", -> - @fs.readFile.calledWith(@path_on_disk, "utf8").should.equal true + describe "with replace set to false", -> + beforeEach -> + @EditorController.addDocWithoutLock = sinon.stub().callsArg(5) + @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback - it "should insert the doc", -> - @EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload") - .should.equal true + it "should read the file from disk", -> + @fs.readFile.calledWith(@path_on_disk, "utf8").should.equal true - describe "addDoc with windows line ending", -> - beforeEach -> - @docContent = "one\r\ntwo\r\nthree" - @docLines = ["one", "two", "three"] - @fs.readFile = sinon.stub().callsArgWith(2, null, @docContent) - @EditorController.addDocWithoutLock = sinon.stub().callsArg(5) - @FileSystemImportManager.addDoc @project_id, @folder_id, @name, @path_on_disk, false, @callback + it "should insert the doc", -> + @EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload") + .should.equal true - it "should strip the \\r characters before adding", -> - @EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload") - .should.equal true + describe "with windows line ending", -> + beforeEach -> + @docContent = "one\r\ntwo\r\nthree" + @docLines = ["one", "two", "three"] + @fs.readFile = sinon.stub().callsArgWith(2, null, @docContent) + @EditorController.addDocWithoutLock = sinon.stub().callsArg(5) + @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback + + it "should strip the \\r characters before adding", -> + @EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload") + .should.equal true + + describe "with replace set to true", -> + describe "when the doc doesn't exist", -> + beforeEach -> + @folder = { + docs: [{ + _id: "doc-id-2" + name: "not-the-right-file.tex" + }] + } + @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder) + @EditorController.addDocWithoutLock = sinon.stub().callsArg(5) + @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback + + it "should look up the folder", -> + @ProjectLocator.findElement + .calledWith(project_id: @project_id, element_id: @folder_id, type: "folder") + .should.equal true + + it "should insert the doc", -> + @EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload") + .should.equal true + + describe "when the doc does exist", -> + beforeEach -> + @folder = { + docs: [{ + _id: @doc_id = "doc-id-1" + name: @name + }, { + _id: "doc-id-2" + name: "not-the-right-file.tex" + }] + } + @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder) + @EditorController.setDoc = sinon.stub().callsArg(5) + @FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback + + it "should look up the folder", -> + @ProjectLocator.findElement + .calledWith(project_id: @project_id, element_id: @folder_id, type: "folder") + .should.equal true + + it "should set the doc with the new doc lines", -> + @EditorController.setDoc.calledWith(@project_id, @doc_id, @user_id, @docLines, "upload") + .should.equal true describe "addFile with replace set to false", -> beforeEach -> @EditorController.addFileWithoutLock = sinon.stub().callsArg(5) - @FileSystemImportManager.addFile @project_id, @folder_id, @name, @path_on_disk, false, @callback + @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback it "should add the file", -> @EditorController.addFileWithoutLock.calledWith(@project_id, @folder_id, @name, @path_on_disk, "upload") @@ -64,7 +115,7 @@ describe "FileSystemImportManager", -> } @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder) @EditorController.addFileWithoutLock = sinon.stub().callsArg(5) - @FileSystemImportManager.addFile @project_id, @folder_id, @name, @path_on_disk, true, @callback + @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback it "should look up the folder", -> @ProjectLocator.findElement @@ -88,7 +139,7 @@ describe "FileSystemImportManager", -> } @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder) @EditorController.replaceFile = sinon.stub().callsArg(4) - @FileSystemImportManager.addFile @project_id, @folder_id, @name, @path_on_disk, true, @callback + @FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback it "should look up the folder", -> @ProjectLocator.findElement @@ -103,15 +154,15 @@ describe "FileSystemImportManager", -> beforeEach -> @new_folder_id = "new-folder-id" @EditorController.addFolderWithoutLock = sinon.stub().callsArgWith(4, null, _id: @new_folder_id) - @FileSystemImportManager.addFolderContents = sinon.stub().callsArg(4) - @FileSystemImportManager.addFolder @project_id, @folder_id, @name, @path_on_disk, @replace, @callback + @FileSystemImportManager.addFolderContents = sinon.stub().callsArg(5) + @FileSystemImportManager.addFolder @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback it "should add a folder to the project", -> @EditorController.addFolderWithoutLock.calledWith(@project_id, @folder_id, @name, "upload") .should.equal true it "should add the folders contents", -> - @FileSystemImportManager.addFolderContents.calledWith(@project_id, @new_folder_id, @path_on_disk, @replace) + @FileSystemImportManager.addFolderContents.calledWith(@user_id, @project_id, @new_folder_id, @path_on_disk, @replace) .should.equal true describe "addFolderContents", -> @@ -119,19 +170,19 @@ describe "FileSystemImportManager", -> @folderEntries = ["path1", "path2", "path3"] @ignoredEntries = [".DS_Store"] @fs.readdir = sinon.stub().callsArgWith(1, null, @folderEntries.concat @ignoredEntries) - @FileSystemImportManager.addEntity = sinon.stub().callsArg(5) + @FileSystemImportManager.addEntity = sinon.stub().callsArg(6) @FileTypeManager.shouldIgnore = (path, callback) => callback null, @ignoredEntries.indexOf(require("path").basename(path)) != -1 - @FileSystemImportManager.addFolderContents @project_id, @folder_id, @path_on_disk, @replace, @callback + @FileSystemImportManager.addFolderContents @user_id, @project_id, @folder_id, @path_on_disk, @replace, @callback it "should call addEntity for each file in the folder which is not ignored", -> for name in @folderEntries - @FileSystemImportManager.addEntity.calledWith(@project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace) + @FileSystemImportManager.addEntity.calledWith(@user_id, @project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace) .should.equal true it "should not call addEntity for the ignored files", -> for name in @ignoredEntries - @FileSystemImportManager.addEntity.calledWith(@project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace) + @FileSystemImportManager.addEntity.calledWith(@user_id, @project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace) .should.equal false it "should look in the correct directory", -> @@ -141,33 +192,33 @@ describe "FileSystemImportManager", -> describe "with directory", -> beforeEach -> @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, true) - @FileSystemImportManager.addFolder = sinon.stub().callsArg(5) - @FileSystemImportManager.addEntity @project_id, @folder_id, @name, @path_on_disk, @replace, @callback + @FileSystemImportManager.addFolder = sinon.stub().callsArg(6) + @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback it "should call addFolder", -> - @FileSystemImportManager.addFolder.calledWith(@project_id, @folder_id, @name, @path_on_disk, @replace, @callback) + @FileSystemImportManager.addFolder.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace) .should.equal true describe "with binary file", -> beforeEach -> @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false) @FileTypeManager.isBinary = sinon.stub().callsArgWith(2, null, true) - @FileSystemImportManager.addFile = sinon.stub().callsArg(5) - @FileSystemImportManager.addEntity @project_id, @folder_id, @name, @path_on_disk, @replace, @callback + @FileSystemImportManager.addFile = sinon.stub().callsArg(6) + @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback it "should call addFile", -> - @FileSystemImportManager.addFile.calledWith(@project_id, @folder_id, @name, @path_on_disk, @replace, @callback) + @FileSystemImportManager.addFile.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback) .should.equal true describe "with text file", -> beforeEach -> @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false) @FileTypeManager.isBinary = sinon.stub().callsArgWith(2, null, false) - @FileSystemImportManager.addDoc = sinon.stub().callsArg(5) - @FileSystemImportManager.addEntity @project_id, @folder_id, @name, @path_on_disk, @replace, @callback + @FileSystemImportManager.addDoc = sinon.stub().callsArg(6) + @FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback it "should call addFile", -> - @FileSystemImportManager.addDoc.calledWith(@project_id, @folder_id, @name, @path_on_disk, @replace, @callback) + @FileSystemImportManager.addDoc.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback) .should.equal true diff --git a/services/web/test/UnitTests/coffee/Uploads/ProjectUploadControllerTests.coffee b/services/web/test/UnitTests/coffee/Uploads/ProjectUploadControllerTests.coffee index dd4240d1b8..176ae96d24 100644 --- a/services/web/test/UnitTests/coffee/Uploads/ProjectUploadControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Uploads/ProjectUploadControllerTests.coffee @@ -103,6 +103,9 @@ describe "ProjectUploadController", -> qqfile: path: @path originalname: @name + @req.session = + user: + _id: @user_id @req.params = Project_id: @project_id @req.query = @@ -115,27 +118,12 @@ describe "ProjectUploadController", -> beforeEach -> @entity = _id : "1234" - @FileSystemImportManager.addEntity = sinon.stub().callsArgWith(5, null, @entity) + @FileSystemImportManager.addEntity = sinon.stub().callsArgWith(6, null, @entity) @ProjectUploadController.uploadFile @req, @res - it "should insert the file into the correct project", -> + it "should insert the file", -> @FileSystemImportManager.addEntity - .calledWith(@project_id) - .should.equal true - - it "should insert the file into the provided folder", -> - @FileSystemImportManager.addEntity - .calledWith(sinon.match.any, @folder_id) - .should.equal true - - it "should insert the file with the correct name", -> - @FileSystemImportManager.addEntity - .calledWith(sinon.match.any, sinon.match.any, @name) - .should.equal true - - it "should insert the file from the uploaded path", -> - @FileSystemImportManager.addEntity - .calledWith(sinon.match.any, sinon.match.any, sinon.match.any, @path) + .calledWith(@user_id, @project_id, @folder_id, @name, @path) .should.equal true it "should return a successful response to the FileUploader client", -> @@ -143,7 +131,6 @@ describe "ProjectUploadController", -> success: true entity_id: @entity._id - it "should output a log line", -> @logger.log .calledWith(sinon.match.any, "uploaded file") @@ -158,7 +145,7 @@ describe "ProjectUploadController", -> describe "when FileSystemImportManager.addEntity returns an error", -> beforeEach -> @FileSystemImportManager.addEntity = sinon.stub() - .callsArgWith(5, new Error("Sorry something went wrong")) + .callsArgWith(6, new Error("Sorry something went wrong")) @ProjectUploadController.uploadFile @req, @res it "should return an unsuccessful response to the FileUploader client", -> diff --git a/services/web/test/UnitTests/coffee/Uploads/ProjectUploadManagerTests.coffee b/services/web/test/UnitTests/coffee/Uploads/ProjectUploadManagerTests.coffee index 53b7cb2064..6cd0a875b3 100644 --- a/services/web/test/UnitTests/coffee/Uploads/ProjectUploadManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Uploads/ProjectUploadManagerTests.coffee @@ -8,6 +8,7 @@ describe "ProjectUploadManager", -> beforeEach -> @project_id = "project-id-123" @folder_id = "folder-id-123" + @owner_id = "onwer-id-123" @callback = sinon.stub() @ProjectUploadManager = SandboxedModule.require modulePath, requires: "./FileSystemImportManager" : @FileSystemImportManager = {} @@ -26,7 +27,7 @@ describe "ProjectUploadManager", -> _id: @project_id rootFolder: [ _id: @root_folder_id ] @ProjectCreationHandler.createBlankProject = sinon.stub().callsArgWith(2, null, @project) - @ProjectUploadManager.insertZipArchiveIntoFolder = sinon.stub().callsArg(3) + @ProjectUploadManager.insertZipArchiveIntoFolder = sinon.stub().callsArg(4) @ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArg(1) @ProjectUploadManager.createProjectFromZipArchive @owner_id, @name, @source, @callback @@ -45,7 +46,7 @@ describe "ProjectUploadManager", -> it "should insert the zip file contents into the root folder", -> @ProjectUploadManager .insertZipArchiveIntoFolder - .calledWith(@project_id, @root_folder_id, @source) + .calledWith(@owner_id, @project_id, @root_folder_id, @source) .should.equal true it "should automatically set the root doc", -> @@ -63,9 +64,9 @@ describe "ProjectUploadManager", -> @destination = "/path/to/zile/file-extracted" @ProjectUploadManager._getDestinationDirectory = sinon.stub().returns @destination @ArchiveManager.extractZipArchive = sinon.stub().callsArg(2) - @FileSystemImportManager.addFolderContents = sinon.stub().callsArg(4) + @FileSystemImportManager.addFolderContents = sinon.stub().callsArg(5) - @ProjectUploadManager.insertZipArchiveIntoFolder @project_id, @folder_id, @source, @callback + @ProjectUploadManager.insertZipArchiveIntoFolder @owner_id, @project_id, @folder_id, @source, @callback it "should set up the directory to extract the archive to", -> @ProjectUploadManager._getDestinationDirectory.calledWith(@source).should.equal true @@ -74,7 +75,7 @@ describe "ProjectUploadManager", -> @ArchiveManager.extractZipArchive.calledWith(@source, @destination).should.equal true it "should insert the extracted archive into the folder", -> - @FileSystemImportManager.addFolderContents.calledWith(@project_id, @folder_id, @destination, false) + @FileSystemImportManager.addFolderContents.calledWith(@owner_id, @project_id, @folder_id, @destination, false) .should.equal true it "should return the callback", ->