2019-05-29 05:21:06 -04:00
|
|
|
const ProjectGetter = require('./ProjectGetter')
|
2019-08-09 05:40:11 -04:00
|
|
|
const ProjectHelper = require('./ProjectHelper')
|
2019-05-29 05:21:06 -04:00
|
|
|
const Errors = require('../Errors/Errors')
|
|
|
|
const _ = require('underscore')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const async = require('async')
|
2019-11-12 03:56:58 -05:00
|
|
|
const { promisifyAll } = require('../../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
const ProjectLocator = {
|
2019-05-29 05:21:06 -04:00
|
|
|
findElement(options, _callback) {
|
2019-11-04 04:50:15 -05:00
|
|
|
// The search algorithm below potentially invokes the callback multiple
|
|
|
|
// times.
|
|
|
|
const callback = _.once(_callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
const {
|
|
|
|
project,
|
|
|
|
project_id: projectId,
|
|
|
|
element_id: elementId,
|
|
|
|
type
|
|
|
|
} = options
|
2019-05-29 05:21:06 -04:00
|
|
|
const elementType = sanitizeTypeOfElement(type)
|
|
|
|
|
|
|
|
let count = 0
|
|
|
|
const endOfBranch = function() {
|
|
|
|
if (--count === 0) {
|
|
|
|
logger.warn(
|
2019-11-04 04:50:15 -05:00
|
|
|
`element ${elementId} could not be found for project ${projectId ||
|
2019-05-29 05:21:06 -04:00
|
|
|
project._id}`
|
|
|
|
)
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(new Errors.NotFoundError('entity not found'))
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
function search(searchFolder, path) {
|
2019-05-29 05:21:06 -04:00
|
|
|
count++
|
|
|
|
const element = _.find(
|
|
|
|
searchFolder[elementType],
|
2019-11-04 04:50:15 -05:00
|
|
|
el => (el != null ? el._id : undefined) + '' === elementId + ''
|
2019-05-29 05:21:06 -04:00
|
|
|
) // need to ToString both id's for robustness
|
|
|
|
if (
|
|
|
|
element == null &&
|
|
|
|
searchFolder.folders != null &&
|
|
|
|
searchFolder.folders.length !== 0
|
|
|
|
) {
|
2019-11-04 04:50:15 -05:00
|
|
|
_.each(searchFolder.folders, (folder, index) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (folder == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const newPath = {}
|
2019-11-04 04:50:15 -05:00
|
|
|
for (let key of Object.keys(path)) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const value = path[key]
|
|
|
|
newPath[key] = value
|
|
|
|
} // make a value copy of the string
|
|
|
|
newPath.fileSystem += `/${folder.name}`
|
|
|
|
newPath.mongo += `.folders.${index}`
|
2019-11-04 04:50:15 -05:00
|
|
|
search(folder, newPath)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
endOfBranch()
|
|
|
|
} else if (element != null) {
|
|
|
|
const elementPlaceInArray = getIndexOf(
|
|
|
|
searchFolder[elementType],
|
2019-11-04 04:50:15 -05:00
|
|
|
elementId
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
path.fileSystem += `/${element.name}`
|
|
|
|
path.mongo += `.${elementType}.${elementPlaceInArray}`
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(null, element, path, searchFolder)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else if (element == null) {
|
2019-11-04 04:50:15 -05:00
|
|
|
endOfBranch()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const path = { fileSystem: '', mongo: 'rootFolder.0' }
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
const startSearch = project => {
|
|
|
|
if (elementId + '' === project.rootFolder[0]._id + '') {
|
|
|
|
callback(null, project.rootFolder[0], path, null)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2019-11-04 04:50:15 -05:00
|
|
|
search(project.rootFolder[0], path)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (project != null) {
|
2019-11-04 04:50:15 -05:00
|
|
|
startSearch(project)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2019-11-04 04:50:15 -05:00
|
|
|
ProjectGetter.getProject(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
{ rootFolder: true, rootDoc_id: true },
|
2019-11-04 04:50:15 -05:00
|
|
|
(err, project) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (project == null) {
|
|
|
|
return callback(new Errors.NotFoundError('project not found'))
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
startSearch(project)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
findRootDoc(opts, callback) {
|
|
|
|
const getRootDoc = project => {
|
|
|
|
if (project.rootDoc_id != null) {
|
2020-05-01 10:00:34 -04:00
|
|
|
ProjectLocator.findElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project, element_id: project.rootDoc_id, type: 'docs' },
|
2019-11-04 04:50:15 -05:00
|
|
|
(error, ...args) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
if (error instanceof Errors.NotFoundError) {
|
|
|
|
return callback(null, null)
|
|
|
|
} else {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(null, ...args)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(null, null)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
const { project, project_id: projectId } = opts
|
2019-05-29 05:21:06 -04:00
|
|
|
if (project != null) {
|
2019-11-04 04:50:15 -05:00
|
|
|
getRootDoc(project)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2019-11-04 04:50:15 -05:00
|
|
|
ProjectGetter.getProject(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
{ rootFolder: true, rootDoc_id: true },
|
2019-11-04 04:50:15 -05:00
|
|
|
(err, project) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn({ err }, 'error getting project')
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2019-11-04 04:50:15 -05:00
|
|
|
getRootDoc(project)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
findElementByPath(options, callback) {
|
2019-11-04 04:50:15 -05:00
|
|
|
const { project, project_id: projectId, path, exactCaseMatch } = options
|
2019-05-29 05:21:06 -04:00
|
|
|
if (path == null) {
|
|
|
|
return new Error('no path provided for findElementByPath')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (project != null) {
|
2019-11-04 04:50:15 -05:00
|
|
|
ProjectLocator._findElementByPathWithProject(
|
2019-05-29 05:21:06 -04:00
|
|
|
project,
|
|
|
|
path,
|
|
|
|
exactCaseMatch,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
} else {
|
2019-11-04 04:50:15 -05:00
|
|
|
ProjectGetter.getProject(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
{ rootFolder: true, rootDoc_id: true },
|
2019-11-04 04:50:15 -05:00
|
|
|
(err, project) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
ProjectLocator._findElementByPathWithProject(
|
2019-05-29 05:21:06 -04:00
|
|
|
project,
|
|
|
|
path,
|
|
|
|
exactCaseMatch,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_findElementByPathWithProject(project, needlePath, exactCaseMatch, callback) {
|
|
|
|
let matchFn
|
|
|
|
if (exactCaseMatch) {
|
|
|
|
matchFn = (a, b) => a === b
|
|
|
|
} else {
|
|
|
|
matchFn = (a, b) =>
|
|
|
|
(a != null ? a.toLowerCase() : undefined) ===
|
|
|
|
(b != null ? b.toLowerCase() : undefined)
|
|
|
|
}
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
function getParentFolder(haystackFolder, foldersList, level, cb) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (foldersList.length === 0) {
|
|
|
|
return cb(null, haystackFolder)
|
|
|
|
}
|
|
|
|
const needleFolderName = foldersList[level]
|
|
|
|
let found = false
|
2019-11-04 04:50:15 -05:00
|
|
|
for (let folder of haystackFolder.folders) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (matchFn(folder.name, needleFolderName)) {
|
|
|
|
found = true
|
|
|
|
if (level === foldersList.length - 1) {
|
|
|
|
return cb(null, folder)
|
|
|
|
} else {
|
|
|
|
return getParentFolder(folder, foldersList, level + 1, cb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2019-11-04 04:50:15 -05:00
|
|
|
cb(
|
|
|
|
new Error(
|
|
|
|
`not found project: ${
|
|
|
|
project._id
|
|
|
|
} search path: ${needlePath}, folder ${
|
|
|
|
foldersList[level]
|
|
|
|
} could not be found`
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
function getEntity(folder, entityName, cb) {
|
2019-05-29 05:21:06 -04:00
|
|
|
let result, type
|
|
|
|
if (entityName == null) {
|
|
|
|
return cb(null, folder, 'folder')
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
for (let file of folder.fileRefs || []) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (matchFn(file != null ? file.name : undefined, entityName)) {
|
|
|
|
result = file
|
|
|
|
type = 'file'
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
for (let doc of folder.docs || []) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (matchFn(doc != null ? doc.name : undefined, entityName)) {
|
|
|
|
result = doc
|
|
|
|
type = 'doc'
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
for (let childFolder of folder.folders || []) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (
|
|
|
|
matchFn(
|
|
|
|
childFolder != null ? childFolder.name : undefined,
|
|
|
|
entityName
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
result = childFolder
|
|
|
|
type = 'folder'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result != null) {
|
2019-11-04 04:50:15 -05:00
|
|
|
cb(null, result, type)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2019-11-04 04:50:15 -05:00
|
|
|
cb(
|
|
|
|
new Error(
|
|
|
|
`not found project: ${
|
|
|
|
project._id
|
|
|
|
} search path: ${needlePath}, entity ${entityName} could not be found`
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (project == null) {
|
2019-11-04 04:50:15 -05:00
|
|
|
return callback(new Error('Tried to find an element for a null project'))
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
if (needlePath === '' || needlePath === '/') {
|
|
|
|
return callback(null, project.rootFolder[0], 'folder')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needlePath.indexOf('/') === 0) {
|
|
|
|
needlePath = needlePath.substring(1)
|
|
|
|
}
|
|
|
|
const foldersList = needlePath.split('/')
|
|
|
|
const needleName = foldersList.pop()
|
|
|
|
const rootFolder = project.rootFolder[0]
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
const jobs = []
|
2019-05-29 05:21:06 -04:00
|
|
|
jobs.push(cb => getParentFolder(rootFolder, foldersList, 0, cb))
|
|
|
|
jobs.push((folder, cb) => getEntity(folder, needleName, cb))
|
2019-11-04 04:50:15 -05:00
|
|
|
async.waterfall(jobs, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
findUsersProjectByName(userId, projectName, callback) {
|
|
|
|
ProjectGetter.findAllUsersProjects(
|
|
|
|
userId,
|
2019-08-27 06:38:17 -04:00
|
|
|
'name archived trashed',
|
2019-11-04 04:50:15 -05:00
|
|
|
(err, allProjects) => {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const { owned, readAndWrite } = allProjects
|
|
|
|
const projects = owned.concat(readAndWrite)
|
|
|
|
projectName = projectName.toLowerCase()
|
2020-08-24 08:21:14 -04:00
|
|
|
const _findNonArchivedProject = () =>
|
|
|
|
_.find(
|
|
|
|
projects,
|
|
|
|
project =>
|
|
|
|
project.name.toLowerCase() === projectName &&
|
|
|
|
!ProjectHelper.isArchivedOrTrashed(project, userId)
|
|
|
|
)
|
|
|
|
const _findArchivedProject = () =>
|
|
|
|
_.find(
|
|
|
|
projects,
|
|
|
|
project =>
|
|
|
|
project.name.toLowerCase() === projectName &&
|
|
|
|
ProjectHelper.isArchivedOrTrashed(project, userId)
|
|
|
|
)
|
|
|
|
const nonArchivedProject = _findNonArchivedProject()
|
|
|
|
if (nonArchivedProject) {
|
|
|
|
return callback(null, {
|
|
|
|
project: nonArchivedProject,
|
|
|
|
isArchivedOrTrashed: false
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
const archivedProject = _findArchivedProject()
|
|
|
|
if (archivedProject) {
|
|
|
|
return callback(null, {
|
|
|
|
project: archivedProject,
|
|
|
|
isArchivedOrTrashed: true
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return callback(null, { project: null })
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
function sanitizeTypeOfElement(elementType) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const lastChar = elementType.slice(-1)
|
|
|
|
if (lastChar !== 's') {
|
|
|
|
elementType += 's'
|
|
|
|
}
|
|
|
|
if (elementType === 'files') {
|
|
|
|
elementType = 'fileRefs'
|
|
|
|
}
|
|
|
|
return elementType
|
|
|
|
}
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
function getIndexOf(searchEntity, id) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const { length } = searchEntity
|
|
|
|
let count = 0
|
|
|
|
while (count < length) {
|
|
|
|
if (
|
|
|
|
(searchEntity[count] != null ? searchEntity[count]._id : undefined) +
|
|
|
|
'' ===
|
|
|
|
id + ''
|
|
|
|
) {
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
|
|
|
|
module.exports = ProjectLocator
|
2019-11-12 03:56:58 -05:00
|
|
|
module.exports.promises = promisifyAll(ProjectLocator, {
|
|
|
|
multiResult: {
|
|
|
|
findElement: ['element', 'path', 'folder'],
|
2020-05-01 10:00:34 -04:00
|
|
|
findElementByPath: ['element', 'type'],
|
|
|
|
findRootDoc: ['element', 'path', 'folder']
|
2019-11-12 03:56:58 -05:00
|
|
|
}
|
|
|
|
})
|