From fe62db05a062e45ca5b92be505cbb4de9d24efe2 Mon Sep 17 00:00:00 2001 From: Hayden Faulds Date: Tue, 2 Jan 2018 12:43:41 +0000 Subject: [PATCH] lock project when uploading a file --- .../Uploads/ProjectUploadController.coffee | 35 +++++++++++-------- .../ProjectUploadControllerTests.coffee | 10 ++++++ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee b/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee index ff8c5aa827..523a4ca8a5 100644 --- a/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee +++ b/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee @@ -5,6 +5,7 @@ Path = require "path" FileSystemImportManager = require "./FileSystemImportManager" ProjectUploadManager = require "./ProjectUploadManager" AuthenticationController = require('../Authentication/AuthenticationController') +LockManager = require("../../infrastructure/LockManager") module.exports = ProjectUploadController = uploadProject: (req, res, next) -> @@ -37,18 +38,24 @@ module.exports = ProjectUploadController = return res.send success: false logger.log folder_id:folder_id, project_id:project_id, "getting upload file request" user_id = AuthenticationController.getLoggedInUserId(req) - FileSystemImportManager.addEntity user_id, project_id, folder_id, name, path, true, (error, entity) -> - fs.unlink path, -> - timer.done() - if error? - logger.error - err: error, project_id: project_id, file_path: path, - file_name: name, folder_id: folder_id, - "error uploading file" - res.send success: false - else - logger.log - project_id: project_id, file_path: path, file_name: name, folder_id: folder_id - "uploaded file" - res.send success: true, entity_id: entity?._id, entity_type: entity?.type + + LockManager.getLock project_id, (err)-> + if err? + logger.err err:err, project_id:project_id, source:source, "could not get lock to uploadFile" + return callback(err) + FileSystemImportManager.addEntity user_id, project_id, folder_id, name, path, true, (error, entity) -> + LockManager.releaseLock project_id, -> + fs.unlink path, -> + timer.done() + if error? + logger.error + err: error, project_id: project_id, file_path: path, + file_name: name, folder_id: folder_id, + "error uploading file" + res.send success: false + else + logger.log + project_id: project_id, file_path: path, file_name: name, folder_id: folder_id + "uploaded file" + res.send success: true, entity_id: entity?._id, entity_type: entity?.type diff --git a/services/web/test/unit/coffee/Uploads/ProjectUploadControllerTests.coffee b/services/web/test/unit/coffee/Uploads/ProjectUploadControllerTests.coffee index 3e60fc0046..24ff97b30f 100644 --- a/services/web/test/unit/coffee/Uploads/ProjectUploadControllerTests.coffee +++ b/services/web/test/unit/coffee/Uploads/ProjectUploadControllerTests.coffee @@ -17,12 +17,16 @@ describe "ProjectUploadController", -> done: sinon.stub() @AuthenticationController = getLoggedInUserId: sinon.stub().returns(@user_id) + @LockManager = + getLock : sinon.stub().yields() + releaseLock : sinon.stub().yields() @ProjectUploadController = SandboxedModule.require modulePath, requires: "./ProjectUploadManager" : @ProjectUploadManager = {} "./FileSystemImportManager" : @FileSystemImportManager = {} "logger-sharelatex" : @logger = {log: sinon.stub(), error: sinon.stub(), err:->} "metrics-sharelatex": @metrics '../Authentication/AuthenticationController': @AuthenticationController + "../../infrastructure/LockManager": @LockManager "fs" : @fs = {} describe "uploadProject", -> @@ -125,11 +129,17 @@ describe "ProjectUploadController", -> @FileSystemImportManager.addEntity = sinon.stub().callsArgWith(6, null, @entity) @ProjectUploadController.uploadFile @req, @res + it "should take the lock", -> + @LockManager.getLock.calledWith(@project_id).should.equal true + it "should insert the file", -> @FileSystemImportManager.addEntity .calledWith(@user_id, @project_id, @folder_id, @name, @path) .should.equal true + it "should release the lock", -> + @LockManager.releaseLock.calledWith(@project_id).should.equal true + it "should return a successful response to the FileUploader client", -> expect(@res.body).to.deep.equal success: true