version replacing files

This commit is contained in:
Hayden Faulds 2017-11-23 16:40:15 +00:00
parent 980e43e478
commit 90e3b71c1c
2 changed files with 40 additions and 24 deletions

View file

@ -223,12 +223,9 @@ module.exports = ProjectEntityHandler =
callback null, fileRef, folder_id
replaceFile: (project_id, file_id, fsPath, callback)->
self = ProjectEntityHandler
ProjectGetter.getProject project_id, {name:true}, (err, project) ->
return callback(err) if err?
findOpts =
project_id:project._id
element_id:file_id
type:"file"
FileStoreHandler.uploadFileFromDisk project._id, file_id, fsPath, (err)->
return callback(err) if err?
# Note there is a potential race condition here (and elsewhere)
@ -236,20 +233,25 @@ module.exports = ProjectEntityHandler =
# then the path to the file element will be out of date. In practice
# this is not a problem so long as we do not do anything longer running
# between them (like waiting for the file to upload.)
projectLocator.findElement findOpts, (err, fileRef, path)=>
self.getAllEntitiesFromProject project, (err, oldDocs, oldFiles) =>
return callback(err) if err?
tpdsUpdateSender.addFile {project_id:project._id, file_id:fileRef._id, path:path.fileSystem, rev:fileRef.rev+1, project_name:project.name}, (error) ->
projectLocator.findElement {project:project, element_id: file_id, type: 'file'}, (err, fileRef, path)=>
return callback(err) if err?
conditons = _id:project._id
inc = {}
inc["#{path.mongo}.rev"] = 1
set = {}
set["#{path.mongo}.created"] = new Date()
update =
"$inc": inc
"$set": set
Project.update conditons, update, {}, (err, second)->
callback()
tpdsUpdateSender.addFile {project_id:project._id, file_id:fileRef._id, path:path.fileSystem, rev:fileRef.rev+1, project_name:project.name}, (err) ->
return callback(err) if err?
conditions = _id:project._id
inc = {}
inc["#{path.mongo}.rev"] = 1
set = {}
set["#{path.mongo}.created"] = new Date()
update =
"$inc": inc
"$set": set
Project.findOneAndUpdate conditions, update, { "new": true}, (err, newProject) ->
return callback(err) if err?
self.getAllEntitiesFromProject newProject, (err, newDocs, newFiles) =>
return callback(err) if err?
DocumentUpdaterHandler.updateProjectStructure project_id, null, oldDocs, newDocs, oldFiles, newFiles, callback
copyFileFromExistingProject: (project_id, folder_id, originalProject_id, origonalFileRef, callback = (error, fileRef, folder_id) ->)->
logger.log project_id:project_id, folder_id:folder_id, originalProject_id:originalProject_id, origonalFileRef:origonalFileRef, "copying file in s3"
@ -392,8 +394,7 @@ module.exports = ProjectEntityHandler =
endPath: result.path.fileSystem,
rev: entity.rev
tpdsUpdateSender.moveEntity opts
self.getAllEntitiesFromProject newProject, (error, newDocs, newFiles
) =>
self.getAllEntitiesFromProject newProject, (error, newDocs, newFiles) =>
return callback(error) if error?
DocumentUpdaterHandler.updateProjectStructure project_id, userId, oldDocs, newDocs, oldFiles, newFiles, callback

View file

@ -583,7 +583,6 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, () ->
describe 'replacing a file', ->
beforeEach ->
@projectLocator
@file_id = "file_id_here"
@ -591,13 +590,23 @@ describe 'ProjectEntityHandler', ->
@fileRef = {rev:3, _id:@file_id}
@filePaths = {fileSystem:"/folder1/file.png", mongo:"folder.1.files.somewhere"}
@projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
@ProjectModel.update = (_, __, ___, cb)-> cb()
@ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
@ProjectEntityHandler.getAllEntitiesFromProject
.onFirstCall()
.callsArgWith(1, null, [], @oldFiles = ['old-file'])
@ProjectEntityHandler.getAllEntitiesFromProject
.onSecondCall()
.callsArgWith(1, null, [], @newFiles = ['new-file'])
@ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
it 'should find the file', (done)->
@ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
@projectLocator.findElement.calledWith({element_id:@file_id, type:"file", project_id:project_id}).should.equal true
@projectLocator.findElement
.calledWith({element_id:@file_id, type:"file", project: @project})
.should.equal true
done()
it 'should tell the file store handler to upload the file from disk', (done)->
@ -617,7 +626,7 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
it 'should inc the rev id', (done)->
@ProjectModel.update = (conditions, update, options, callback)=>
@ProjectModel.findOneAndUpdate = (conditions, update, options, callback)=>
conditions._id.should.equal project_id
update.$inc["#{@filePaths.mongo}.rev"].should.equal 1
done()
@ -626,7 +635,7 @@ describe 'ProjectEntityHandler', ->
it 'should update the created at date', (done)->
d = new Date()
@ProjectModel.update = (conditions, update, options, callback)=>
@ProjectModel.findOneAndUpdate = (conditions, update, options, callback)=>
conditions._id.should.equal project_id
differenceInMs = update.$set["#{@filePaths.mongo}.created"].getTime() - d.getTime()
differenceInMs.should.be.below(20)
@ -634,6 +643,12 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
it "should should send the old and new project structure to the doc updater", (done) ->
@ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
@documentUpdaterHandler.updateProjectStructure
.calledWith(project_id, null, [], [], @oldFiles, @newFiles)
.should.equal true
done()
describe 'addFolder', ->
folderName = "folder1234"