2019-05-29 05:21:06 -04:00
|
|
|
const _ = require('underscore')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const async = require('async')
|
2021-02-17 08:00:48 -05:00
|
|
|
const ProjectGetter = require('./ProjectGetter')
|
|
|
|
const Errors = require('../Errors/Errors')
|
|
|
|
const { promisifyMultiResult } = require('../../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
function findElement(options, _callback) {
|
|
|
|
// The search algorithm below potentially invokes the callback multiple
|
|
|
|
// times.
|
|
|
|
const callback = _.once(_callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
const {
|
|
|
|
project,
|
|
|
|
project_id: projectId,
|
|
|
|
element_id: elementId,
|
|
|
|
type
|
|
|
|
} = options
|
|
|
|
const elementType = sanitizeTypeOfElement(type)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
let count = 0
|
|
|
|
const endOfBranch = function() {
|
|
|
|
if (--count === 0) {
|
|
|
|
logger.warn(
|
|
|
|
`element ${elementId} could not be found for project ${projectId ||
|
|
|
|
project._id}`
|
|
|
|
)
|
|
|
|
callback(new Errors.NotFoundError('entity not found'))
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
function search(searchFolder, path) {
|
|
|
|
count++
|
|
|
|
const element = _.find(
|
|
|
|
searchFolder[elementType],
|
|
|
|
el => (el != null ? el._id : undefined) + '' === elementId + ''
|
|
|
|
) // need to ToString both id's for robustness
|
|
|
|
if (
|
|
|
|
element == null &&
|
|
|
|
searchFolder.folders != null &&
|
|
|
|
searchFolder.folders.length !== 0
|
|
|
|
) {
|
|
|
|
_.each(searchFolder.folders, (folder, index) => {
|
|
|
|
if (folder == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const newPath = {}
|
|
|
|
for (let key of Object.keys(path)) {
|
|
|
|
const value = path[key]
|
|
|
|
newPath[key] = value
|
|
|
|
} // make a value copy of the string
|
|
|
|
newPath.fileSystem += `/${folder.name}`
|
|
|
|
newPath.mongo += `.folders.${index}`
|
|
|
|
search(folder, newPath)
|
|
|
|
})
|
|
|
|
endOfBranch()
|
|
|
|
} else if (element != null) {
|
|
|
|
const elementPlaceInArray = getIndexOf(
|
2019-05-29 05:21:06 -04:00
|
|
|
searchFolder[elementType],
|
2021-02-17 08:00:48 -05:00
|
|
|
elementId
|
|
|
|
)
|
|
|
|
path.fileSystem += `/${element.name}`
|
|
|
|
path.mongo += `.${elementType}.${elementPlaceInArray}`
|
|
|
|
callback(null, element, path, searchFolder)
|
|
|
|
} else if (element == null) {
|
|
|
|
endOfBranch()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
const path = { fileSystem: '', mongo: 'rootFolder.0' }
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
const startSearch = project => {
|
|
|
|
if (elementId + '' === project.rootFolder[0]._id + '') {
|
|
|
|
callback(null, project.rootFolder[0], path, null)
|
|
|
|
} else {
|
|
|
|
search(project.rootFolder[0], path)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
if (project != null) {
|
|
|
|
startSearch(project)
|
|
|
|
} else {
|
|
|
|
ProjectGetter.getProject(
|
|
|
|
projectId,
|
|
|
|
{ rootFolder: true, rootDoc_id: true },
|
|
|
|
(err, project) => {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
if (project == null) {
|
|
|
|
return callback(new Errors.NotFoundError('project not found'))
|
|
|
|
}
|
|
|
|
startSearch(project)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
function findRootDoc(opts, callback) {
|
|
|
|
const getRootDoc = project => {
|
|
|
|
if (project.rootDoc_id != null) {
|
|
|
|
findElement(
|
|
|
|
{ project, element_id: project.rootDoc_id, type: 'docs' },
|
|
|
|
(error, ...args) => {
|
|
|
|
if (error != null) {
|
|
|
|
if (error instanceof Errors.NotFoundError) {
|
|
|
|
return callback(null, null)
|
|
|
|
} else {
|
|
|
|
return callback(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
callback(null, ...args)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
2021-02-17 08:00:48 -05:00
|
|
|
} else {
|
|
|
|
callback(null, null)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
}
|
|
|
|
const { project, project_id: projectId } = opts
|
|
|
|
if (project != null) {
|
|
|
|
getRootDoc(project)
|
|
|
|
} else {
|
|
|
|
ProjectGetter.getProject(
|
|
|
|
projectId,
|
|
|
|
{ rootFolder: true, rootDoc_id: true },
|
|
|
|
(err, project) => {
|
|
|
|
if (err != null) {
|
|
|
|
logger.warn({ err }, 'error getting project')
|
|
|
|
callback(err)
|
|
|
|
} else {
|
|
|
|
getRootDoc(project)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
function findElementByPath(options, callback) {
|
|
|
|
const { project, project_id: projectId, path, exactCaseMatch } = options
|
|
|
|
if (path == null) {
|
|
|
|
return new Error('no path provided for findElementByPath')
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
if (project != null) {
|
|
|
|
_findElementByPathWithProject(project, path, exactCaseMatch, callback)
|
|
|
|
} else {
|
|
|
|
ProjectGetter.getProject(
|
|
|
|
projectId,
|
|
|
|
{ rootFolder: true, rootDoc_id: true },
|
|
|
|
(err, project) => {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
_findElementByPathWithProject(project, path, exactCaseMatch, callback)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
function _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-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
function getParentFolder(haystackFolder, foldersList, level, cb) {
|
|
|
|
if (foldersList.length === 0) {
|
|
|
|
return cb(null, haystackFolder)
|
|
|
|
}
|
|
|
|
const needleFolderName = foldersList[level]
|
|
|
|
let found = false
|
|
|
|
for (let folder of haystackFolder.folders) {
|
|
|
|
if (matchFn(folder.name, needleFolderName)) {
|
|
|
|
found = true
|
|
|
|
if (level === foldersList.length - 1) {
|
|
|
|
return cb(null, folder)
|
|
|
|
} else {
|
|
|
|
return getParentFolder(folder, foldersList, level + 1, cb)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
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
|
|
|
)
|
2021-02-17 08:00:48 -05:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
function getEntity(folder, entityName, cb) {
|
|
|
|
let result, type
|
|
|
|
if (entityName == null) {
|
|
|
|
return cb(null, folder, 'folder')
|
|
|
|
}
|
|
|
|
for (let file of folder.fileRefs || []) {
|
|
|
|
if (matchFn(file != null ? file.name : undefined, entityName)) {
|
|
|
|
result = file
|
|
|
|
type = 'file'
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
for (let doc of folder.docs || []) {
|
|
|
|
if (matchFn(doc != null ? doc.name : undefined, entityName)) {
|
|
|
|
result = doc
|
|
|
|
type = 'doc'
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
for (let childFolder of folder.folders || []) {
|
|
|
|
if (
|
|
|
|
matchFn(childFolder != null ? childFolder.name : undefined, entityName)
|
|
|
|
) {
|
|
|
|
result = childFolder
|
|
|
|
type = 'folder'
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
if (result != null) {
|
|
|
|
cb(null, result, type)
|
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
if (project == null) {
|
|
|
|
return callback(new Error('Tried to find an element for a null project'))
|
|
|
|
}
|
|
|
|
if (needlePath === '' || needlePath === '/') {
|
|
|
|
return callback(null, project.rootFolder[0], 'folder')
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
if (needlePath.indexOf('/') === 0) {
|
|
|
|
needlePath = needlePath.substring(1)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
const foldersList = needlePath.split('/')
|
|
|
|
const needleName = foldersList.pop()
|
|
|
|
const rootFolder = project.rootFolder[0]
|
|
|
|
|
|
|
|
const jobs = []
|
|
|
|
jobs.push(cb => getParentFolder(rootFolder, foldersList, 0, cb))
|
|
|
|
jobs.push((folder, cb) => getEntity(folder, needleName, cb))
|
|
|
|
async.waterfall(jobs, callback)
|
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
|
|
|
|
2021-02-17 08:00:48 -05:00
|
|
|
module.exports = {
|
|
|
|
findElement,
|
|
|
|
findElementByPath,
|
|
|
|
findRootDoc,
|
|
|
|
promises: {
|
|
|
|
findElement: promisifyMultiResult(findElement, [
|
|
|
|
'element',
|
|
|
|
'path',
|
|
|
|
'folder'
|
|
|
|
]),
|
|
|
|
findElementByPath: promisifyMultiResult(findElementByPath, [
|
|
|
|
'element',
|
|
|
|
'type'
|
|
|
|
]),
|
|
|
|
findRootDoc: promisifyMultiResult(findRootDoc, [
|
|
|
|
'element',
|
|
|
|
'path',
|
|
|
|
'folder'
|
|
|
|
])
|
2019-11-12 03:56:58 -05:00
|
|
|
}
|
2021-02-17 08:00:48 -05:00
|
|
|
}
|