2014-02-12 05:23:40 -05:00
|
|
|
Project = require('../../models/Project').Project
|
2016-02-26 03:31:47 -05:00
|
|
|
ProjectGetter = require("./ProjectGetter")
|
2016-03-23 11:14:49 -04:00
|
|
|
Errors = require "../Errors/Errors"
|
2014-02-12 05:23:40 -05:00
|
|
|
_ = require('underscore')
|
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
async = require('async')
|
|
|
|
|
2016-02-26 03:35:10 -05:00
|
|
|
module.exports = ProjectLocator =
|
2016-02-05 10:39:26 -05:00
|
|
|
findElement: (options, _callback = (err, element, path, parentFolder)->)->
|
|
|
|
callback = (args...) ->
|
|
|
|
_callback(args...)
|
|
|
|
_callback = () ->
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
{project, project_id, element_id, type} = options
|
|
|
|
elementType = sanitizeTypeOfElement type
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
endOfBranch = ->
|
|
|
|
if --count == 0
|
|
|
|
logger.warn "element #{element_id} could not be found for project #{project_id || project._id}"
|
|
|
|
return callback(new Errors.NotFoundError("entity not found"))
|
|
|
|
|
|
|
|
search = (searchFolder, path)->
|
|
|
|
count++
|
2014-03-12 18:38:03 -04:00
|
|
|
element = _.find searchFolder[elementType], (el)-> el?._id+'' == element_id+'' #need to ToString both id's for robustness
|
2014-02-12 05:23:40 -05:00
|
|
|
if !element? && searchFolder.folders? && searchFolder.folders.length != 0
|
|
|
|
_.each searchFolder.folders, (folder, index)->
|
2017-03-17 09:29:09 -04:00
|
|
|
if !folder?
|
|
|
|
return
|
2014-02-12 05:23:40 -05:00
|
|
|
newPath = {}
|
|
|
|
newPath[key] = value for own key,value of path #make a value copy of the string
|
|
|
|
newPath.fileSystem += "/#{folder.name}"
|
|
|
|
newPath.mongo += ".folders.#{index}"
|
|
|
|
search folder, newPath
|
|
|
|
endOfBranch()
|
|
|
|
return
|
|
|
|
else if element?
|
|
|
|
elementPlaceInArray = getIndexOf(searchFolder[elementType], element_id)
|
|
|
|
path.fileSystem += "/#{element.name}"
|
|
|
|
path.mongo +=".#{elementType}.#{elementPlaceInArray}"
|
|
|
|
callback(null, element, path, searchFolder)
|
|
|
|
else if !element?
|
|
|
|
return endOfBranch()
|
|
|
|
|
|
|
|
path = {fileSystem:'',mongo:'rootFolder.0'}
|
|
|
|
|
|
|
|
startSearch = (project)->
|
|
|
|
if element_id+'' == project.rootFolder[0]._id+''
|
|
|
|
callback(null, project.rootFolder[0], path, null)
|
|
|
|
else
|
|
|
|
search project.rootFolder[0], path
|
|
|
|
|
|
|
|
if project?
|
|
|
|
startSearch(project)
|
|
|
|
else
|
2016-02-29 11:03:22 -05:00
|
|
|
ProjectGetter.getProject project_id, {rootFolder:true, rootDoc_id:true}, (err, project)->
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(err) if err?
|
|
|
|
if !project?
|
|
|
|
return callback(new Errors.NotFoundError("project not found"))
|
|
|
|
startSearch project
|
|
|
|
|
|
|
|
findRootDoc : (opts, callback)->
|
|
|
|
getRootDoc = (project)=>
|
2015-09-01 12:25:13 -04:00
|
|
|
if project.rootDoc_id?
|
2016-05-23 09:58:28 -04:00
|
|
|
@findElement {project:project, element_id:project.rootDoc_id, type:"docs"}, (error, args...) ->
|
|
|
|
if error?
|
|
|
|
if error instanceof Errors.NotFoundError
|
|
|
|
return callback null, null
|
|
|
|
else
|
|
|
|
return callback error
|
|
|
|
return callback null, args...
|
2015-09-01 12:25:13 -04:00
|
|
|
else
|
|
|
|
callback null, null
|
2014-02-12 05:23:40 -05:00
|
|
|
{project, project_id} = opts
|
|
|
|
if project?
|
|
|
|
getRootDoc project
|
|
|
|
else
|
2016-02-29 11:03:22 -05:00
|
|
|
ProjectGetter.getProject project_id, {rootFolder:true, rootDoc_id:true}, (err, project)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, "error getting project"
|
|
|
|
return callback(err)
|
|
|
|
else
|
|
|
|
getRootDoc project
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-02-12 11:03:11 -05:00
|
|
|
findElementByPath: (options, callback = (err, foundEntity, type)->)->
|
|
|
|
{project, project_id, path} = options
|
|
|
|
if !path?
|
|
|
|
return new Error('no path provided for findElementByPath')
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-02-12 11:03:11 -05:00
|
|
|
if project?
|
|
|
|
ProjectLocator._findElementByPathWithProject project, path, callback
|
|
|
|
else
|
|
|
|
ProjectGetter.getProject project_id, {rootFolder:true, rootDoc_id:true}, (err, project)->
|
|
|
|
return callback(err) if err?
|
|
|
|
ProjectLocator._findElementByPathWithProject project, path, callback
|
|
|
|
|
|
|
|
_findElementByPathWithProject: (project, needlePath, callback = (err, foundEntity, type)->)->
|
2014-02-12 05:23:40 -05:00
|
|
|
getParentFolder = (haystackFolder, foldersList, level, cb)->
|
|
|
|
if foldersList.length == 0
|
|
|
|
return cb null, haystackFolder
|
|
|
|
needleFolderName = foldersList[level]
|
|
|
|
found = false
|
2014-11-11 09:44:44 -05:00
|
|
|
for folder in haystackFolder.folders
|
2014-02-12 05:23:40 -05:00
|
|
|
if folder.name.toLowerCase() == needleFolderName.toLowerCase()
|
|
|
|
found = true
|
|
|
|
if level == foldersList.length-1
|
2014-11-11 09:44:44 -05:00
|
|
|
return cb null, folder
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
2014-11-11 09:44:44 -05:00
|
|
|
return getParentFolder(folder, foldersList, level+1, cb)
|
2014-02-12 05:23:40 -05:00
|
|
|
if !found
|
2018-02-12 11:03:11 -05:00
|
|
|
cb("not found project: #{project._id} search path: #{needlePath}, folder #{foldersList[level]} could not be found")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
getEntity = (folder, entityName, cb)->
|
|
|
|
if !entityName?
|
2016-09-19 09:32:58 -04:00
|
|
|
return cb null, folder, "folder"
|
2016-09-20 11:55:12 -04:00
|
|
|
for file in folder.fileRefs or []
|
2016-09-19 09:32:58 -04:00
|
|
|
if file?.name.toLowerCase() == entityName.toLowerCase()
|
|
|
|
result = file
|
|
|
|
type = "file"
|
2016-09-20 11:55:12 -04:00
|
|
|
for doc in folder.docs or []
|
2016-09-19 09:32:58 -04:00
|
|
|
if doc?.name.toLowerCase() == entityName.toLowerCase()
|
|
|
|
result = doc
|
|
|
|
type = "doc"
|
2016-09-20 11:55:12 -04:00
|
|
|
for childFolder in folder.folders or []
|
2016-09-19 09:32:58 -04:00
|
|
|
if childFolder?.name.toLowerCase() == entityName.toLowerCase()
|
|
|
|
result = childFolder
|
|
|
|
type = "folder"
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
if result?
|
2016-09-19 09:32:58 -04:00
|
|
|
cb null, result, type
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
2018-02-12 11:03:11 -05:00
|
|
|
cb("not found project: #{project._id} search path: #{needlePath}, entity #{entityName} could not be found")
|
|
|
|
|
|
|
|
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project._id, "error getting project for finding element"
|
|
|
|
return callback(err)
|
|
|
|
if !project?
|
|
|
|
return callback("project could not be found for finding a element #{project._id}")
|
|
|
|
if needlePath == '' || needlePath == '/'
|
|
|
|
return callback(null, project.rootFolder[0], "folder")
|
|
|
|
|
|
|
|
if needlePath.indexOf('/') == 0
|
|
|
|
needlePath = needlePath.substring(1)
|
|
|
|
foldersList = needlePath.split('/')
|
|
|
|
needleName = foldersList.pop()
|
|
|
|
rootFolder = project.rootFolder[0]
|
|
|
|
|
|
|
|
logger.log project_id:project._id, path:needlePath, foldersList:foldersList, "looking for element by path"
|
|
|
|
jobs = new Array()
|
|
|
|
jobs.push(
|
|
|
|
(cb)->
|
|
|
|
getParentFolder rootFolder, foldersList, 0, cb
|
|
|
|
)
|
|
|
|
jobs.push(
|
|
|
|
(folder, cb)->
|
|
|
|
getEntity folder, needleName, cb
|
|
|
|
)
|
|
|
|
async.waterfall jobs, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
findUsersProjectByName: (user_id, projectName, callback)->
|
2017-10-20 06:49:20 -04:00
|
|
|
ProjectGetter.findAllUsersProjects user_id, 'name archived', (err, allProjects)->
|
2016-03-03 12:19:03 -05:00
|
|
|
return callback(error) if error?
|
2017-10-20 06:49:20 -04:00
|
|
|
{owned, readAndWrite} = allProjects
|
|
|
|
projects = owned.concat(readAndWrite)
|
2014-02-12 05:23:40 -05:00
|
|
|
projectName = projectName.toLowerCase()
|
2015-12-10 12:06:00 -05:00
|
|
|
project = _.find projects, (project)->
|
|
|
|
project.name.toLowerCase() == projectName and project.archived != true
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.log user_id:user_id, projectName:projectName, totalProjects:projects.length, project:project, "looking for project by name"
|
|
|
|
callback(null, project)
|
|
|
|
|
|
|
|
|
|
|
|
sanitizeTypeOfElement = (elementType)->
|
|
|
|
lastChar = elementType.slice -1
|
|
|
|
if lastChar != "s"
|
|
|
|
elementType +="s"
|
|
|
|
if elementType == "files"
|
|
|
|
elementType = "fileRefs"
|
|
|
|
return elementType
|
|
|
|
|
|
|
|
|
|
|
|
getIndexOf = (searchEntity, id)->
|
|
|
|
length = searchEntity.length
|
|
|
|
count = 0
|
|
|
|
while(count < length)
|
2014-03-12 18:38:03 -04:00
|
|
|
if searchEntity[count]?._id+"" == id+""
|
2014-02-12 05:23:40 -05:00
|
|
|
return count
|
|
|
|
count++
|