mirror of
https://github.com/overleaf/overleaf.git
synced 2025-01-28 22:31:30 +00:00
lock project when uploading a file
This commit is contained in:
parent
8c29b93ae9
commit
fe62db05a0
2 changed files with 31 additions and 14 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue