Merge pull request #244 from sharelatex/hof-lock-file-upload

Project Locking: ProjectUploadController
This commit is contained in:
James Allen 2018-01-11 08:42:36 +00:00 committed by GitHub
commit 945ef25ef5
3 changed files with 33 additions and 15 deletions

View file

@ -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,21 @@ 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.runWithLock project_id,
(cb) -> FileSystemImportManager.addEntity user_id, project_id, folder_id, name, path, true, cb
(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

View file

@ -51,4 +51,11 @@ module.exports = LockManager =
releaseLock: (key, callback)->
rclient.del LockManager._blockingKey(key), callback
runWithLock: (key, runner = ( (releaseLock = (error) ->) -> ), callback = ( (error) -> )) ->
LockManager.getLock key, (error) ->
return callback(error) if error?
runner (error1, values...) ->
LockManager.releaseLock key, (error2) ->
error = error1 or error2
return callback(error) if error?
callback null, values...

View file

@ -17,12 +17,16 @@ describe "ProjectUploadController", ->
done: sinon.stub()
@AuthenticationController =
getLoggedInUserId: sinon.stub().returns(@user_id)
@LockManager =
runWithLock : sinon.spy((key, runner, callback) -> runner(callback))
@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,6 +129,9 @@ describe "ProjectUploadController", ->
@FileSystemImportManager.addEntity = sinon.stub().callsArgWith(6, null, @entity)
@ProjectUploadController.uploadFile @req, @res
it "should take the lock", ->
@LockManager.runWithLock.calledWith(@project_id).should.equal true
it "should insert the file", ->
@FileSystemImportManager.addEntity
.calledWith(@user_id, @project_id, @folder_id, @name, @path)