2014-02-12 05:23:40 -05:00
|
|
|
_ = require('underscore')
|
2018-02-01 10:31:42 -05:00
|
|
|
async = require "async"
|
|
|
|
path = require "path"
|
2014-02-12 05:23:40 -05:00
|
|
|
logger = require('logger-sharelatex')
|
2014-04-30 06:15:29 -04:00
|
|
|
DocstoreManager = require "../Docstore/DocstoreManager"
|
2017-11-15 10:07:55 -05:00
|
|
|
DocumentUpdaterHandler = require('../../Features/DocumentUpdater/DocumentUpdaterHandler')
|
2018-02-01 10:31:42 -05:00
|
|
|
Errors = require '../Errors/Errors'
|
|
|
|
Project = require('../../models/Project').Project
|
|
|
|
ProjectGetter = require "./ProjectGetter"
|
|
|
|
TpdsUpdateSender = require('../ThirdPartyDataStore/TpdsUpdateSender')
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
module.exports = ProjectEntityHandler = self =
|
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
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
self._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"
|
2018-02-01 10:31:42 -05:00
|
|
|
self._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
|
|
|
|
|
2018-10-23 09:05:50 -04:00
|
|
|
getAllEntities: (project_id, callback) ->
|
|
|
|
ProjectGetter.getProject project_id, (err, project) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
self.getAllEntitiesFromProject project, callback
|
|
|
|
|
2017-08-16 05:49:29 -04:00
|
|
|
getAllEntitiesFromProject: (project, callback) ->
|
2018-03-07 11:08:58 -05:00
|
|
|
logger.log project:project, "getting all entities for project"
|
2018-02-01 10:31:42 -05:00
|
|
|
self._getAllFoldersFromProject project, (err, folders = {}) ->
|
2017-08-16 05:49:29 -04:00
|
|
|
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
|
|
|
|
|
2018-09-24 11:04:23 -04:00
|
|
|
getAllDocPathsFromProjectById: (project_id, callback) ->
|
|
|
|
ProjectGetter.getProjectWithoutDocLines project_id, (err, project) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
return callback(Errors.NotFoundError("no project")) if !project?
|
|
|
|
self.getAllDocPathsFromProject project, callback
|
|
|
|
|
2017-08-01 09:39:02 -04:00
|
|
|
getAllDocPathsFromProject: (project, callback) ->
|
|
|
|
logger.log project:project, "getting all docs for project"
|
2018-02-01 10:31:42 -05:00
|
|
|
self._getAllFoldersFromProject project, (err, folders = {}) ->
|
2017-08-01 09:39:02 -04:00
|
|
|
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) ->
|
|
|
|
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) ->
|
2018-02-01 10:31:42 -05:00
|
|
|
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) ->
|
2018-02-01 10:31:42 -05:00
|
|
|
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-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
|
|
|
|
2018-04-06 09:50:56 -04:00
|
|
|
DocstoreManager.getDoc project_id, doc_id, options, callback
|
2016-03-09 06:16:27 -05:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
_getAllFolders: (project_id, callback) ->
|
|
|
|
logger.log project_id:project_id, "getting all folders for project"
|
|
|
|
ProjectGetter.getProjectWithoutDocLines project_id, (err, project) ->
|
2017-11-02 05:44:23 -04:00
|
|
|
return callback(err) if err?
|
2018-02-01 10:31:42 -05:00
|
|
|
return callback(Errors.NotFoundError("no project")) if !project?
|
|
|
|
self._getAllFoldersFromProject project, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-02-01 10:31:42 -05: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
|
2016-03-08 09:46:53 -05:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
processFolder "/", project.rootFolder[0]
|
|
|
|
callback null, folders
|