Merge pull request #2639 from overleaf/em-convert-doc-to-file

Endpoint for converting a doc to a file

GitOrigin-RevId: 0a3bd46a7a78537b0f64dc577402277cbe81fecb
This commit is contained in:
Eric Mc Sween
2020-03-04 04:37:43 -05:00
committed by Copybot
parent 693100358c
commit 2627595040
9 changed files with 1011 additions and 615 deletions

View File

@@ -47,6 +47,7 @@ module.exports = {
'path',
'newProject'
]),
replaceDocWithFile: callbackify(replaceDocWithFile),
mkdirp: callbackifyMultiResult(wrapWithLock(mkdirp), [
'newFolders',
'folder'
@@ -81,6 +82,7 @@ module.exports = {
addFile: wrapWithLock(addFile),
addFolder: wrapWithLock(addFolder),
replaceFileWithNew: wrapWithLock(replaceFileWithNew),
replaceDocWithFile: wrapWithLock(replaceDocWithFile),
mkdirp: wrapWithLock(mkdirp),
moveEntity: wrapWithLock(moveEntity),
deleteEntity: wrapWithLock(deleteEntity),
@@ -186,6 +188,33 @@ async function replaceFileWithNew(projectId, fileId, newFileRef) {
return { oldFileRef: fileRef, project, path, newProject }
}
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,
type: 'doc'
})
const folderMongoPath = _getParentMongoPath(path.mongo)
const newProject = await Project.findOneAndUpdate(
{ _id: project._id },
{
$pull: {
[`${folderMongoPath}.docs`]: { _id: docId }
},
$push: {
[`${folderMongoPath}.fileRefs`]: fileRef
},
$inc: { version: 1 }
},
{ new: true }
).exec()
return newProject
}
async function mkdirp(projectId, path, options = {}) {
// defaults to case insensitive paths, use options {exactCaseMatch:true}
// to make matching case-sensitive
@@ -637,3 +666,14 @@ async function createNewFolderStructure(projectId, docUploads, fileUploads) {
}).withCause(err)
}
}
/**
* 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('.')
}