2014-02-12 05:23:40 -05:00
Project = require ( ' ../../models/Project ' ) . Project
Doc = require ( ' ../../models/Doc ' ) . Doc
Folder = require ( ' ../../models/Folder ' ) . Folder
File = require ( ' ../../models/File ' ) . File
FileStoreHandler = require ( " ../FileStore/FileStoreHandler " )
Errors = require " ../../errors "
tpdsUpdateSender = require ( ' ../ThirdPartyDataStore/TpdsUpdateSender ' )
projectLocator = require ( ' ./ProjectLocator ' )
path = require " path "
async = require " async "
_ = require ( ' underscore ' )
logger = require ( ' logger-sharelatex ' )
docComparitor = require ( ' ./DocLinesComparitor ' )
projectUpdateHandler = require ( ' ./ProjectUpdateHandler ' )
2014-04-30 06:15:29 -04:00
DocstoreManager = require " ../Docstore/DocstoreManager "
2014-05-06 08:15:18 -04:00
ProjectGetter = require " ./ProjectGetter "
2014-02-12 05:23:40 -05:00
module.exports = ProjectEntityHandler =
2014-10-15 09:11:02 -04:00
getAllFolders: (project_id, callback) ->
logger . log project_id : project_id , " getting all folders for project "
2014-02-12 05:23:40 -05:00
folders = { }
processFolder = (basePath, folder) ->
folders [ basePath ] = folder
processFolder path . join ( basePath , childFolder . name ) , childFolder for childFolder in folder . folders
2014-05-06 08:15:18 -04:00
ProjectGetter . getProjectWithoutDocLines project_id , (err, project) ->
2014-02-12 05:23:40 -05:00
return callback ( err ) if err ?
return callback ( " no project " ) if ! project ?
processFolder " / " , project . rootFolder [ 0 ]
callback null , folders
2014-10-15 09:11:02 -04:00
getAllDocs: (project_id, callback) ->
2014-02-12 05:23:40 -05:00
logger . log project_id : project_id , " getting all docs for project "
2014-05-06 07:35:27 -04:00
# We get the path and name info from the project, and the lines and
# version info from the doc store.
DocstoreManager . getAllDocs project_id , (error, docContentsArray) ->
return callback ( error ) if error ?
# Turn array from docstore into a dictionary based on doc id
docContents = { }
for docContent in docContentsArray
docContents [ docContent . _id ] = docContent
2014-10-15 09:11:02 -04:00
ProjectEntityHandler . getAllFolders project_id , (error, folders) ->
2014-05-06 07:35:27 -04:00
return callback ( error ) if error ?
docs = { }
for folderPath , folder of folders
for doc in folder . docs
content = docContents [ doc . _id . toString ( ) ]
2015-02-18 16:25:53 -05:00
if content ?
docs [ path . join ( folderPath , doc . name ) ] = {
_id: doc . _id
name: doc . name
lines: content . lines
rev: content . rev
}
2014-05-06 07:35:27 -04:00
logger . log count : _ . keys ( docs ) . length , project_id : project_id , " returning docs for project "
callback null , docs
2014-02-12 05:23:40 -05:00
2014-10-15 09:11:02 -04:00
getAllFiles: (project_id, callback) ->
2014-02-12 05:23:40 -05:00
logger . log project_id : project_id , " getting all files for project "
2014-10-15 09:11:02 -04:00
@ getAllFolders project_id , (err, folders) ->
2014-02-12 05:23:40 -05:00
return callback ( err ) if err ?
files = { }
for folderPath , folder of folders
for file in folder . fileRefs
2014-10-31 06:34:18 -04:00
if file ?
files [ path . join ( folderPath , file . name ) ] = file
2014-02-12 05:23:40 -05:00
callback null , files
2014-10-15 09:11:02 -04:00
flushProjectToThirdPartyDataStore: (project_id, callback) ->
2014-02-12 05:23:40 -05:00
self = @
2014-10-15 09:11:02 -04:00
logger . log project_id : project_id , " flushing project to tpds "
2014-02-12 05:23:40 -05:00
documentUpdaterHandler = require ( ' ../../Features/DocumentUpdater/DocumentUpdaterHandler ' )
2014-10-31 08:41:06 -04:00
documentUpdaterHandler . flushProjectToMongo project_id , (error) ->
2014-05-06 07:35:27 -04:00
return callback ( error ) if error ?
Project . findById project_id , (error, project) ->
2014-02-12 05:23:40 -05:00
return callback ( error ) if error ?
requests = [ ]
2014-05-06 07:35:27 -04:00
self . getAllDocs project_id , (error, docs) ->
return callback ( error ) if error ?
2014-02-12 05:23:40 -05:00
for docPath , doc of docs
do (docPath, doc) ->
2016-02-23 10:50:30 -05:00
requests . push (cb) ->
tpdsUpdateSender . addDoc { project_id : project_id , doc_id : doc . _id , path : docPath , project_name : project . name , rev : doc . rev || 0 } , cb
2014-05-06 07:35:27 -04:00
self . getAllFiles project_id , (error, files) ->
return callback ( error ) if error ?
2014-02-12 05:23:40 -05:00
for filePath , file of files
do (filePath, file) ->
2016-02-23 10:50:30 -05:00
requests . push (cb) ->
tpdsUpdateSender . addFile { project_id : project_id , file_id : file . _id , path : filePath , project_name : project . name , rev : file . rev } , cb
2014-02-12 05:23:40 -05:00
async . series requests , (err) ->
2014-10-15 09:11:02 -04:00
logger . log project_id : project_id , " finished flushing project to tpds "
2014-02-12 05:23:40 -05:00
callback ( err )
2014-10-15 09:11:02 -04:00
setRootDoc: ( project_id , newRootDocID , callback = (error) -> ) ->
logger . log project_id: project_id , rootDocId: newRootDocID , " setting root doc "
2014-02-12 05:23:40 -05:00
Project . update { _id : project_id } , { rootDoc_id : newRootDocID } , { } , callback
2014-10-15 09:11:02 -04:00
unsetRootDoc: ( project_id , callback = (error) -> ) ->
logger . log project_id: project_id , " removing root doc "
2014-02-12 05:23:40 -05:00
Project . update { _id : project_id } , { $unset: { rootDoc_id: true } } , { } , callback
2014-06-06 09:24:45 -04:00
getDoc: ( project_id , doc_id , options = { } , callback = (error, lines, rev) -> ) ->
if typeof ( options ) == " function "
callback = options
options = { }
DocstoreManager . getDoc project_id , doc_id , options , callback
2014-05-08 08:42:30 -04:00
2015-12-15 08:27:41 -05:00
addDoc: ( project_id , folder_id , docName , docLines , callback = (error, doc, folder_id) -> ) =>
ProjectGetter . getProjectWithOnlyFolders project_id , (err, project) ->
2014-10-15 09:11:02 -04:00
logger . log project: project . _id , folder_id: folder_id , doc_name: docName , " adding doc "
2014-02-12 05:23:40 -05:00
return callback ( err ) if err ?
confirmFolder project , folder_id , (folder_id)=>
2014-05-07 09:31:46 -04:00
doc = new Doc name: docName
2015-10-16 07:34:33 -04:00
# Put doc in docstore first, so that if it errors, we don't have a doc_id in the project
# which hasn't been created in docstore.
DocstoreManager . updateDoc project . _id . toString ( ) , doc . _id . toString ( ) , docLines , (err, modified, rev) ->
return callback ( err ) if err ?
2016-02-24 10:36:39 -05:00
ProjectEntityHandler . _putElement project . _id , folder_id , doc , " doc " , (err, result)=>
2015-10-16 07:34:33 -04:00
return callback ( err ) if err ?
2014-05-07 09:31:46 -04:00
tpdsUpdateSender . addDoc {
project_id: project . _id ,
2014-05-20 08:18:59 -04:00
doc_id: doc . _id
2014-05-07 09:31:46 -04:00
path: result . path . fileSystem ,
project_name: project . name ,
rev: 0
2014-10-15 09:11:02 -04:00
} , (err) ->
2014-05-07 09:31:46 -04:00
return callback ( err ) if err ?
callback ( null , doc , folder_id )
2014-02-12 05:23:40 -05:00
2014-06-05 11:18:25 -04:00
restoreDoc: ( project_id , doc_id , name , callback = (error, doc, folder_id) -> ) ->
# getDoc will return the deleted doc's lines, but we don't actually remove
# the deleted doc, just create a new one from its lines.
2014-06-06 09:24:45 -04:00
ProjectEntityHandler . getDoc project_id , doc_id , include_deleted: true , (error, lines) ->
2014-06-05 11:18:25 -04:00
return callback ( error ) if error ?
ProjectEntityHandler . addDoc project_id , null , name , lines , callback
2014-10-15 09:11:02 -04:00
addFile: ( project_or_id , folder_id , fileName , path , callback = (error, fileRef, folder_id) -> ) ->
2015-10-21 11:56:06 -04:00
ProjectGetter . getProjectWithOnlyFolders project_or_id , (err, project) ->
2014-10-15 09:11:02 -04:00
logger . log project_id: project . _id , folder_id: folder_id , file_name: fileName , path : path , " adding file "
2014-02-12 05:23:40 -05:00
return callback ( err ) if err ?
confirmFolder project , folder_id , (folder_id)->
fileRef = new File name : fileName
FileStoreHandler . uploadFileFromDisk project . _id , fileRef . _id , path , (err)->
if err ?
logger . err err : err , project_id: project . _id , folder_id: folder_id , file_name: fileName , fileRef : fileRef , " error uploading image to s3 "
return callback ( err )
2016-02-24 10:36:39 -05:00
ProjectEntityHandler . _putElement project . _id , folder_id , fileRef , " file " , (err, result)=>
2014-10-15 09:11:02 -04:00
tpdsUpdateSender . addFile { project_id : project . _id , file_id : fileRef . _id , path : result . path . fileSystem , project_name : project . name , rev : fileRef . rev } , ->
2014-02-12 05:23:40 -05:00
callback ( err , fileRef , folder_id )
replaceFile: (project_or_id, file_id, fsPath, callback)->
Project . getProject project_or_id , " " , (err, project) ->
2014-06-04 06:46:01 -04:00
return callback ( err ) if err ?
2014-02-12 05:23:40 -05:00
findOpts =
project_id : project . _id
element_id : file_id
type : " file "
2014-06-04 06:46:01 -04:00
FileStoreHandler . uploadFileFromDisk project . _id , file_id , fsPath , (err)->
return callback ( err ) if err ?
# Note there is a potential race condition here (and elsewhere)
# If the file tree changes between findElement and the Project.update
# 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)=>
return callback ( err ) if err ?
2014-10-15 09:11:02 -04:00
tpdsUpdateSender . addFile { project_id : project . _id , file_id : fileRef . _id , path : path . fileSystem , rev : fileRef . rev + 1 , project_name : project . name } , (error) ->
2014-06-04 06:46:01 -04:00
return callback ( err ) if err ?
2014-02-12 05:23:40 -05:00
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 ( )
2014-10-15 09:11:02 -04:00
copyFileFromExistingProject: ( project_or_id , folder_id , originalProject_id , origonalFileRef , callback = (error, fileRef, folder_id) -> ) ->
2016-02-23 10:34:31 -05:00
Project . getProject project_or_id , " name " , (err, project) ->
2014-10-15 09:11:02 -04:00
logger . log project_id : project . _id , folder_id : folder_id , originalProject_id : originalProject_id , origonalFileRef : origonalFileRef , " copying file in s3 "
2014-02-12 05:23:40 -05:00
return callback ( err ) if err ?
confirmFolder project , folder_id , (folder_id)=>
2014-10-31 06:50:20 -04:00
if ! origonalFileRef ?
logger . err project_id : project . _id , folder_id : folder_id , originalProject_id : originalProject_id , origonalFileRef : origonalFileRef , " file trying to copy is null "
return callback ( )
2014-02-12 05:23:40 -05:00
fileRef = new File name : origonalFileRef . name
FileStoreHandler . copyFile originalProject_id , origonalFileRef . _id , project . _id , fileRef . _id , (err)->
if err ?
logger . err err : err , project_id : project . _id , folder_id : folder_id , originalProject_id : originalProject_id , origonalFileRef : origonalFileRef , " error coping file in s3 "
2016-02-24 10:36:39 -05:00
ProjectEntityHandler . _putElement project . _id , folder_id , fileRef , " file " , (err, result)=>
2014-10-15 09:11:02 -04:00
tpdsUpdateSender . addFile { project_id : project . _id , file_id : fileRef . _id , path : result . path . fileSystem , rev : fileRef . rev , project_name : project . name } , (error) ->
2014-02-12 05:23:40 -05:00
callback ( error , fileRef , folder_id )
2014-11-27 07:07:49 -05:00
mkdirp: ( project_id , path , callback = (err, newlyCreatedFolders, lastFolderInPath)-> ) ->
2014-02-12 05:23:40 -05:00
self = @
folders = path . split ( ' / ' )
folders = _ . select folders , (folder)->
return folder . length != 0
2016-02-23 10:36:59 -05:00
ProjectGetter . getProjectWithOnlyFolders project_id , (err, project)=>
2014-02-12 05:23:40 -05:00
if path == ' / '
logger . log project_id: project . _id , " mkdir is only trying to make path of / so sending back root folder "
return callback ( null , [ ] , project . rootFolder [ 0 ] )
logger . log project_id: project . _id , path : path , folders : folders , " running mkdirp "
builtUpPath = ' '
procesFolder = (previousFolders, folderName, callback)=>
previousFolders = previousFolders || [ ]
parentFolder = previousFolders [ previousFolders . length - 1 ]
if parentFolder ?
parentFolder_id = parentFolder . _id
builtUpPath = " #{ builtUpPath } / #{ folderName } "
2014-11-27 07:07:49 -05:00
projectLocator . findElementByPath project_id , builtUpPath , (err, foundFolder)=>
2014-02-12 05:23:40 -05:00
if ! foundFolder ?
2014-10-15 09:11:02 -04:00
logger . log path : path , project_id : project . _id , folderName : folderName , " making folder from mkdirp "
2014-11-27 07:07:49 -05:00
@ addFolder project_id , parentFolder_id , folderName , (err, newFolder, parentFolder_id)->
2014-02-12 05:23:40 -05:00
newFolder.parentFolder_id = parentFolder_id
previousFolders . push newFolder
callback null , previousFolders
else
foundFolder.filterOut = true
previousFolders . push foundFolder
callback null , previousFolders
async . reduce folders , [ ] , procesFolder , (err, folders)->
lastFolder = folders [ folders . length - 1 ]
folders = _ . select folders , (folder)->
! folder . filterOut
callback ( null , folders , lastFolder )
2016-02-23 10:45:40 -05:00
addFolder: (project_id, parentFolder_id, folderName, callback) ->
2014-02-12 05:23:40 -05:00
folder = new Folder name: folderName
2016-02-23 10:45:40 -05:00
ProjectGetter . getProjectWithOnlyFolders project_id , (err, project)=>
2014-02-12 05:23:40 -05:00
return callback ( err ) if err ?
confirmFolder project , parentFolder_id , (parentFolder_id)=>
2016-02-23 10:45:40 -05:00
logger . log project: project_id , parentFolder_id : parentFolder_id , folderName : folderName , " new folder added "
2016-02-24 10:36:39 -05:00
ProjectEntityHandler . _putElement project . _id , parentFolder_id , folder , " folder " , (err, result)=>
2014-02-12 05:23:40 -05:00
if callback ?
callback ( err , folder , parentFolder_id )
2014-05-15 07:44:08 -04:00
updateDocLines : ( project_id , doc_id , lines , callback = (error) -> ) ->
2014-05-08 10:47:50 -04:00
ProjectGetter . getProjectWithoutDocLines project_id , (err, project)->
2014-02-12 05:23:40 -05:00
return callback ( err ) if err ?
return callback ( new Errors . NotFoundError ( " project not found " ) ) if ! project ?
2014-05-15 07:44:08 -04:00
logger . log project_id: project_id , doc_id: doc_id , " updating doc lines "
2014-05-08 10:47:50 -04:00
projectLocator . findElement { project : project , element_id : doc_id , type : " docs " } , (err, doc, path)->
if err ?
2014-05-15 07:44:08 -04:00
logger . error err: err , doc_id: doc_id , project_id: project_id , lines: lines , " error finding doc while updating doc lines "
2014-05-08 10:47:50 -04:00
return callback err
if ! doc ?
error = new Errors . NotFoundError ( " doc not found " )
2014-05-15 07:44:08 -04:00
logger . error err: error , doc_id: doc_id , project_id: project_id , lines: lines , " doc not found while updating doc lines "
2014-05-08 10:47:50 -04:00
return callback ( error )
2014-12-12 07:21:14 -05:00
logger . log project_id: project_id , doc_id: doc_id , " telling docstore manager to update doc "
2014-05-15 07:44:08 -04:00
DocstoreManager . updateDoc project_id , doc_id , lines , (err, modified, rev) ->
2014-02-12 05:23:40 -05:00
if err ?
2014-05-15 07:44:08 -04:00
logger . error err: err , doc_id: doc_id , project_id : project_id , lines: lines , " error sending doc to docstore "
2014-05-08 10:47:50 -04:00
return callback ( err )
2014-12-12 07:21:14 -05:00
logger . log project_id: project_id , doc_id: doc_id , modified : modified , " finished updating doc lines "
2014-05-08 10:47:50 -04:00
if modified
# Don't need to block for marking as updated
projectUpdateHandler . markAsUpdated project_id
2014-05-20 08:18:59 -04:00
tpdsUpdateSender . addDoc { project_id : project_id , path : path . fileSystem , doc_id : doc_id , project_name : project . name , rev : rev } , callback
2014-02-12 05:23:40 -05:00
else
2014-05-08 10:47:50 -04:00
callback ( )
2014-02-12 05:23:40 -05:00
2014-08-22 09:38:41 -04:00
moveEntity: ( project_id , entity_id , folder_id , entityType , callback = (error) -> ) ->
2014-02-12 05:23:40 -05:00
self = @
destinationFolder_id = folder_id
2014-08-22 09:38:41 -04:00
logger . log entityType : entityType , entity_id : entity_id , project_id : project_id , folder_id : folder_id , " moving entity "
2014-02-12 05:23:40 -05:00
if ! entityType ?
logger . err err: " No entityType set " , project_id: project_id , entity_id: entity_id
return callback ( " No entityType set " )
entityType = entityType . toLowerCase ( )
Project . findById project_id , (err, project)=>
2014-08-22 09:38:41 -04:00
return callback ( err ) if err ?
2014-02-12 05:23:40 -05:00
projectLocator . findElement { project : project , element_id : entity_id , type : entityType } , (err, entity, path)->
return callback ( err ) if err ?
2014-08-22 09:38:41 -04:00
if entityType . match ( /folder/ )
ensureFolderIsNotMovedIntoChild = ( callback = (error) -> ) ->
projectLocator . findElement { project: project , element_id: folder_id , type : " folder " } , (err, destEntity, destPath) ->
logger . log destPath: destPath . fileSystem , folderPath: path . fileSystem , " checking folder is not moving into child folder "
if ( destPath . fileSystem . slice ( 0 , path . fileSystem . length ) == path . fileSystem )
logger . log " destination is a child folder, aborting "
callback ( new Error ( " destination folder is a child folder of me " ) )
else
callback ( )
else
ensureFolderIsNotMovedIntoChild = ( callback = () -> ) -> callback ( )
ensureFolderIsNotMovedIntoChild (error) ->
return callback ( error ) if error ?
self . _removeElementFromMongoArray Project , project_id , path . mongo , (err)->
2014-02-12 05:23:40 -05:00
return callback ( err ) if err ?
2016-02-24 10:36:39 -05:00
ProjectEntityHandler . _putElement project_id , destinationFolder_id , entity , entityType , (err, result)->
2014-08-22 09:38:41 -04:00
return callback ( err ) if err ?
opts =
project_id : project_id
project_name : project . name
startPath : path . fileSystem
endPath : result . path . fileSystem ,
rev : entity . rev
tpdsUpdateSender . moveEntity opts , callback
2014-02-12 05:23:40 -05:00
2014-10-15 09:11:02 -04:00
deleteEntity: ( project_id , entity_id , entityType , callback = (error) -> ) ->
2014-02-12 05:23:40 -05:00
self = @
2014-10-20 09:22:31 -04:00
logger . log entity_id : entity_id , entityType : entityType , project_id : project_id , " deleting project entity "
2014-02-12 05:23:40 -05:00
if ! entityType ?
logger . err err: " No entityType set " , project_id: project_id , entity_id: entity_id
return callback ( " No entityType set " )
entityType = entityType . toLowerCase ( )
Project . findById project_id , (err, project)=>
return callback ( error ) if error ?
projectLocator . findElement { project: project , element_id: entity_id , type: entityType } , (error, entity, path)=>
return callback ( error ) if error ?
ProjectEntityHandler . _cleanUpEntity project , entity , entityType , (error) ->
return callback ( error ) if error ?
2014-10-15 09:11:02 -04:00
tpdsUpdateSender . deleteEntity project_id : project_id , path : path . fileSystem , project_name : project . name , (error) ->
2014-02-12 05:23:40 -05:00
return callback ( error ) if error ?
self . _removeElementFromMongoArray Project , project_id , path . mongo , (error) ->
return callback ( error ) if error ?
callback null
2014-04-07 07:18:24 -04:00
renameEntity: (project_id, entity_id, entityType, newName, callback)->
logger . log ( entity_id: entity_id , project_id: project_id , ( ' renaming ' + entityType ) )
if ! entityType ?
logger . err err: " No entityType set " , project_id: project_id , entity_id: entity_id
return callback ( " No entityType set " )
entityType = entityType . toLowerCase ( )
Project . findById project_id , (err, project)=>
projectLocator . findElement { project : project , element_id : entity_id , type : entityType } , (err, entity, path, folder)=>
if err ?
return callback err
conditons = { _id : project_id }
update = " $set " : { }
namePath = path . mongo + " .name "
update [ " $set " ] [ namePath ] = newName
endPath = path . fileSystem . replace ( entity . name , newName )
tpdsUpdateSender . moveEntity ( { project_id : project_id , startPath : path . fileSystem , endPath : endPath , project_name : project . name , rev : entity . rev } )
Project . update conditons , update , { } , (err)->
if callback ?
callback err
2014-10-15 09:11:02 -04:00
_cleanUpEntity: ( project , entity , entityType , callback = (error) -> ) ->
2014-02-12 05:23:40 -05:00
if ( entityType . indexOf ( " file " ) != - 1 )
2014-10-15 09:11:02 -04:00
ProjectEntityHandler . _cleanUpFile project , entity , callback
2014-02-12 05:23:40 -05:00
else if ( entityType . indexOf ( " doc " ) != - 1 )
2014-10-15 09:11:02 -04:00
ProjectEntityHandler . _cleanUpDoc project , entity , callback
2014-02-12 05:23:40 -05:00
else if ( entityType . indexOf ( " folder " ) != - 1 )
2014-10-15 09:11:02 -04:00
ProjectEntityHandler . _cleanUpFolder project , entity , callback
2014-02-12 05:23:40 -05:00
else
callback ( )
2014-10-15 09:11:02 -04:00
_cleanUpDoc: ( project , doc , callback = (error) -> ) ->
2014-02-12 05:23:40 -05:00
project_id = project . _id . toString ( )
doc_id = doc . _id . toString ( )
unsetRootDocIfRequired = (callback) =>
if project . rootDoc_id ? and project . rootDoc_id . toString ( ) == doc_id
@ unsetRootDoc project_id , callback
else
callback ( )
unsetRootDocIfRequired (error) ->
return callback ( error ) if error ?
2014-04-30 06:15:29 -04:00
require ( ' ../../Features/DocumentUpdater/DocumentUpdaterHandler ' ) . deleteDoc project_id , doc_id , (error) ->
return callback ( error ) if error ?
ProjectEntityHandler . _insertDeletedDocReference project . _id , doc , (error) ->
return callback ( error ) if error ?
DocstoreManager . deleteDoc project_id , doc_id , (error) ->
return callback ( error ) if error ?
callback ( )
2014-02-12 05:23:40 -05:00
2014-10-15 09:11:02 -04:00
_cleanUpFile: ( project , file , callback = (error) -> ) ->
2014-02-12 05:23:40 -05:00
project_id = project . _id . toString ( )
file_id = file . _id . toString ( )
FileStoreHandler . deleteFile project_id , file_id , callback
2014-10-15 09:11:02 -04:00
_cleanUpFolder: ( project , folder , callback = (error) -> ) ->
2014-02-12 05:23:40 -05:00
jobs = [ ]
for doc in folder . docs
do (doc) ->
2014-10-15 09:11:02 -04:00
jobs . push (callback) -> ProjectEntityHandler . _cleanUpDoc project , doc , callback
2014-02-12 05:23:40 -05:00
for file in folder . fileRefs
do (file) ->
2014-10-15 09:11:02 -04:00
jobs . push (callback) -> ProjectEntityHandler . _cleanUpFile project , file , callback
2014-02-12 05:23:40 -05:00
for childFolder in folder . folders
do (childFolder) ->
2014-10-15 09:11:02 -04:00
jobs . push (callback) -> ProjectEntityHandler . _cleanUpFolder project , childFolder , callback
2014-02-12 05:23:40 -05:00
async . series jobs , callback
_removeElementFromMongoArray : (model, model_id, path, callback)->
conditons = { _id : model_id }
update = { " $unset " : { } }
update [ " $unset " ] [ path ] = 1
model . update conditons , update , { } , (err)->
pullUpdate = { " $pull " : { } }
nonArrayPath = path . slice ( 0 , path . lastIndexOf ( " . " ) )
pullUpdate [ " $pull " ] [ nonArrayPath ] = null
model . update conditons , pullUpdate , { } , (err)->
if callback ?
callback ( err )
2014-04-30 06:15:29 -04:00
_insertDeletedDocReference: ( project_id , doc , callback = (error) -> ) ->
Project . update {
_id: project_id
} , {
$push: {
deletedDocs: {
_id: doc . _id
name: doc . name
}
}
} , { } , callback
2016-02-24 10:36:39 -05:00
_putElement: ( project_id , folder_id , element , type , callback = (err, path)-> ) ->
sanitizeTypeOfElement = (elementType)->
lastChar = elementType . slice - 1
if lastChar != " s "
elementType += " s "
if elementType == " files "
elementType = " fileRefs "
return elementType
if ! element ?
e = new Error ( " no element passed to be inserted " )
logger . err project_id : project_id , folder_id : folder_id , element : element , type : type , " failed trying to insert element as it was null "
return callback ( e )
type = sanitizeTypeOfElement type
ProjectGetter . getProject project_id , " rootFolder " , (err, project)=>
if err ?
return callback ( err )
if ! folder_id ?
folder_id = project . rootFolder [ 0 ] . _id
projectLocator . findElement { project : project , element_id : folder_id , type : " folders " } , (err, folder, path)=>
newPath =
fileSystem: " #{ path . fileSystem } / #{ element . name } "
mongo: path . mongo
if err ?
logger . err err : err , project_id : project_id , folder_id : folder_id , type : type , element : element , " error finding folder for _putElement "
return callback ( err )
logger . log project_id: project_id , element_id: element . _id , fileType: type , folder_id: folder_id , " adding element to project "
id = element . _id + ' '
element._id = require ( ' mongoose ' ) . Types . ObjectId ( id )
conditions = _id : project_id
mongopath = " #{ path . mongo } . #{ type } "
update = " $push " : { }
update [ " $push " ] [ mongopath ] = element
Project . update conditions , update , { } , (err)->
if err ?
logger . err err: err , project_id: project_id , ' error saving in putElement project '
return callback ( err )
callback ( err , { path : newPath } )
2014-02-12 05:23:40 -05:00
confirmFolder = (project, folder_id, callback)->
logger . log folder_id : folder_id , project_id : project . _id , " confirming folder in project "
if folder_id + ' ' == ' undefined '
callback ( project . rootFolder [ 0 ] . _id )
else if folder_id != null
callback folder_id
else
callback ( project . rootFolder [ 0 ] . _id )