2019-11-21 07:44:39 -05:00
|
|
|
const { callbackify } = require('util')
|
|
|
|
const { callbackifyMultiResult } = require('../../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
const _ = require('underscore')
|
2021-11-10 08:40:18 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = require('path')
|
2019-11-21 07:44:39 -05:00
|
|
|
const { ObjectId } = require('mongodb')
|
2021-07-07 05:38:56 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2019-11-21 07:44:39 -05:00
|
|
|
const OError = require('@overleaf/o-error')
|
2019-05-29 05:21:06 -04:00
|
|
|
const CooldownManager = require('../Cooldown/CooldownManager')
|
|
|
|
const Errors = require('../Errors/Errors')
|
|
|
|
const { Folder } = require('../../models/Folder')
|
|
|
|
const LockManager = require('../../infrastructure/LockManager')
|
|
|
|
const { Project } = require('../../models/Project')
|
|
|
|
const ProjectEntityHandler = require('./ProjectEntityHandler')
|
|
|
|
const ProjectGetter = require('./ProjectGetter')
|
|
|
|
const ProjectLocator = require('./ProjectLocator')
|
2019-11-21 07:44:39 -05:00
|
|
|
const FolderStructureBuilder = require('./FolderStructureBuilder')
|
2019-05-29 05:21:06 -04:00
|
|
|
const SafePath = require('./SafePath')
|
2021-02-17 06:40:56 -05:00
|
|
|
const { DeletedFile } = require('../../models/DeletedFile')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
const LOCK_NAMESPACE = 'mongoTransaction'
|
2019-11-21 07:44:39 -05:00
|
|
|
const ENTITY_TYPE_TO_MONGO_PATH_SEGMENT = {
|
|
|
|
doc: 'docs',
|
|
|
|
docs: 'docs',
|
|
|
|
file: 'fileRefs',
|
|
|
|
files: 'fileRefs',
|
|
|
|
fileRefs: 'fileRefs',
|
|
|
|
folder: 'folders',
|
2021-04-27 03:52:58 -04:00
|
|
|
folders: 'folders',
|
2019-11-21 07:44:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
LOCK_NAMESPACE,
|
|
|
|
addDoc: callbackifyMultiResult(wrapWithLock(addDoc), ['result', 'project']),
|
|
|
|
addFile: callbackifyMultiResult(wrapWithLock(addFile), ['result', 'project']),
|
|
|
|
addFolder: callbackifyMultiResult(wrapWithLock(addFolder), [
|
|
|
|
'folder',
|
2021-04-27 03:52:58 -04:00
|
|
|
'parentFolderId',
|
2019-11-21 07:44:39 -05:00
|
|
|
]),
|
|
|
|
replaceFileWithNew: callbackifyMultiResult(wrapWithLock(replaceFileWithNew), [
|
|
|
|
'oldFileRef',
|
|
|
|
'project',
|
|
|
|
'path',
|
2021-04-27 03:52:58 -04:00
|
|
|
'newProject',
|
2019-11-21 07:44:39 -05:00
|
|
|
]),
|
2020-03-04 04:37:43 -05:00
|
|
|
replaceDocWithFile: callbackify(replaceDocWithFile),
|
2020-04-27 09:00:56 -04:00
|
|
|
replaceFileWithDoc: callbackify(replaceFileWithDoc),
|
2019-11-21 07:44:39 -05:00
|
|
|
mkdirp: callbackifyMultiResult(wrapWithLock(mkdirp), [
|
|
|
|
'newFolders',
|
2021-04-27 03:52:58 -04:00
|
|
|
'folder',
|
2019-11-21 07:44:39 -05:00
|
|
|
]),
|
|
|
|
moveEntity: callbackifyMultiResult(wrapWithLock(moveEntity), [
|
|
|
|
'project',
|
|
|
|
'startPath',
|
|
|
|
'endPath',
|
|
|
|
'rev',
|
2021-04-27 03:52:58 -04:00
|
|
|
'changes',
|
2019-11-21 07:44:39 -05:00
|
|
|
]),
|
|
|
|
deleteEntity: callbackifyMultiResult(wrapWithLock(deleteEntity), [
|
|
|
|
'entity',
|
|
|
|
'path',
|
|
|
|
'projectBeforeDeletion',
|
2021-04-27 03:52:58 -04:00
|
|
|
'newProject',
|
2019-11-21 07:44:39 -05:00
|
|
|
]),
|
|
|
|
renameEntity: callbackifyMultiResult(wrapWithLock(renameEntity), [
|
|
|
|
'project',
|
|
|
|
'startPath',
|
|
|
|
'endPath',
|
|
|
|
'rev',
|
2021-04-27 03:52:58 -04:00
|
|
|
'changes',
|
2019-11-21 07:44:39 -05:00
|
|
|
]),
|
|
|
|
createNewFolderStructure: callbackify(wrapWithLock(createNewFolderStructure)),
|
|
|
|
_insertDeletedFileReference: callbackify(_insertDeletedFileReference),
|
|
|
|
_putElement: callbackifyMultiResult(_putElement, ['result', 'project']),
|
|
|
|
_confirmFolder,
|
|
|
|
promises: {
|
|
|
|
addDoc: wrapWithLock(addDoc),
|
|
|
|
addFile: wrapWithLock(addFile),
|
|
|
|
addFolder: wrapWithLock(addFolder),
|
|
|
|
replaceFileWithNew: wrapWithLock(replaceFileWithNew),
|
2020-03-04 04:37:43 -05:00
|
|
|
replaceDocWithFile: wrapWithLock(replaceDocWithFile),
|
2020-04-27 09:00:56 -04:00
|
|
|
replaceFileWithDoc: wrapWithLock(replaceFileWithDoc),
|
2019-11-21 07:44:39 -05:00
|
|
|
mkdirp: wrapWithLock(mkdirp),
|
|
|
|
moveEntity: wrapWithLock(moveEntity),
|
|
|
|
deleteEntity: wrapWithLock(deleteEntity),
|
|
|
|
renameEntity: wrapWithLock(renameEntity),
|
|
|
|
createNewFolderStructure: wrapWithLock(createNewFolderStructure),
|
|
|
|
_insertDeletedFileReference,
|
2021-04-27 03:52:58 -04:00
|
|
|
_putElement,
|
|
|
|
},
|
2019-11-21 07:44:39 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
function wrapWithLock(methodWithoutLock) {
|
2019-05-29 05:21:06 -04:00
|
|
|
// This lock is used whenever we read or write to an existing project's
|
|
|
|
// structure. Some operations to project structure cannot be done atomically
|
|
|
|
// in mongo, this lock is used to prevent reading the structure between two
|
|
|
|
// parts of a staged update.
|
2019-11-21 07:44:39 -05:00
|
|
|
async function methodWithLock(projectId, ...rest) {
|
|
|
|
return LockManager.promises.runWithLock(LOCK_NAMESPACE, projectId, () =>
|
|
|
|
methodWithoutLock(projectId, ...rest)
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return methodWithLock
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
async function addDoc(projectId, folderId, doc) {
|
|
|
|
const project = await ProjectGetter.promises.getProjectWithoutLock(
|
|
|
|
projectId,
|
|
|
|
{
|
|
|
|
rootFolder: true,
|
|
|
|
name: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
overleaf: true,
|
2019-11-21 07:44:39 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
folderId = _confirmFolder(project, folderId)
|
|
|
|
const { result, project: newProject } = await _putElement(
|
|
|
|
project,
|
|
|
|
folderId,
|
|
|
|
doc,
|
|
|
|
'doc'
|
|
|
|
)
|
|
|
|
return { result, project: newProject }
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
async function addFile(projectId, folderId, fileRef) {
|
|
|
|
const project = await ProjectGetter.promises.getProjectWithoutLock(
|
|
|
|
projectId,
|
|
|
|
{ rootFolder: true, name: true, overleaf: true }
|
|
|
|
)
|
|
|
|
folderId = _confirmFolder(project, folderId)
|
|
|
|
const { result, project: newProject } = await _putElement(
|
|
|
|
project,
|
|
|
|
folderId,
|
|
|
|
fileRef,
|
|
|
|
'file'
|
|
|
|
)
|
|
|
|
return { result, project: newProject }
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
async function addFolder(projectId, parentFolderId, folderName) {
|
|
|
|
const project = await ProjectGetter.promises.getProjectWithoutLock(
|
|
|
|
projectId,
|
|
|
|
{ rootFolder: true, name: true, overleaf: true }
|
|
|
|
)
|
|
|
|
parentFolderId = _confirmFolder(project, parentFolderId)
|
|
|
|
const folder = new Folder({ name: folderName })
|
|
|
|
await _putElement(project, parentFolderId, folder, 'folder')
|
|
|
|
return { folder, parentFolderId }
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
async function replaceFileWithNew(projectId, fileId, newFileRef) {
|
|
|
|
const project = await ProjectGetter.promises.getProjectWithoutLock(
|
|
|
|
projectId,
|
|
|
|
{ rootFolder: true, name: true, overleaf: true }
|
|
|
|
)
|
|
|
|
const { element: fileRef, path } = await ProjectLocator.promises.findElement({
|
|
|
|
project,
|
|
|
|
element_id: fileId,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'file',
|
2019-11-21 07:44:39 -05:00
|
|
|
})
|
|
|
|
await _insertDeletedFileReference(projectId, fileRef)
|
|
|
|
const newProject = await Project.findOneAndUpdate(
|
|
|
|
{ _id: project._id },
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
[`${path.mongo}._id`]: newFileRef._id,
|
|
|
|
[`${path.mongo}.created`]: new Date(),
|
|
|
|
[`${path.mongo}.linkedFileData`]: newFileRef.linkedFileData,
|
2021-04-27 03:52:58 -04:00
|
|
|
[`${path.mongo}.hash`]: newFileRef.hash,
|
2019-11-21 07:44:39 -05:00
|
|
|
},
|
|
|
|
$inc: {
|
|
|
|
version: 1,
|
2021-04-27 03:52:58 -04:00
|
|
|
[`${path.mongo}.rev`]: 1,
|
|
|
|
},
|
2019-11-21 07:44:39 -05:00
|
|
|
},
|
|
|
|
{ new: true }
|
|
|
|
).exec()
|
|
|
|
// Note: Mongoose uses new:true to return the modified document
|
|
|
|
// https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
|
|
|
|
// but Mongo uses returnNewDocument:true instead
|
|
|
|
// https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/
|
|
|
|
// We are using Mongoose here, but if we ever switch to a direct mongo call
|
|
|
|
// the next line will need to be updated.
|
|
|
|
return { oldFileRef: fileRef, project, path, newProject }
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
|
2020-03-04 04:37:43 -05:00
|
|
|
async function replaceDocWithFile(projectId, docId, fileRef) {
|
|
|
|
const project = await ProjectGetter.promises.getProjectWithoutLock(
|
|
|
|
projectId,
|
|
|
|
{ rootFolder: true, name: true, overleaf: true }
|
|
|
|
)
|
|
|
|
const { path } = await ProjectLocator.promises.findElement({
|
|
|
|
project,
|
|
|
|
element_id: docId,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'doc',
|
2020-03-04 04:37:43 -05:00
|
|
|
})
|
|
|
|
const folderMongoPath = _getParentMongoPath(path.mongo)
|
|
|
|
const newProject = await Project.findOneAndUpdate(
|
|
|
|
{ _id: project._id },
|
|
|
|
{
|
|
|
|
$pull: {
|
2021-04-27 03:52:58 -04:00
|
|
|
[`${folderMongoPath}.docs`]: { _id: docId },
|
2020-03-04 04:37:43 -05:00
|
|
|
},
|
|
|
|
$push: {
|
2021-04-27 03:52:58 -04:00
|
|
|
[`${folderMongoPath}.fileRefs`]: fileRef,
|
2020-03-04 04:37:43 -05:00
|
|
|
},
|
2021-04-27 03:52:58 -04:00
|
|
|
$inc: { version: 1 },
|
2020-03-04 04:37:43 -05:00
|
|
|
},
|
|
|
|
{ new: true }
|
|
|
|
).exec()
|
|
|
|
return newProject
|
|
|
|
}
|
|
|
|
|
2020-04-27 09:00:56 -04:00
|
|
|
async function replaceFileWithDoc(projectId, fileId, newDoc) {
|
|
|
|
const project = await ProjectGetter.promises.getProjectWithoutLock(
|
|
|
|
projectId,
|
|
|
|
{ rootFolder: true, name: true, overleaf: true }
|
|
|
|
)
|
|
|
|
const { path } = await ProjectLocator.promises.findElement({
|
|
|
|
project,
|
|
|
|
element_id: fileId,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'file',
|
2020-04-27 09:00:56 -04:00
|
|
|
})
|
|
|
|
const folderMongoPath = _getParentMongoPath(path.mongo)
|
|
|
|
const newProject = await Project.findOneAndUpdate(
|
|
|
|
{ _id: project._id },
|
|
|
|
{
|
|
|
|
$pull: {
|
2021-04-27 03:52:58 -04:00
|
|
|
[`${folderMongoPath}.fileRefs`]: { _id: fileId },
|
2020-04-27 09:00:56 -04:00
|
|
|
},
|
|
|
|
$push: {
|
2021-04-27 03:52:58 -04:00
|
|
|
[`${folderMongoPath}.docs`]: newDoc,
|
2020-04-27 09:00:56 -04:00
|
|
|
},
|
2021-04-27 03:52:58 -04:00
|
|
|
$inc: { version: 1 },
|
2020-04-27 09:00:56 -04:00
|
|
|
},
|
|
|
|
{ new: true }
|
|
|
|
).exec()
|
|
|
|
return newProject
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
async function mkdirp(projectId, path, options = {}) {
|
|
|
|
// defaults to case insensitive paths, use options {exactCaseMatch:true}
|
|
|
|
// to make matching case-sensitive
|
|
|
|
let folders = path.split('/')
|
|
|
|
folders = _.select(folders, folder => folder.length !== 0)
|
|
|
|
|
|
|
|
const project = await ProjectGetter.promises.getProjectWithOnlyFolders(
|
|
|
|
projectId
|
|
|
|
)
|
|
|
|
if (path === '/') {
|
|
|
|
return { newFolders: [], folder: project.rootFolder[0] }
|
|
|
|
}
|
|
|
|
|
|
|
|
const newFolders = []
|
|
|
|
let builtUpPath = ''
|
|
|
|
let lastFolder = null
|
|
|
|
for (const folderName of folders) {
|
|
|
|
builtUpPath += `/${folderName}`
|
|
|
|
try {
|
|
|
|
const {
|
2021-04-27 03:52:58 -04:00
|
|
|
element: foundFolder,
|
2019-11-21 07:44:39 -05:00
|
|
|
} = await ProjectLocator.promises.findElementByPath({
|
|
|
|
project,
|
|
|
|
path: builtUpPath,
|
2021-04-27 03:52:58 -04:00
|
|
|
exactCaseMatch: options.exactCaseMatch,
|
2019-11-04 04:50:15 -05:00
|
|
|
})
|
2019-11-21 07:44:39 -05:00
|
|
|
lastFolder = foundFolder
|
|
|
|
} catch (err) {
|
|
|
|
// Folder couldn't be found. Create it.
|
|
|
|
const parentFolderId = lastFolder && lastFolder._id
|
|
|
|
const {
|
|
|
|
folder: newFolder,
|
2021-04-27 03:52:58 -04:00
|
|
|
parentFolderId: newParentFolderId,
|
2019-11-21 07:44:39 -05:00
|
|
|
} = await addFolder(projectId, parentFolderId, folderName)
|
|
|
|
newFolder.parentFolder_id = newParentFolderId
|
|
|
|
lastFolder = newFolder
|
|
|
|
newFolders.push(newFolder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { folder: lastFolder, newFolders }
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
async function moveEntity(projectId, entityId, destFolderId, entityType) {
|
|
|
|
const project = await ProjectGetter.promises.getProjectWithoutLock(
|
2019-11-04 04:50:15 -05:00
|
|
|
projectId,
|
2019-11-21 07:44:39 -05:00
|
|
|
{ rootFolder: true, name: true, overleaf: true }
|
|
|
|
)
|
|
|
|
const {
|
|
|
|
element: entity,
|
2021-04-27 03:52:58 -04:00
|
|
|
path: entityPath,
|
2019-11-21 07:44:39 -05:00
|
|
|
} = await ProjectLocator.promises.findElement({
|
|
|
|
project,
|
|
|
|
element_id: entityId,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: entityType,
|
2019-11-21 07:44:39 -05:00
|
|
|
})
|
|
|
|
// Prevent top-level docs/files with reserved names (to match v1 behaviour)
|
|
|
|
if (_blockedFilename(entityPath, entityType)) {
|
|
|
|
throw new Errors.InvalidNameError('blocked element name')
|
|
|
|
}
|
|
|
|
await _checkValidMove(project, entityType, entity, entityPath, destFolderId)
|
|
|
|
const {
|
|
|
|
docs: oldDocs,
|
2021-04-27 03:52:58 -04:00
|
|
|
files: oldFiles,
|
2019-11-21 07:44:39 -05:00
|
|
|
} = await ProjectEntityHandler.promises.getAllEntitiesFromProject(project)
|
|
|
|
// For safety, insert the entity in the destination
|
|
|
|
// location first, and then remove the original. If
|
|
|
|
// there is an error the entity may appear twice. This
|
|
|
|
// will cause some breakage but is better than being
|
|
|
|
// lost, which is what happens if this is done in the
|
|
|
|
// opposite order.
|
|
|
|
const { result } = await _putElement(
|
|
|
|
project,
|
2019-05-29 05:21:06 -04:00
|
|
|
destFolderId,
|
2019-11-21 07:44:39 -05:00
|
|
|
entity,
|
|
|
|
entityType
|
|
|
|
)
|
|
|
|
// Note: putElement always pushes onto the end of an
|
|
|
|
// array so it will never change an existing mongo
|
|
|
|
// path. Therefore it is safe to remove an element
|
|
|
|
// from the project with an existing path after
|
|
|
|
// calling putElement. But we must be sure that we
|
|
|
|
// have not moved a folder subfolder of itself (which
|
|
|
|
// is done by _checkValidMove above) because that
|
|
|
|
// would lead to it being deleted.
|
|
|
|
const newProject = await _removeElementFromMongoArray(
|
|
|
|
Project,
|
|
|
|
projectId,
|
|
|
|
entityPath.mongo,
|
|
|
|
entityId
|
|
|
|
)
|
|
|
|
const {
|
|
|
|
docs: newDocs,
|
2021-04-27 03:52:58 -04:00
|
|
|
files: newFiles,
|
2019-11-21 07:44:39 -05:00
|
|
|
} = await ProjectEntityHandler.promises.getAllEntitiesFromProject(newProject)
|
|
|
|
const startPath = entityPath.fileSystem
|
|
|
|
const endPath = result.path.fileSystem
|
|
|
|
const changes = {
|
|
|
|
oldDocs,
|
|
|
|
newDocs,
|
|
|
|
oldFiles,
|
|
|
|
newFiles,
|
2021-04-27 03:52:58 -04:00
|
|
|
newProject,
|
2019-11-21 07:44:39 -05:00
|
|
|
}
|
|
|
|
// check that no files have been lost (or duplicated)
|
|
|
|
if (
|
|
|
|
oldFiles.length !== newFiles.length ||
|
|
|
|
oldDocs.length !== newDocs.length
|
2019-05-29 05:21:06 -04:00
|
|
|
) {
|
2019-11-21 07:44:39 -05:00
|
|
|
logger.warn(
|
|
|
|
{
|
2019-11-04 04:50:15 -05:00
|
|
|
projectId,
|
2019-11-21 07:44:39 -05:00
|
|
|
oldDocs: oldDocs.length,
|
|
|
|
newDocs: newDocs.length,
|
|
|
|
oldFiles: oldFiles.length,
|
|
|
|
newFiles: newFiles.length,
|
|
|
|
origProject: project,
|
2021-04-27 03:52:58 -04:00
|
|
|
newProject,
|
2019-11-21 07:44:39 -05:00
|
|
|
},
|
|
|
|
"project corrupted moving files - shouldn't happen"
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-11-21 07:44:39 -05:00
|
|
|
throw new Error('unexpected change in project structure')
|
|
|
|
}
|
|
|
|
return { project, startPath, endPath, rev: entity.rev, changes }
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
async function deleteEntity(projectId, entityId, entityType, callback) {
|
|
|
|
const project = await ProjectGetter.promises.getProjectWithoutLock(
|
|
|
|
projectId,
|
2020-02-27 07:46:37 -05:00
|
|
|
{ name: true, rootFolder: true, overleaf: true, rootDoc_id: true }
|
2019-11-21 07:44:39 -05:00
|
|
|
)
|
2020-02-27 07:46:37 -05:00
|
|
|
const deleteRootDoc =
|
|
|
|
project.rootDoc_id &&
|
|
|
|
entityId &&
|
|
|
|
project.rootDoc_id.toString() === entityId.toString()
|
2019-11-21 07:44:39 -05:00
|
|
|
const { element: entity, path } = await ProjectLocator.promises.findElement({
|
|
|
|
project,
|
|
|
|
element_id: entityId,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: entityType,
|
2019-11-21 07:44:39 -05:00
|
|
|
})
|
|
|
|
const newProject = await _removeElementFromMongoArray(
|
|
|
|
Project,
|
|
|
|
projectId,
|
|
|
|
path.mongo,
|
2020-02-27 07:46:37 -05:00
|
|
|
entityId,
|
|
|
|
deleteRootDoc
|
2019-11-21 07:44:39 -05:00
|
|
|
)
|
|
|
|
return { entity, path, projectBeforeDeletion: project, newProject }
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
async function renameEntity(
|
|
|
|
projectId,
|
|
|
|
entityId,
|
|
|
|
entityType,
|
|
|
|
newName,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
const project = await ProjectGetter.promises.getProjectWithoutLock(
|
|
|
|
projectId,
|
|
|
|
{ rootFolder: true, name: true, overleaf: true }
|
|
|
|
)
|
|
|
|
const {
|
|
|
|
element: entity,
|
|
|
|
path: entPath,
|
2021-04-27 03:52:58 -04:00
|
|
|
folder: parentFolder,
|
2019-11-21 07:44:39 -05:00
|
|
|
} = await ProjectLocator.promises.findElement({
|
|
|
|
project,
|
|
|
|
element_id: entityId,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: entityType,
|
2019-11-21 07:44:39 -05:00
|
|
|
})
|
|
|
|
const startPath = entPath.fileSystem
|
|
|
|
const endPath = path.join(path.dirname(entPath.fileSystem), newName)
|
|
|
|
|
|
|
|
// Prevent top-level docs/files with reserved names (to match v1 behaviour)
|
|
|
|
if (_blockedFilename({ fileSystem: endPath }, entityType)) {
|
|
|
|
throw new Errors.InvalidNameError('blocked element name')
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
// check if the new name already exists in the current folder
|
|
|
|
_checkValidElementName(parentFolder, newName)
|
|
|
|
|
|
|
|
const {
|
|
|
|
docs: oldDocs,
|
2021-04-27 03:52:58 -04:00
|
|
|
files: oldFiles,
|
2019-11-21 07:44:39 -05:00
|
|
|
} = await ProjectEntityHandler.promises.getAllEntitiesFromProject(project)
|
|
|
|
|
|
|
|
// we need to increment the project version number for any structure change
|
|
|
|
const newProject = await Project.findOneAndUpdate(
|
|
|
|
{ _id: projectId },
|
|
|
|
{ $set: { [`${entPath.mongo}.name`]: newName }, $inc: { version: 1 } },
|
|
|
|
{ new: true }
|
|
|
|
).exec()
|
|
|
|
|
|
|
|
const {
|
|
|
|
docs: newDocs,
|
2021-04-27 03:52:58 -04:00
|
|
|
files: newFiles,
|
2019-11-21 07:44:39 -05:00
|
|
|
} = await ProjectEntityHandler.promises.getAllEntitiesFromProject(newProject)
|
|
|
|
return {
|
|
|
|
project,
|
|
|
|
startPath,
|
|
|
|
endPath,
|
|
|
|
rev: entity.rev,
|
2021-04-27 03:52:58 -04:00
|
|
|
changes: { oldDocs, newDocs, oldFiles, newFiles, newProject },
|
2019-11-21 07:44:39 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
async function _insertDeletedFileReference(projectId, fileRef) {
|
2021-02-17 06:40:56 -05:00
|
|
|
await DeletedFile.create({
|
|
|
|
projectId,
|
|
|
|
_id: fileRef._id,
|
|
|
|
name: fileRef.name,
|
|
|
|
linkedFileData: fileRef.linkedFileData,
|
|
|
|
hash: fileRef.hash,
|
2021-04-27 03:52:58 -04:00
|
|
|
deletedAt: new Date(),
|
2021-02-17 06:40:56 -05:00
|
|
|
})
|
2019-11-21 07:44:39 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-02-27 07:46:37 -05:00
|
|
|
async function _removeElementFromMongoArray(
|
|
|
|
model,
|
|
|
|
modelId,
|
|
|
|
path,
|
|
|
|
elementId,
|
|
|
|
deleteRootDoc = false
|
|
|
|
) {
|
2019-11-21 07:44:39 -05:00
|
|
|
const nonArrayPath = path.slice(0, path.lastIndexOf('.'))
|
2020-02-27 07:46:37 -05:00
|
|
|
const options = { new: true }
|
|
|
|
const query = { _id: modelId }
|
|
|
|
const update = {
|
|
|
|
$pull: { [nonArrayPath]: { _id: elementId } },
|
2021-04-27 03:52:58 -04:00
|
|
|
$inc: { version: 1 },
|
2020-02-27 07:46:37 -05:00
|
|
|
}
|
|
|
|
if (deleteRootDoc) {
|
|
|
|
update.$unset = { rootDoc_id: 1 }
|
|
|
|
}
|
|
|
|
return model.findOneAndUpdate(query, update, options).exec()
|
2019-11-21 07:44:39 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
function _countElements(project) {
|
|
|
|
function countFolder(folder) {
|
|
|
|
if (folder == null) {
|
|
|
|
return 0
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
let total = 0
|
|
|
|
if (folder.folders) {
|
|
|
|
total += folder.folders.length
|
|
|
|
for (const subfolder of folder.folders) {
|
|
|
|
total += countFolder(subfolder)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2019-11-21 07:44:39 -05:00
|
|
|
if (folder.docs) {
|
|
|
|
total += folder.docs.length
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-21 07:44:39 -05:00
|
|
|
if (folder.fileRefs) {
|
|
|
|
total += folder.fileRefs.length
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-21 07:44:39 -05:00
|
|
|
return total
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
return countFolder(project.rootFolder[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _putElement(project, folderId, element, type) {
|
|
|
|
if (element == null || element._id == null) {
|
|
|
|
logger.warn(
|
|
|
|
{ projectId: project._id, folderId, element, type },
|
|
|
|
'failed trying to insert element as it was null'
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-11-21 07:44:39 -05:00
|
|
|
throw new Error('no element passed to be inserted')
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
const pathSegment = _getMongoPathSegmentFromType(type)
|
|
|
|
|
|
|
|
// original check path.resolve("/", element.name) isnt "/#{element.name}" or element.name.match("/")
|
|
|
|
// check if name is allowed
|
|
|
|
if (!SafePath.isCleanFilename(element.name)) {
|
|
|
|
logger.warn(
|
|
|
|
{ projectId: project._id, folderId, element, type },
|
|
|
|
'failed trying to insert element as name was invalid'
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-11-21 07:44:39 -05:00
|
|
|
throw new Errors.InvalidNameError('invalid element name')
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-21 07:44:39 -05:00
|
|
|
if (folderId == null) {
|
|
|
|
folderId = project.rootFolder[0]._id
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_countElements(project) > Settings.maxEntitiesPerProject) {
|
|
|
|
logger.warn(
|
|
|
|
{ projectId: project._id },
|
|
|
|
'project too big, stopping insertions'
|
|
|
|
)
|
|
|
|
CooldownManager.putProjectOnCooldown(project._id)
|
2020-02-18 06:37:15 -05:00
|
|
|
throw new Error('project_has_too_many_files')
|
2019-11-21 07:44:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const { element: folder, path } = await ProjectLocator.promises.findElement({
|
|
|
|
project,
|
|
|
|
element_id: folderId,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'folder',
|
2019-11-21 07:44:39 -05:00
|
|
|
})
|
|
|
|
const newPath = {
|
|
|
|
fileSystem: `${path.fileSystem}/${element.name}`,
|
2021-04-27 03:52:58 -04:00
|
|
|
mongo: path.mongo,
|
2019-11-21 07:44:39 -05:00
|
|
|
}
|
|
|
|
// check if the path would be too long
|
|
|
|
if (!SafePath.isAllowedLength(newPath.fileSystem)) {
|
|
|
|
throw new Errors.InvalidNameError('path too long')
|
|
|
|
}
|
|
|
|
// Prevent top-level docs/files with reserved names (to match v1 behaviour)
|
|
|
|
if (_blockedFilename(newPath, type)) {
|
|
|
|
throw new Errors.InvalidNameError('blocked element name')
|
|
|
|
}
|
|
|
|
_checkValidElementName(folder, element.name)
|
|
|
|
element._id = ObjectId(element._id.toString())
|
|
|
|
const mongoPath = `${path.mongo}.${pathSegment}`
|
|
|
|
const newProject = await Project.findOneAndUpdate(
|
|
|
|
{ _id: project._id },
|
|
|
|
{ $push: { [mongoPath]: element }, $inc: { version: 1 } },
|
|
|
|
{ new: true }
|
|
|
|
).exec()
|
|
|
|
return { result: { path: newPath }, project: newProject }
|
|
|
|
}
|
|
|
|
|
|
|
|
function _blockedFilename(entityPath, entityType) {
|
|
|
|
// check if name would be blocked in v1
|
|
|
|
// javascript reserved names are forbidden for docs and files
|
|
|
|
// at the top-level (but folders with reserved names are allowed).
|
|
|
|
const isFolder = entityType === 'folder'
|
|
|
|
const dir = path.dirname(entityPath.fileSystem)
|
|
|
|
const file = path.basename(entityPath.fileSystem)
|
|
|
|
const isTopLevel = dir === '/'
|
|
|
|
if (isTopLevel && !isFolder && SafePath.isBlockedFilename(file)) {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getMongoPathSegmentFromType(type) {
|
|
|
|
const pathSegment = ENTITY_TYPE_TO_MONGO_PATH_SEGMENT[type]
|
|
|
|
if (pathSegment == null) {
|
|
|
|
throw new Error(`Unknown entity type: ${type}`)
|
|
|
|
}
|
|
|
|
return pathSegment
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the name is already taken by a doc, file or folder. If so, return an
|
|
|
|
* error "file already exists".
|
|
|
|
*/
|
|
|
|
function _checkValidElementName(folder, name) {
|
|
|
|
if (folder == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const elements = []
|
|
|
|
.concat(folder.docs || [])
|
|
|
|
.concat(folder.fileRefs || [])
|
|
|
|
.concat(folder.folders || [])
|
|
|
|
for (const element of elements) {
|
|
|
|
if (element.name === name) {
|
|
|
|
throw new Errors.InvalidNameError('file already exists')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _confirmFolder(project, folderId) {
|
|
|
|
if (folderId == null) {
|
|
|
|
return project.rootFolder[0]._id
|
|
|
|
} else {
|
|
|
|
return folderId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _checkValidMove(
|
|
|
|
project,
|
|
|
|
entityType,
|
|
|
|
entity,
|
|
|
|
entityPath,
|
|
|
|
destFolderId
|
|
|
|
) {
|
|
|
|
const {
|
|
|
|
element: destEntity,
|
2021-04-27 03:52:58 -04:00
|
|
|
path: destFolderPath,
|
2019-11-21 07:44:39 -05:00
|
|
|
} = await ProjectLocator.promises.findElement({
|
|
|
|
project,
|
|
|
|
element_id: destFolderId,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'folder',
|
2019-11-21 07:44:39 -05:00
|
|
|
})
|
|
|
|
// check if there is already a doc/file/folder with the same name
|
|
|
|
// in the destination folder
|
|
|
|
_checkValidElementName(destEntity, entity.name)
|
|
|
|
if (/folder/.test(entityType)) {
|
|
|
|
const isNestedFolder =
|
|
|
|
destFolderPath.fileSystem.slice(0, entityPath.fileSystem.length) ===
|
|
|
|
entityPath.fileSystem
|
|
|
|
if (isNestedFolder) {
|
|
|
|
throw new Errors.InvalidNameError(
|
|
|
|
'destination folder is a child folder of me'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-04-23 07:51:06 -04:00
|
|
|
/**
|
|
|
|
* Create an initial file tree out of a list of doc and file entries
|
|
|
|
*
|
|
|
|
* Each entry specifies a path to the doc or file. Folders are automatically
|
|
|
|
* created.
|
|
|
|
*
|
|
|
|
* @param {ObjectId} projectId - id of the project
|
|
|
|
* @param {DocEntry[]} docEntries - list of docs to add
|
|
|
|
* @param {FileEntry[]} fileEntries - list of files to add
|
|
|
|
* @return {Promise<string>} the project version after the operation
|
|
|
|
*/
|
|
|
|
async function createNewFolderStructure(projectId, docEntries, fileEntries) {
|
2019-11-21 07:44:39 -05:00
|
|
|
try {
|
|
|
|
const rootFolder = FolderStructureBuilder.buildFolderStructure(
|
2020-04-23 07:51:06 -04:00
|
|
|
docEntries,
|
|
|
|
fileEntries
|
2019-11-21 07:44:39 -05:00
|
|
|
)
|
2020-04-23 07:51:06 -04:00
|
|
|
const project = await Project.findOneAndUpdate(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
2019-11-21 07:44:39 -05:00
|
|
|
_id: projectId,
|
|
|
|
'rootFolder.0.folders.0': { $exists: false },
|
|
|
|
'rootFolder.0.docs.0': { $exists: false },
|
2021-04-27 03:52:58 -04:00
|
|
|
'rootFolder.0.files.0': { $exists: false },
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
{
|
2019-11-21 07:44:39 -05:00
|
|
|
$set: { rootFolder: [rootFolder] },
|
2021-04-27 03:52:58 -04:00
|
|
|
$inc: { version: 1 },
|
2020-04-23 07:51:06 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
new: true,
|
|
|
|
lean: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
fields: { version: 1 },
|
2019-11-21 07:44:39 -05:00
|
|
|
}
|
|
|
|
).exec()
|
2020-04-23 07:51:06 -04:00
|
|
|
if (project == null) {
|
2020-08-11 05:28:29 -04:00
|
|
|
throw new OError('project not found or folder structure already exists', {
|
2021-04-27 03:52:58 -04:00
|
|
|
projectId,
|
2019-11-21 07:44:39 -05:00
|
|
|
})
|
|
|
|
}
|
2020-04-23 07:51:06 -04:00
|
|
|
return project.version
|
2019-11-21 07:44:39 -05:00
|
|
|
} catch (err) {
|
2020-08-11 05:28:29 -04:00
|
|
|
throw OError.tag(err, 'failed to create folder structure', { projectId })
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-04 04:37:43 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a Mongo path to an entity, return the Mongo path to the parent folder
|
|
|
|
*/
|
|
|
|
function _getParentMongoPath(mongoPath) {
|
|
|
|
const segments = mongoPath.split('.')
|
|
|
|
if (segments.length <= 2) {
|
|
|
|
throw new Error('Root folder has no parents')
|
|
|
|
}
|
|
|
|
return segments.slice(0, -2).join('.')
|
|
|
|
}
|