2014-02-12 05:23:40 -05:00
|
|
|
Project = require('../../models/Project').Project
|
2016-02-29 08:05:17 -05:00
|
|
|
settings = require "settings-sharelatex"
|
2014-02-12 05:23:40 -05:00
|
|
|
Doc = require('../../models/Doc').Doc
|
|
|
|
Folder = require('../../models/Folder').Folder
|
|
|
|
File = require('../../models/File').File
|
|
|
|
FileStoreHandler = require("../FileStore/FileStoreHandler")
|
2016-03-23 11:14:49 -04:00
|
|
|
Errors = require "../Errors/Errors"
|
2014-02-12 05:23:40 -05:00
|
|
|
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"
|
2017-05-09 09:20:29 -04:00
|
|
|
CooldownManager = require '../Cooldown/CooldownManager'
|
2017-11-15 10:07:55 -05:00
|
|
|
DocumentUpdaterHandler = require('../../Features/DocumentUpdater/DocumentUpdaterHandler')
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = ProjectEntityHandler =
|
2014-10-15 09:11:02 -04:00
|
|
|
getAllFolders: (project_id, callback) ->
|
2017-08-03 05:30:50 -04:00
|
|
|
logger.log project_id:project_id, "getting all folders for project"
|
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?
|
2017-08-01 09:39:02 -04:00
|
|
|
ProjectEntityHandler.getAllFoldersFromProject project, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
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
|
|
|
|
|
2016-03-23 06:54:36 -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
|
2016-03-23 06:54:36 -04:00
|
|
|
for doc in (folder.docs or [])
|
2014-05-06 07:35:27 -04:00
|
|
|
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"
|
2016-03-23 06:54:36 -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
|
2016-03-23 06:54:36 -04:00
|
|
|
for file in (folder.fileRefs or [])
|
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
|
|
|
|
|
2017-08-01 09:39:02 -04:00
|
|
|
getAllFoldersFromProject: (project, callback) ->
|
|
|
|
folders = {}
|
|
|
|
processFolder = (basePath, folder) ->
|
|
|
|
folders[basePath] = folder
|
|
|
|
for childFolder in (folder.folders or [])
|
|
|
|
if childFolder.name?
|
|
|
|
processFolder path.join(basePath, childFolder.name), childFolder
|
|
|
|
|
|
|
|
processFolder "/", project.rootFolder[0]
|
|
|
|
callback null, folders
|
|
|
|
|
2017-08-16 05:49:29 -04:00
|
|
|
getAllEntitiesFromProject: (project, callback) ->
|
|
|
|
logger.log project:project, "getting all files for project"
|
|
|
|
@getAllFoldersFromProject project, (err, folders = {}) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
docs = []
|
|
|
|
files = []
|
|
|
|
for folderPath, folder of folders
|
|
|
|
for doc in (folder.docs or [])
|
|
|
|
if doc?
|
|
|
|
docs.push({path: path.join(folderPath, doc.name), doc:doc})
|
|
|
|
for file in (folder.fileRefs or [])
|
|
|
|
if file?
|
|
|
|
files.push({path: path.join(folderPath, file.name), file:file})
|
|
|
|
callback null, docs, files
|
|
|
|
|
2017-08-01 09:39:02 -04:00
|
|
|
getAllDocPathsFromProject: (project, callback) ->
|
|
|
|
logger.log project:project, "getting all docs for project"
|
|
|
|
@getAllFoldersFromProject project, (err, folders = {}) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
docPath = {}
|
|
|
|
for folderPath, folder of folders
|
|
|
|
for doc in (folder.docs or [])
|
|
|
|
docPath[doc._id] = path.join(folderPath, doc.name)
|
|
|
|
logger.log count:_.keys(docPath).length, project_id:project._id, "returning docPaths for project"
|
|
|
|
callback null, docPath
|
|
|
|
|
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"
|
2017-11-15 10:07:55 -05:00
|
|
|
DocumentUpdaterHandler.flushProjectToMongo project_id, (error) ->
|
2014-05-06 07:35:27 -04:00
|
|
|
return callback(error) if error?
|
2016-02-29 11:03:22 -05:00
|
|
|
ProjectGetter.getProject project_id, {name:true}, (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
|
2017-09-28 10:01:45 -04:00
|
|
|
|
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 = {}
|
2014-05-08 08:42:30 -04:00
|
|
|
|
2017-09-28 10:01:45 -04:00
|
|
|
if options["pathname"]
|
|
|
|
delete options["pathname"]
|
|
|
|
projectLocator.findElement {project_id: project_id, element_id: doc_id, type: 'doc'}, (error, doc, path) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
DocstoreManager.getDoc project_id, doc_id, options, (error, lines, rev, version, ranges) =>
|
|
|
|
callback(error, lines, rev, version, ranges, path.fileSystem)
|
|
|
|
else
|
|
|
|
DocstoreManager.getDoc project_id, doc_id, options, callback
|
2016-03-09 06:16:27 -05:00
|
|
|
|
2017-12-18 11:39:27 -05:00
|
|
|
addDoc: (project_or_id, folder_id, docName, docLines, userId, callback = (error, doc, folder_id) ->)=>
|
2017-12-19 06:52:15 -05:00
|
|
|
ProjectEntityHandler.addDocWithoutUpdatingHistory project_or_id, folder_id, docName, docLines, userId, (error, doc, folder_id, path) ->
|
2017-12-18 11:39:27 -05:00
|
|
|
return callback(error) if error?
|
|
|
|
newDocs = [
|
|
|
|
doc: doc
|
2017-12-19 06:52:15 -05:00
|
|
|
path: path
|
2017-12-18 11:39:27 -05:00
|
|
|
docLines: docLines.join('\n')
|
|
|
|
]
|
|
|
|
project_id = project_or_id._id or project_or_id
|
|
|
|
DocumentUpdaterHandler.updateProjectStructure project_id, userId, {newDocs}, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null, doc, folder_id
|
|
|
|
|
|
|
|
addDocWithoutUpdatingHistory: (project_or_id, folder_id, docName, docLines, userId, callback = (error, doc, folder_id) ->)=>
|
|
|
|
# This method should never be called directly, except when importing a project
|
|
|
|
# from Overleaf. It skips sending updates to the project history, which will break
|
|
|
|
# the history unless you are making sure it is updated in some other way.
|
|
|
|
getProject = (cb) ->
|
|
|
|
if project_or_id._id? # project
|
|
|
|
return cb(null, project_or_id)
|
|
|
|
else # id
|
2018-01-19 07:01:23 -05:00
|
|
|
# need to retrieve full project structure to check for duplicates
|
2018-01-23 05:21:05 -05:00
|
|
|
return ProjectGetter.getProject project_or_id, {rootFolder:true, name:true}, cb
|
2017-12-18 11:39:27 -05:00
|
|
|
getProject (error, project) ->
|
2016-03-09 06:16:27 -05:00
|
|
|
if err?
|
|
|
|
logger.err project_id:project_id, err:err, "error getting project for add doc"
|
|
|
|
return callback(err)
|
2017-12-18 11:39:27 -05:00
|
|
|
ProjectEntityHandler._addDocWithProject project, folder_id, docName, docLines, userId, callback
|
2016-03-09 06:16:27 -05:00
|
|
|
|
2017-12-19 06:52:15 -05:00
|
|
|
_addDocWithProject: (project, folder_id, docName, docLines, userId, callback = (error, doc, folder_id, path) ->)=>
|
2016-03-09 06:16:27 -05:00
|
|
|
project_id = project._id
|
|
|
|
logger.log project_id: project_id, folder_id: folder_id, doc_name: docName, "adding doc to project with project"
|
|
|
|
confirmFolder project, folder_id, (folder_id)=>
|
|
|
|
doc = new Doc name: docName
|
|
|
|
# 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.
|
2016-12-08 09:09:06 -05:00
|
|
|
DocstoreManager.updateDoc project_id.toString(), doc._id.toString(), docLines, 0, {}, (err, modified, rev) ->
|
2017-09-28 10:01:45 -04:00
|
|
|
return callback(err) if err?
|
2016-03-09 06:16:27 -05:00
|
|
|
|
|
|
|
ProjectEntityHandler._putElement project, folder_id, doc, "doc", (err, result)=>
|
|
|
|
return callback(err) if err?
|
|
|
|
tpdsUpdateSender.addDoc {
|
|
|
|
project_id: project_id,
|
|
|
|
doc_id: doc?._id
|
|
|
|
path: result?.path?.fileSystem,
|
|
|
|
project_name: project.name,
|
|
|
|
rev: 0
|
|
|
|
}, (err) ->
|
2017-04-11 10:41:17 -04:00
|
|
|
return callback(err) if err?
|
2017-12-19 06:52:15 -05:00
|
|
|
callback(null, doc, folder_id, result?.path?.fileSystem)
|
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
|
|
|
|
|
2017-12-19 06:52:15 -05:00
|
|
|
addFileWithoutUpdatingHistory: (project_id, folder_id, fileName, path, userId, callback = (error, fileRef, folder_id, path, fileStoreUrl) ->)->
|
2018-01-23 05:21:05 -05:00
|
|
|
ProjectGetter.getProject project_id, {rootFolder:true, name:true}, (err, project) ->
|
2016-03-09 06:16:27 -05:00
|
|
|
if err?
|
|
|
|
logger.err project_id:project_id, err:err, "error getting project for add file"
|
|
|
|
return callback(err)
|
2017-12-18 11:39:27 -05:00
|
|
|
logger.log project_id: project._id, folder_id: folder_id, file_name: fileName, path:path, "adding file"
|
|
|
|
return callback(err) if err?
|
|
|
|
confirmFolder project, folder_id, (folder_id)->
|
|
|
|
fileRef = new File name : fileName
|
|
|
|
FileStoreHandler.uploadFileFromDisk project._id, fileRef._id, path, (err, fileStoreUrl)->
|
2016-03-14 10:42:13 -04:00
|
|
|
if err?
|
2017-12-18 11:39:27 -05:00
|
|
|
logger.err err:err, project_id: project._id, folder_id: folder_id, file_name: fileName, fileRef:fileRef, "error uploading image to s3"
|
2016-03-14 10:42:13 -04:00
|
|
|
return callback(err)
|
2017-12-18 11:39:27 -05:00
|
|
|
ProjectEntityHandler._putElement project, folder_id, fileRef, "file", (err, result)=>
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id: project._id, folder_id: folder_id, file_name: fileName, fileRef:fileRef, "error adding file with project"
|
|
|
|
return callback(err)
|
|
|
|
tpdsUpdateSender.addFile {project_id:project._id, file_id:fileRef._id, path:result?.path?.fileSystem, project_name:project.name, rev:fileRef.rev}, (err) ->
|
|
|
|
return callback(err) if err?
|
2017-12-19 06:52:15 -05:00
|
|
|
callback(null, fileRef, folder_id, result?.path?.fileSystem, fileStoreUrl)
|
2017-12-18 11:39:27 -05:00
|
|
|
|
2017-12-19 06:52:15 -05:00
|
|
|
addFile: (project_id, folder_id, fileName, fsPath, userId, callback = (error, fileRef, folder_id) ->)->
|
|
|
|
ProjectEntityHandler.addFileWithoutUpdatingHistory project_id, folder_id, fileName, fsPath, userId, (error, fileRef, folder_id, path, fileStoreUrl) ->
|
2017-12-18 11:39:27 -05:00
|
|
|
newFiles = [
|
|
|
|
file: fileRef
|
2017-12-19 06:52:15 -05:00
|
|
|
path: path
|
2017-12-18 11:39:27 -05:00
|
|
|
url: fileStoreUrl
|
|
|
|
]
|
|
|
|
DocumentUpdaterHandler.updateProjectStructure project_id, userId, {newFiles}, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null, fileRef, folder_id
|
2016-03-09 06:16:27 -05:00
|
|
|
|
2017-11-24 06:26:43 -05:00
|
|
|
replaceFile: (project_id, file_id, fsPath, userId, callback)->
|
2017-11-23 11:40:15 -05:00
|
|
|
self = ProjectEntityHandler
|
2017-11-29 06:37:47 -05:00
|
|
|
FileStoreHandler.uploadFileFromDisk project_id, file_id, fsPath, (err, fileStoreUrl)->
|
2014-06-04 06:46:01 -04:00
|
|
|
return callback(err) if err?
|
2017-11-27 12:10:20 -05:00
|
|
|
ProjectGetter.getProject project_id, {rootFolder: true, name:true}, (err, project) ->
|
2014-06-04 06:46:01 -04:00
|
|
|
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.)
|
2017-11-29 06:37:47 -05:00
|
|
|
projectLocator.findElement {project:project, element_id: file_id, type: 'file'}, (err, fileRef, path)=>
|
2014-06-04 06:46:01 -04:00
|
|
|
return callback(err) if err?
|
2017-11-29 06:37:47 -05:00
|
|
|
tpdsUpdateSender.addFile {project_id:project._id, file_id:fileRef._id, path:path.fileSystem, rev:fileRef.rev+1, project_name:project.name}, (err) ->
|
2014-06-04 06:46:01 -04:00
|
|
|
return callback(err) if err?
|
2017-11-29 06:37:47 -05:00
|
|
|
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) ->
|
2017-11-23 11:40:15 -05:00
|
|
|
return callback(err) if err?
|
2017-11-30 10:46:37 -05:00
|
|
|
newFiles = [
|
2017-11-29 06:37:47 -05:00
|
|
|
file: fileRef
|
|
|
|
path: path.fileSystem
|
|
|
|
url: fileStoreUrl
|
2017-11-30 10:46:37 -05:00
|
|
|
]
|
|
|
|
DocumentUpdaterHandler.updateProjectStructure project_id, userId, {newFiles}, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2017-11-24 06:37:19 -05:00
|
|
|
copyFileFromExistingProjectWithProject: (project, folder_id, originalProject_id, origonalFileRef, userId, callback = (error, fileRef, folder_id) ->)->
|
2016-03-09 06:16:27 -05:00
|
|
|
project_id = project._id
|
2017-11-24 06:14:15 -05:00
|
|
|
logger.log { project_id, folder_id, originalProject_id, origonalFileRef }, "copying file in s3 with project"
|
2016-03-09 06:16:27 -05:00
|
|
|
return callback(err) if err?
|
|
|
|
confirmFolder project, folder_id, (folder_id)=>
|
|
|
|
if !origonalFileRef?
|
2017-11-24 06:14:15 -05:00
|
|
|
logger.err { project_id, folder_id, originalProject_id, origonalFileRef }, "file trying to copy is null"
|
2016-03-09 06:16:27 -05:00
|
|
|
return callback()
|
|
|
|
fileRef = new File name : origonalFileRef.name
|
2017-11-24 06:14:15 -05:00
|
|
|
FileStoreHandler.copyFile originalProject_id, origonalFileRef._id, project._id, fileRef._id, (err, fileStoreUrl)->
|
2016-03-09 06:16:27 -05:00
|
|
|
if err?
|
2017-11-24 06:14:15 -05:00
|
|
|
logger.err { err, project_id, folder_id, originalProject_id, origonalFileRef }, "error coping file in s3"
|
2016-03-09 06:16:27 -05:00
|
|
|
return callback(err)
|
|
|
|
ProjectEntityHandler._putElement project, folder_id, fileRef, "file", (err, result)=>
|
2014-02-12 05:23:40 -05:00
|
|
|
if err?
|
2017-11-24 06:14:15 -05:00
|
|
|
logger.err { err, project_id, folder_id }, "error putting element as part of copy"
|
2016-02-29 17:51:46 -05:00
|
|
|
return callback(err)
|
2017-11-24 06:14:15 -05:00
|
|
|
tpdsUpdateSender.addFile { project_id, file_id:fileRef._id, path:result?.path?.fileSystem, rev:fileRef.rev, project_name:project.name}, (err) ->
|
2016-03-14 06:49:28 -04:00
|
|
|
if err?
|
2017-11-24 06:14:15 -05:00
|
|
|
logger.err { err, project_id, folder_id, originalProject_id, origonalFileRef }, "error sending file to tpds worker"
|
2017-11-30 10:46:37 -05:00
|
|
|
newFiles = [
|
2017-11-24 06:14:15 -05:00
|
|
|
file: fileRef
|
|
|
|
path: result?.path?.fileSystem
|
|
|
|
url: fileStoreUrl
|
2017-11-30 10:46:37 -05:00
|
|
|
]
|
|
|
|
DocumentUpdaterHandler.updateProjectStructure project_id, userId, {newFiles}, (error) ->
|
2017-11-24 06:14:15 -05:00
|
|
|
return callback(error) if error?
|
|
|
|
callback null, fileRef, folder_id
|
2014-02-12 05:23:40 -05:00
|
|
|
|
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]
|
2017-09-28 10:01:45 -04:00
|
|
|
if parentFolder?
|
2014-02-12 05:23:40 -05:00
|
|
|
parentFolder_id = parentFolder._id
|
|
|
|
builtUpPath = "#{builtUpPath}/#{folderName}"
|
2016-02-29 12:34:38 -05:00
|
|
|
projectLocator.findElementByPath project, 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)->
|
2016-08-18 12:48:33 -04:00
|
|
|
return callback(err) if err?
|
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)->
|
2016-08-18 12:48:33 -04:00
|
|
|
return callback(err) if err?
|
2014-02-12 05:23:40 -05:00
|
|
|
lastFolder = folders[folders.length-1]
|
|
|
|
folders = _.select folders, (folder)->
|
|
|
|
!folder.filterOut
|
|
|
|
callback(null, folders, lastFolder)
|
2016-03-09 06:16:27 -05:00
|
|
|
|
|
|
|
addFolder: (project_id, parentFolder_id, folderName, callback) ->
|
2018-01-23 05:21:05 -05:00
|
|
|
ProjectGetter.getProject project_id, {rootFolder:true, name:true}, (err, project)=>
|
2016-03-09 06:16:27 -05:00
|
|
|
if err?
|
|
|
|
logger.err project_id:project_id, err:err, "error getting project for add folder"
|
|
|
|
return callback(err)
|
|
|
|
ProjectEntityHandler.addFolderWithProject project, parentFolder_id, folderName, callback
|
|
|
|
|
|
|
|
addFolderWithProject: (project, parentFolder_id, folderName, callback = (err, folder, parentFolder_id)->) ->
|
|
|
|
confirmFolder project, parentFolder_id, (parentFolder_id)=>
|
|
|
|
folder = new Folder name: folderName
|
|
|
|
logger.log project: project._id, parentFolder_id:parentFolder_id, folderName:folderName, "adding new folder"
|
|
|
|
ProjectEntityHandler._putElement project, parentFolder_id, folder, "folder", (err, result)=>
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project._id, "error adding folder to project"
|
|
|
|
return callback(err)
|
|
|
|
callback(err, folder, parentFolder_id)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-12-08 09:09:06 -05:00
|
|
|
updateDocLines : (project_id, doc_id, lines, version, ranges, 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"
|
2016-12-08 09:09:06 -05:00
|
|
|
DocstoreManager.updateDoc project_id, doc_id, lines, version, ranges, (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
|
2017-04-11 10:41:17 -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
|
|
|
|
2017-11-02 05:44:23 -04:00
|
|
|
moveEntity: (project_id, entity_id, destFolderId, entityType, userId, callback = (error) ->)->
|
2014-02-12 05:23:40 -05:00
|
|
|
self = @
|
2017-11-02 05:44:23 -04:00
|
|
|
logger.log {entityType, entity_id, project_id, destFolderId}, "moving entity"
|
2014-02-12 05:23:40 -05:00
|
|
|
if !entityType?
|
2017-11-02 05:44:23 -04:00
|
|
|
logger.err {err: "No entityType set", project_id, entity_id}
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback("No entityType set")
|
|
|
|
entityType = entityType.toLowerCase()
|
2016-03-08 09:46:53 -05:00
|
|
|
ProjectGetter.getProject project_id, {rootFolder:true, name:true}, (err, project)=>
|
2014-08-22 09:38:41 -04:00
|
|
|
return callback(err) if err?
|
2017-11-02 05:44:23 -04:00
|
|
|
projectLocator.findElement {project, element_id: entity_id, type: entityType}, (err, entity, entityPath)->
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(err) if err?
|
2018-01-18 11:42:26 -05:00
|
|
|
self._checkValidMove project, entityType, entity, entityPath, destFolderId, (error) ->
|
2014-08-22 09:38:41 -04:00
|
|
|
return callback(error) if error?
|
2017-11-07 05:06:12 -05:00
|
|
|
self.getAllEntitiesFromProject project, (error, oldDocs, oldFiles) =>
|
2017-11-02 05:44:23 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
self._removeElementFromMongoArray Project, project_id, entityPath.mongo, (err, newProject)->
|
2014-08-22 09:38:41 -04:00
|
|
|
return callback(err) if err?
|
2017-11-07 05:06:12 -05:00
|
|
|
self._putElement newProject, destFolderId, entity, entityType, (err, result, newProject)->
|
2016-03-15 11:14:54 -04:00
|
|
|
return callback(err) if err?
|
2017-09-28 10:01:45 -04:00
|
|
|
opts =
|
2017-11-02 05:44:23 -04:00
|
|
|
project_id: project_id
|
|
|
|
project_name: project.name
|
|
|
|
startPath: entityPath.fileSystem
|
|
|
|
endPath: result.path.fileSystem,
|
|
|
|
rev: entity.rev
|
|
|
|
tpdsUpdateSender.moveEntity opts
|
2017-11-23 11:40:15 -05:00
|
|
|
self.getAllEntitiesFromProject newProject, (error, newDocs, newFiles) =>
|
2017-11-02 05:44:23 -04:00
|
|
|
return callback(error) if error?
|
2017-11-30 10:46:37 -05:00
|
|
|
DocumentUpdaterHandler.updateProjectStructure project_id, userId, {oldDocs, newDocs, oldFiles, newFiles}, callback
|
2017-11-02 05:44:23 -04:00
|
|
|
|
2018-01-18 11:42:26 -05:00
|
|
|
_checkValidMove: (project, entityType, entity, entityPath, destFolderId, callback = (error) ->) ->
|
2017-11-02 05:44:23 -04:00
|
|
|
projectLocator.findElement { project, element_id: destFolderId, type:"folder"}, (err, destEntity, destFolderPath) ->
|
|
|
|
return callback(err) if err?
|
2018-01-18 11:42:26 -05:00
|
|
|
# check if there is already a doc/file/folder with the same name
|
|
|
|
# in the destination folder
|
2018-01-23 05:35:38 -05:00
|
|
|
ProjectEntityHandler.checkValidElementName destEntity, entity.name, (err)->
|
2018-01-18 11:42:26 -05:00
|
|
|
return callback(err) if err?
|
|
|
|
if entityType.match(/folder/)
|
|
|
|
logger.log destFolderPath: destFolderPath.fileSystem, folderPath: entityPath.fileSystem, "checking folder is not moving into child folder"
|
|
|
|
isNestedFolder = destFolderPath.fileSystem.slice(0, entityPath.fileSystem.length) == entityPath.fileSystem
|
|
|
|
if isNestedFolder
|
|
|
|
return callback(new Error("destination folder is a child folder of me"))
|
2017-11-02 05:44:23 -04:00
|
|
|
callback()
|
|
|
|
|
2017-12-15 05:23:10 -05:00
|
|
|
deleteEntity: (project_id, entity_id, entityType, userId, 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()
|
2016-02-29 11:03:22 -05:00
|
|
|
ProjectGetter.getProject project_id, {name:true, rootFolder:true}, (err, project)=>
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
|
|
|
projectLocator.findElement {project: project, element_id: entity_id, type: entityType}, (error, entity, path)=>
|
|
|
|
return callback(error) if error?
|
2017-12-15 08:17:26 -05:00
|
|
|
ProjectEntityHandler._cleanUpEntity project, entity, entityType, path.fileSystem, userId, (error) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
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
|
|
|
|
2017-11-01 14:21:05 -04:00
|
|
|
renameEntity: (project_id, entity_id, entityType, newName, userId, callback)->
|
2014-04-07 07:18:24 -04:00
|
|
|
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()
|
2017-11-01 14:21:05 -04:00
|
|
|
ProjectGetter.getProject project_id, {rootFolder:true, name:true}, (error, project)=>
|
|
|
|
return callback(error) if error?
|
2017-11-06 10:11:33 -05:00
|
|
|
ProjectEntityHandler.getAllEntitiesFromProject project, (error, oldDocs, oldFiles) =>
|
2017-11-01 14:21:05 -04:00
|
|
|
return callback(error) if error?
|
2018-01-18 11:22:38 -05:00
|
|
|
projectLocator.findElement {project:project, element_id:entity_id, type:entityType}, (error, entity, entPath, parentFolder)=>
|
2017-11-01 14:21:05 -04:00
|
|
|
return callback(error) if error?
|
2018-01-18 11:22:38 -05:00
|
|
|
# check if the new name already exists in the current folder
|
2018-01-23 05:35:38 -05:00
|
|
|
ProjectEntityHandler.checkValidElementName parentFolder, newName, (error) =>
|
2018-01-18 11:22:38 -05:00
|
|
|
return callback(error) if error?
|
2018-01-19 07:02:04 -05:00
|
|
|
endPath = path.join(path.dirname(entPath.fileSystem), newName)
|
|
|
|
conditions = {_id:project_id}
|
|
|
|
update = "$set":{}
|
|
|
|
namePath = entPath.mongo+".name"
|
|
|
|
update["$set"][namePath] = newName
|
|
|
|
tpdsUpdateSender.moveEntity({project_id:project_id, startPath:entPath.fileSystem, endPath:endPath, project_name:project.name, rev:entity.rev})
|
|
|
|
Project.findOneAndUpdate conditions, update, { "new": true}, (error, newProject) ->
|
2017-11-01 14:21:05 -04:00
|
|
|
return callback(error) if error?
|
2018-01-19 07:02:04 -05:00
|
|
|
ProjectEntityHandler.getAllEntitiesFromProject newProject, (error, newDocs, newFiles) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
DocumentUpdaterHandler.updateProjectStructure project_id, userId, {oldDocs, newDocs, oldFiles, newFiles}, callback
|
2014-04-07 07:18:24 -04:00
|
|
|
|
2017-12-15 08:17:26 -05:00
|
|
|
_cleanUpEntity: (project, entity, entityType, path, userId, callback = (error) ->) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
if(entityType.indexOf("file") != -1)
|
2017-12-15 08:17:26 -05:00
|
|
|
ProjectEntityHandler._cleanUpFile project, entity, path, userId, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
else if (entityType.indexOf("doc") != -1)
|
2017-12-15 08:17:26 -05:00
|
|
|
ProjectEntityHandler._cleanUpDoc project, entity, path, userId, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
else if (entityType.indexOf("folder") != -1)
|
2017-12-15 08:17:26 -05:00
|
|
|
ProjectEntityHandler._cleanUpFolder project, entity, path, userId, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
|
|
|
callback()
|
|
|
|
|
2017-12-15 08:17:26 -05:00
|
|
|
_cleanUpDoc: (project, doc, path, userId, 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?
|
2017-11-15 10:07:55 -05:00
|
|
|
DocumentUpdaterHandler.deleteDoc project_id, doc_id, (error) ->
|
2014-04-30 06:15:29 -04:00
|
|
|
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?
|
2017-12-14 11:03:43 -05:00
|
|
|
changes = oldDocs: [ {doc, path} ]
|
2017-12-15 05:14:01 -05:00
|
|
|
DocumentUpdaterHandler.updateProjectStructure project_id, userId, changes, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2017-12-15 08:17:26 -05:00
|
|
|
_cleanUpFile: (project, file, path, userId, callback = (error) ->) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
project_id = project._id.toString()
|
|
|
|
file_id = file._id.toString()
|
2017-12-14 11:03:43 -05:00
|
|
|
FileStoreHandler.deleteFile project_id, file_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
changes = oldFiles: [ {file, path} ]
|
2017-12-15 05:14:01 -05:00
|
|
|
DocumentUpdaterHandler.updateProjectStructure project_id, userId, changes, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2017-12-15 08:17:26 -05:00
|
|
|
_cleanUpFolder: (project, folder, folderPath, userId, callback = (error) ->) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
jobs = []
|
|
|
|
for doc in folder.docs
|
|
|
|
do (doc) ->
|
2017-12-14 11:03:43 -05:00
|
|
|
docPath = path.join(folderPath, doc.name)
|
2017-12-15 08:17:26 -05:00
|
|
|
jobs.push (callback) -> ProjectEntityHandler._cleanUpDoc project, doc, docPath, userId, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
for file in folder.fileRefs
|
|
|
|
do (file) ->
|
2017-12-14 11:03:43 -05:00
|
|
|
filePath = path.join(folderPath, file.name)
|
2017-12-15 08:17:26 -05:00
|
|
|
jobs.push (callback) -> ProjectEntityHandler._cleanUpFile project, file, filePath, userId, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
for childFolder in folder.folders
|
|
|
|
do (childFolder) ->
|
2017-12-14 11:03:43 -05:00
|
|
|
folderPath = path.join(folderPath, childFolder.name)
|
2017-12-15 08:17:26 -05:00
|
|
|
jobs.push (callback) -> ProjectEntityHandler._cleanUpFolder project, childFolder, folderPath, userId, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
async.series jobs, callback
|
|
|
|
|
2017-11-06 07:43:04 -05:00
|
|
|
_removeElementFromMongoArray : (model, model_id, path, callback = (err, project) ->)->
|
|
|
|
conditions = {_id:model_id}
|
2014-02-12 05:23:40 -05:00
|
|
|
update = {"$unset":{}}
|
|
|
|
update["$unset"][path] = 1
|
2017-11-06 07:43:04 -05:00
|
|
|
model.update conditions, update, {}, (err)->
|
2014-02-12 05:23:40 -05:00
|
|
|
pullUpdate = {"$pull":{}}
|
|
|
|
nonArrayPath = path.slice(0, path.lastIndexOf("."))
|
|
|
|
pullUpdate["$pull"][nonArrayPath] = null
|
2017-11-06 07:43:04 -05:00
|
|
|
model.findOneAndUpdate conditions, pullUpdate, {"new": true}, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
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-03-09 06:16:27 -05:00
|
|
|
|
2016-02-26 08:25:09 -05:00
|
|
|
_countElements : (project, callback)->
|
2017-09-28 10:01:45 -04:00
|
|
|
|
2016-02-26 08:25:09 -05:00
|
|
|
countFolder = (folder, cb = (err, count)->)->
|
|
|
|
|
|
|
|
jobs = _.map folder?.folders, (folder)->
|
|
|
|
(asyncCb)-> countFolder folder, asyncCb
|
|
|
|
|
|
|
|
async.series jobs, (err, subfolderCounts)->
|
|
|
|
total = 0
|
|
|
|
|
|
|
|
if subfolderCounts?.length > 0
|
|
|
|
total = _.reduce subfolderCounts, (a, b)-> return a + b
|
2016-02-29 08:05:17 -05:00
|
|
|
if folder?.folders?.length?
|
|
|
|
total += folder?.folders?.length
|
2016-02-26 08:25:09 -05:00
|
|
|
if folder?.docs?.length?
|
|
|
|
total += folder?.docs?.length
|
|
|
|
if folder?.fileRefs?.length?
|
|
|
|
total += folder?.fileRefs?.length
|
|
|
|
cb(null, total)
|
|
|
|
|
|
|
|
countFolder project.rootFolder[0], callback
|
|
|
|
|
2017-11-06 07:43:04 -05:00
|
|
|
_putElement: (project, folder_id, element, type, callback = (err, path, project)->)->
|
2016-02-24 10:36:39 -05:00
|
|
|
sanitizeTypeOfElement = (elementType)->
|
|
|
|
lastChar = elementType.slice -1
|
|
|
|
if lastChar != "s"
|
|
|
|
elementType +="s"
|
|
|
|
if elementType == "files"
|
|
|
|
elementType = "fileRefs"
|
|
|
|
return elementType
|
|
|
|
|
2016-03-15 07:29:59 -04:00
|
|
|
if !element? or !element._id?
|
2016-02-24 10:36:39 -05:00
|
|
|
e = new Error("no element passed to be inserted")
|
2016-03-09 06:16:27 -05:00
|
|
|
logger.err project_id:project._id, folder_id:folder_id, element:element, type:type, "failed trying to insert element as it was null"
|
2016-02-24 10:36:39 -05:00
|
|
|
return callback(e)
|
|
|
|
type = sanitizeTypeOfElement type
|
2016-03-09 06:16:27 -05:00
|
|
|
|
2016-07-19 06:58:10 -04:00
|
|
|
if path.resolve("/", element.name) isnt "/#{element.name}" or element.name.match("/")
|
|
|
|
e = new Error("invalid element name")
|
|
|
|
logger.err project_id:project._id, folder_id:folder_id, element:element, type:type, "failed trying to insert element as name was invalid"
|
|
|
|
return callback(e)
|
|
|
|
|
2016-03-09 06:16:27 -05:00
|
|
|
if !folder_id?
|
|
|
|
folder_id = project.rootFolder[0]._id
|
|
|
|
ProjectEntityHandler._countElements project, (err, count)->
|
2016-03-09 10:53:03 -05:00
|
|
|
if count > settings.maxEntitiesPerProject
|
2016-03-09 06:16:27 -05:00
|
|
|
logger.warn project_id:project._id, "project too big, stopping insertions"
|
2017-05-09 09:20:29 -04:00
|
|
|
CooldownManager.putProjectOnCooldown(project._id)
|
2016-03-09 06:16:27 -05:00
|
|
|
return callback("project_has_to_many_files")
|
|
|
|
projectLocator.findElement {project:project, element_id:folder_id, type:"folders"}, (err, folder, path)=>
|
|
|
|
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)
|
|
|
|
newPath =
|
|
|
|
fileSystem: "#{path.fileSystem}/#{element.name}"
|
|
|
|
mongo: path.mongo
|
2018-01-23 05:35:38 -05:00
|
|
|
ProjectEntityHandler.checkValidElementName folder, element.name, (err) =>
|
2018-01-19 07:01:23 -05:00
|
|
|
return callback(err) if err?
|
|
|
|
id = element._id+''
|
|
|
|
element._id = require('mongoose').Types.ObjectId(id)
|
|
|
|
conditions = _id:project._id
|
|
|
|
mongopath = "#{path.mongo}.#{type}"
|
|
|
|
update = "$push":{}
|
|
|
|
update["$push"][mongopath] = element
|
|
|
|
logger.log project_id: project._id, element_id: element._id, fileType: type, folder_id: folder_id, mongopath:mongopath, "adding element to project"
|
|
|
|
Project.findOneAndUpdate conditions, update, {"new": true}, (err, project)->
|
|
|
|
if err?
|
|
|
|
logger.err err: err, project_id: project._id, 'error saving in putElement project'
|
|
|
|
return callback(err)
|
|
|
|
callback(err, {path:newPath}, project)
|
|
|
|
|
2018-01-23 05:35:38 -05:00
|
|
|
checkValidElementName: (folder, name, callback = (err) ->) ->
|
2018-01-19 07:01:23 -05:00
|
|
|
# check if the name is already taken by a doc, file or
|
|
|
|
# folder. If so, return an error "file already exists".
|
|
|
|
err = new Errors.InvalidNameError("file already exists")
|
|
|
|
for doc in folder?.docs or []
|
|
|
|
return callback(err) if doc.name is name
|
|
|
|
for file in folder?.fileRefs or []
|
|
|
|
return callback(err) if file.name is name
|
|
|
|
for folder in folder?.folders or []
|
|
|
|
return callback(err) if folder.name is name
|
|
|
|
callback()
|
2016-03-08 09:46:53 -05:00
|
|
|
|
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)
|