| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730 |
- const _ = require('lodash')
- const OError = require('@overleaf/o-error')
- const logger = require('@overleaf/logger')
- const Settings = require('@overleaf/settings')
- const Path = require('path')
- const fs = require('fs')
- const { Doc } = require('../../models/Doc')
- const DocstoreManager = require('../Docstore/DocstoreManager')
- const DocumentUpdaterHandler = require('../../Features/DocumentUpdater/DocumentUpdaterHandler')
- const Errors = require('../Errors/Errors')
- const FileStoreHandler = require('../FileStore/FileStoreHandler')
- const LockManager = require('../../infrastructure/LockManager')
- const { Project } = require('../../models/Project')
- const ProjectEntityHandler = require('./ProjectEntityHandler')
- const ProjectGetter = require('./ProjectGetter')
- const ProjectLocator = require('./ProjectLocator')
- const ProjectOptionsHandler = require('./ProjectOptionsHandler')
- const ProjectUpdateHandler = require('./ProjectUpdateHandler')
- const ProjectEntityMongoUpdateHandler = require('./ProjectEntityMongoUpdateHandler')
- const SafePath = require('./SafePath')
- const TpdsUpdateSender = require('../ThirdPartyDataStore/TpdsUpdateSender')
- const FileWriter = require('../../infrastructure/FileWriter')
- const EditorRealTimeController = require('../Editor/EditorRealTimeController')
- const {
- callbackifyMultiResult,
- callbackify,
- } = require('@overleaf/promise-utils')
- const { iterablePaths } = require('./IterablePath')
- const LOCK_NAMESPACE = 'sequentialProjectStructureUpdateLock'
- const VALID_ROOT_DOC_EXTENSIONS = Settings.validRootDocExtensions
- const VALID_ROOT_DOC_REGEXP = new RegExp(
- `^\\.(${VALID_ROOT_DOC_EXTENSIONS.join('|')})$`,
- 'i'
- )
- function wrapWithLock(methodWithoutLock, lockManager = LockManager) {
- // This lock is used to make sure that the project structure updates are made
- // sequentially. In particular the updates must be made in mongo and sent to
- // the doc-updater in the same order.
- if (typeof methodWithoutLock === 'function') {
- const methodWithLock = async (projectId, ...rest) => {
- return lockManager.promises.runWithLock(LOCK_NAMESPACE, projectId, () =>
- methodWithoutLock(projectId, ...rest)
- )
- }
- methodWithLock.withoutLock = methodWithoutLock
- return methodWithLock
- } else {
- // handle case with separate setup and locked stages
- const mainTask = methodWithoutLock.withLock
- const methodWithLock = async (projectId, ...rest) => {
- const arg = await methodWithoutLock.beforeLock(projectId, ...rest)
- return lockManager.promises.runWithLock(LOCK_NAMESPACE, projectId, () =>
- mainTask(arg)
- )
- }
- methodWithLock.withoutLock = async (...args) => {
- return await mainTask(await methodWithoutLock.beforeLock(...args))
- }
- methodWithLock.beforeLock = methodWithoutLock.beforeLock
- methodWithLock.mainTask = methodWithoutLock.withLock
- return methodWithLock
- }
- }
- async function getDocContext(projectId, docId) {
- let project
- try {
- project = await ProjectGetter.promises.getProject(projectId, {
- name: true,
- rootFolder: true,
- })
- } catch (err) {
- throw OError.tag(err, 'error fetching project', {
- projectId,
- })
- }
- if (!project) {
- throw new Errors.NotFoundError('project not found')
- }
- try {
- const { path, folder } = await ProjectLocator.promises.findElement({
- project,
- element_id: docId,
- type: 'docs',
- })
- return {
- projectName: project.name,
- isDeletedDoc: false,
- path: path.fileSystem,
- folder,
- }
- } catch (err) {
- if (err instanceof Errors.NotFoundError) {
- // (Soft-)Deleted docs are removed from the file-tree (rootFolder).
- // docstore can tell whether it exists and is (soft)-deleted.
- let isDeletedDoc
- try {
- isDeletedDoc = await DocstoreManager.promises.isDocDeleted(
- projectId,
- docId
- )
- if (!isDeletedDoc) {
- // NOTE: This can happen while we delete a doc:
- // 1. web will update the projects entry
- // 2. web triggers flushes to tpds/doc-updater
- // 3. web triggers (soft)-delete in docstore
- // Specifically when an update comes in after 1
- // and before 3 completes.
- logger.debug(
- { projectId, docId },
- 'updating doc that is in process of getting soft-deleted'
- )
- }
- return {
- projectName: project.name,
- isDeletedDoc: true,
- path: null,
- folder: null,
- }
- } catch (error) {
- if (error instanceof Errors.NotFoundError) {
- logger.warn(
- { projectId, docId },
- 'doc not found while updating doc lines'
- )
- throw error
- }
- throw OError.tag(
- error,
- 'error checking deletion status with docstore',
- {
- projectId,
- docId,
- }
- )
- }
- } else {
- throw OError.tag(err, 'error finding doc in rootFolder', {
- docId,
- projectId,
- })
- }
- }
- }
- async function updateDocLines(
- projectId,
- docId,
- lines,
- version,
- ranges,
- lastUpdatedAt,
- lastUpdatedBy
- ) {
- let ctx
- try {
- ctx = await getDocContext(projectId, docId)
- } catch (error) {
- if (error instanceof Errors.NotFoundError) {
- // Do not allow an update to a doc which has never exist on this project
- logger.warn(
- { docId, projectId },
- 'project or doc not found while updating doc lines'
- )
- }
- throw error
- }
- const { projectName, isDeletedDoc, path, folder } = ctx
- logger.debug({ projectId, docId }, 'telling docstore manager to update doc')
- let modified, rev
- try {
- ;({ modified, rev } = await DocstoreManager.promises.updateDoc(
- projectId,
- docId,
- lines,
- version,
- ranges
- ))
- } catch (err) {
- throw OError.tag(err, 'error sending doc to docstore', { docId, projectId })
- }
- // path will only be present if the doc is not deleted
- if (!modified || isDeletedDoc) {
- return { rev }
- }
- // Don't need to block for marking as updated
- ProjectUpdateHandler.promises
- .markAsUpdated(projectId, lastUpdatedAt, lastUpdatedBy)
- .catch(error => {
- logger.error({ error }, 'failed to mark project as updated')
- })
- await TpdsUpdateSender.promises.addDoc({
- projectId,
- path,
- docId,
- projectName,
- rev,
- folderId: folder?._id,
- })
- return { rev, modified }
- }
- async function setRootDoc(projectId, newRootDocID) {
- logger.debug({ projectId, rootDocId: newRootDocID }, 'setting root doc')
- if (projectId == null || newRootDocID == null) {
- throw new Errors.InvalidError('missing arguments (project or doc)')
- }
- const docPath =
- await ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
- projectId,
- newRootDocID
- )
- if (ProjectEntityUpdateHandler.isPathValidForRootDoc(docPath)) {
- await Project.updateOne(
- { _id: projectId },
- { rootDoc_id: newRootDocID }
- ).exec()
- } else {
- throw new Errors.UnsupportedFileTypeError(
- 'invalid file extension for root doc'
- )
- }
- }
- async function unsetRootDoc(projectId) {
- logger.debug({ projectId }, 'removing root doc')
- await Project.updateOne(
- { _id: projectId },
- { $unset: { rootDoc_id: true } }
- ).exec()
- }
- async function addDoc(projectId, folderId, docName, docLines, userId, source) {
- return await ProjectEntityUpdateHandler.promises.addDocWithRanges(
- projectId,
- folderId,
- docName,
- docLines,
- {},
- userId,
- source
- )
- }
- const addDocWithRanges = wrapWithLock({
- async beforeLock(
- projectId,
- folderId,
- docName,
- docLines,
- ranges,
- userId,
- source
- ) {
- if (!SafePath.isCleanFilename(docName)) {
- throw new Errors.InvalidNameError('invalid element name')
- }
- // Put doc in docstore first, so that if it errors, we don't have a doc_id in the project
- // which hasn't been created in docstore.
- const doc = new Doc({ name: docName })
- const { rev } = await DocstoreManager.promises.updateDoc(
- projectId.toString(),
- doc._id.toString(),
- docLines,
- 0,
- ranges
- )
- doc.rev = rev
- return {
- projectId,
- folderId,
- doc,
- docName,
- docLines,
- ranges,
- userId,
- source,
- }
- },
- async withLock({
- projectId,
- folderId,
- doc,
- docName,
- docLines,
- ranges,
- userId,
- source,
- }) {
- const { result, project } =
- await ProjectEntityUpdateHandler._addDocAndSendToTpds(
- projectId,
- folderId,
- doc,
- userId
- )
- const docPath = result?.path?.fileSystem
- const projectHistoryId = project?.overleaf?.history?.id
- const newDocs = [
- {
- doc,
- path: docPath,
- docLines: docLines.join('\n'),
- ranges,
- },
- ]
- await DocumentUpdaterHandler.promises.updateProjectStructure(
- projectId,
- projectHistoryId,
- userId,
- { newDocs, newProject: project },
- source
- )
- return { doc, folderId: folderId || project.rootFolder[0]._id }
- },
- })
- const addFile = wrapWithLock({
- async beforeLock(
- projectId,
- folderId,
- fileName,
- fsPath,
- linkedFileData,
- userId,
- source
- ) {
- if (!SafePath.isCleanFilename(fileName)) {
- throw new Errors.InvalidNameError('invalid element name')
- }
- const { url, fileRef, createdBlob } =
- await ProjectEntityUpdateHandler._uploadFile(
- projectId,
- folderId,
- fileName,
- fsPath,
- linkedFileData
- )
- return {
- projectId,
- folderId,
- userId,
- fileRef,
- fileStoreUrl: url,
- createdBlob,
- source,
- }
- },
- async withLock({
- projectId,
- folderId,
- userId,
- fileRef,
- fileStoreUrl,
- createdBlob,
- source,
- }) {
- const { result, project } =
- await ProjectEntityUpdateHandler._addFileAndSendToTpds(
- projectId,
- folderId,
- fileRef,
- userId
- )
- const projectHistoryId = project.overleaf?.history?.id
- const newFiles = [
- {
- createdBlob,
- file: fileRef,
- path: result && result.path && result.path.fileSystem,
- url: fileStoreUrl,
- },
- ]
- await DocumentUpdaterHandler.promises.updateProjectStructure(
- projectId,
- projectHistoryId,
- userId,
- { newFiles, newProject: project },
- source
- )
- return { fileRef, folderId, createdBlob }
- },
- })
- const upsertDoc = wrapWithLock(
- async function (projectId, folderId, docName, docLines, source, userId) {
- if (!SafePath.isCleanFilename(docName)) {
- throw new Errors.InvalidNameError('invalid element name')
- }
- let element, folderPath
- try {
- ;({ element, path: folderPath } =
- await ProjectLocator.promises.findElement({
- project_id: projectId,
- element_id: folderId,
- type: 'folder',
- }))
- } catch (error) {
- if (error instanceof Errors.NotFoundError) {
- throw new Error('folder_not_found')
- }
- throw error
- }
- if (element == null) {
- throw new Error("Couldn't find folder")
- }
- const existingDoc = element.docs.find(({ name }) => name === docName)
- const existingFile = element.fileRefs.find(({ name }) => name === docName)
- if (existingFile) {
- const doc = new Doc({ name: docName })
- const filePath = `${folderPath.fileSystem}/${existingFile.name}`
- const { rev } = await DocstoreManager.promises.updateDoc(
- projectId.toString(),
- doc._id.toString(),
- docLines,
- 0,
- {}
- )
- doc.rev = rev
- const project =
- await ProjectEntityMongoUpdateHandler.promises.replaceFileWithDoc(
- projectId,
- existingFile._id,
- doc,
- userId
- )
- await TpdsUpdateSender.promises.addDoc({
- projectId,
- docId: doc._id,
- path: filePath,
- projectName: project.name,
- rev: existingFile.rev + 1,
- folderId,
- })
- const projectHistoryId =
- project.overleaf &&
- project.overleaf.history &&
- project.overleaf.history.id
- const newDocs = [
- {
- doc,
- path: filePath,
- docLines: docLines.join('\n'),
- },
- ]
- const oldFiles = [
- {
- file: existingFile,
- path: filePath,
- },
- ]
- await DocumentUpdaterHandler.promises.updateProjectStructure(
- projectId,
- projectHistoryId,
- userId,
- { oldFiles, newDocs, newProject: project },
- source
- )
- EditorRealTimeController.emitToRoom(
- projectId,
- 'removeEntity',
- existingFile._id,
- 'convertFileToDoc'
- )
- return { doc, isNew: true }
- } else if (existingDoc) {
- const result = await DocumentUpdaterHandler.promises.setDocument(
- projectId,
- existingDoc._id,
- userId,
- docLines,
- source
- )
- logger.debug(
- { projectId, docId: existingDoc._id },
- 'notifying users that the document has been updated'
- )
- // there is no need to flush the doc to mongo at this point as docupdater
- // flushes it as part of setDoc.
- //
- // combine rev from response with existing doc metadata
- return {
- doc: { ...existingDoc, ...result },
- isNew: existingDoc == null,
- }
- } else {
- const { doc } =
- await ProjectEntityUpdateHandler.promises.addDocWithRanges.withoutLock(
- projectId,
- folderId,
- docName,
- docLines,
- {},
- userId,
- source
- )
- return { doc, isNew: existingDoc == null }
- }
- }
- )
- const appendToDoc = wrapWithLock(
- async (projectId, docId, lines, source, userId) => {
- const { element } = await ProjectLocator.promises.findElement({
- project_id: projectId,
- element_id: docId,
- type: 'doc',
- })
- return await DocumentUpdaterHandler.promises.appendToDocument(
- projectId,
- element._id,
- userId,
- lines,
- source
- )
- }
- )
- const upsertFile = wrapWithLock({
- async beforeLock(
- projectId,
- folderId,
- fileName,
- fsPath,
- linkedFileData,
- userId,
- source
- ) {
- if (!SafePath.isCleanFilename(fileName)) {
- throw new Errors.InvalidNameError('invalid element name')
- }
- // create a new file
- const fileArgs = {
- name: fileName,
- linkedFileData,
- }
- const { url, fileRef, createdBlob } =
- await FileStoreHandler.promises.uploadFileFromDisk(
- projectId,
- fileArgs,
- fsPath
- )
- return {
- projectId,
- folderId,
- fileName,
- fsPath,
- linkedFileData,
- userId,
- fileRef,
- fileStoreUrl: url,
- createdBlob,
- source,
- }
- },
- async withLock({
- projectId,
- folderId,
- fileName,
- userId,
- fileRef,
- fileStoreUrl,
- createdBlob,
- source,
- }) {
- let element
- try {
- ;({ element } = await ProjectLocator.promises.findElement({
- project_id: projectId,
- element_id: folderId,
- type: 'folder',
- }))
- } catch (error) {
- if (error instanceof Errors.NotFoundError) {
- throw new Error('folder_not_found')
- }
- throw error
- }
- if (element == null) {
- throw new Error("Couldn't find folder")
- }
- const existingFile = element.fileRefs.find(({ name }) => name === fileName)
- const existingDoc = element.docs.find(({ name }) => name === fileName)
- if (existingDoc) {
- let path
- try {
- ;({ path } = await ProjectLocator.promises.findElement({
- project_id: projectId,
- element_id: existingDoc._id,
- type: 'doc',
- }))
- } catch (err) {
- throw new Error("couldn't find existing file")
- }
- const project =
- await ProjectEntityMongoUpdateHandler.promises.replaceDocWithFile(
- projectId,
- existingDoc._id,
- fileRef,
- userId
- )
- const projectHistoryId = project.overleaf?.history?.id
- await TpdsUpdateSender.promises.addFile({
- projectId: project._id,
- historyId: projectHistoryId,
- fileId: fileRef._id,
- hash: fileRef.hash,
- path: path.fileSystem,
- rev: fileRef.rev,
- projectName: project.name,
- folderId,
- })
- await DocumentUpdaterHandler.promises.updateProjectStructure(
- projectId,
- projectHistoryId,
- userId,
- {
- oldDocs: [{ doc: existingDoc, path: path.fileSystem }],
- newFiles: [
- {
- createdBlob,
- file: fileRef,
- path: path.fileSystem,
- url: fileStoreUrl,
- },
- ],
- newProject: project,
- },
- source
- )
- EditorRealTimeController.emitToRoom(
- projectId,
- 'removeEntity',
- existingDoc._id,
- 'convertDocToFile'
- )
- return { fileRef, isNew: true, oldFileRef: existingFile }
- } else if (existingFile) {
- await ProjectEntityUpdateHandler._replaceFile(
- projectId,
- existingFile._id,
- userId,
- fileRef,
- fileStoreUrl,
- folderId,
- source,
- createdBlob
- )
- return { fileRef, isNew: false, oldFileRef: existingFile }
- } else {
- // this calls directly into the addFile main task (without the beforeLock part)
- await ProjectEntityUpdateHandler.promises.addFile.mainTask({
- projectId,
- folderId,
- userId,
- fileRef,
- fileStoreUrl,
- createdBlob,
- source,
- })
- return {
- fileRef,
- isNew: existingFile == null,
- oldFileRef: existingFile,
- }
- }
- },
- })
- const upsertDocWithPath = wrapWithLock(
- async function (projectId, elementPath, docLines, source, userId) {
- if (!SafePath.isCleanPath(elementPath)) {
- throw new Errors.InvalidNameError('invalid element name')
- }
- const docName = Path.basename(elementPath)
- const folderPath = Path.dirname(elementPath)
- const { newFolders, folder } =
- await ProjectEntityUpdateHandler.promises.mkdirp.withoutLock(
- projectId,
- folderPath,
- userId
- )
- const { isNew, doc } =
- await ProjectEntityUpdateHandler.promises.upsertDoc.withoutLock(
- projectId,
- folder._id,
- docName,
- docLines,
- source,
- userId
- )
- return { doc, isNew, newFolders, folder }
- }
- )
- const upsertFileWithPath = wrapWithLock({
- async beforeLock(
- projectId,
- elementPath,
- fsPath,
- linkedFileData,
- userId,
- source
- ) {
- if (!SafePath.isCleanPath(elementPath)) {
- throw new Errors.InvalidNameError('invalid element name')
- }
- const fileName = Path.basename(elementPath)
- const folderPath = Path.dirname(elementPath)
- // create a new file
- const fileArgs = {
- name: fileName,
- linkedFileData,
- }
- const {
- url: fileStoreUrl,
- fileRef,
- createdBlob,
- } = await FileStoreHandler.promises.uploadFileFromDisk(
- projectId,
- fileArgs,
- fsPath
- )
- return {
- projectId,
- folderPath,
- fileName,
- fsPath,
- linkedFileData,
- userId,
- fileRef,
- fileStoreUrl,
- createdBlob,
- source,
- }
- },
- async withLock({
- projectId,
- folderPath,
- fileName,
- fsPath,
- linkedFileData,
- userId,
- fileRef,
- fileStoreUrl,
- createdBlob,
- source,
- }) {
- const { newFolders, folder } =
- await ProjectEntityUpdateHandler.promises.mkdirp.withoutLock(
- projectId,
- folderPath,
- userId
- )
- // this calls directly into the upsertFile main task (without the beforeLock part)
- const {
- fileRef: newFileRef,
- isNew,
- oldFileRef,
- } = await ProjectEntityUpdateHandler.promises.upsertFile.mainTask({
- projectId,
- folderId: folder._id,
- fileName,
- fsPath,
- linkedFileData,
- userId,
- fileRef,
- fileStoreUrl,
- createdBlob,
- source,
- })
- return {
- fileRef: newFileRef,
- isNew,
- oldFileRef,
- newFolders,
- folder,
- }
- },
- })
- const deleteEntity = wrapWithLock(
- async function (projectId, entityId, entityType, userId, source, callback) {
- logger.debug({ entityId, entityType, projectId }, 'deleting project entity')
- if (entityType == null) {
- logger.warn({ err: 'No entityType set', projectId, entityId })
- throw new Error('No entityType set')
- }
- entityType = entityType.toLowerCase()
- // Flush the entire project to avoid leaving partially deleted docs in redis.
- await DocumentUpdaterHandler.promises.flushProjectToMongo(projectId)
- const { entity, path, projectBeforeDeletion, newProject } =
- await ProjectEntityMongoUpdateHandler.promises.deleteEntity(
- projectId,
- entityId,
- entityType,
- userId
- )
- const subtreeListing = await ProjectEntityUpdateHandler._cleanUpEntity(
- projectBeforeDeletion,
- newProject,
- entity,
- entityType,
- path.fileSystem,
- userId,
- source
- )
- const subtreeEntityIds = subtreeListing.map(entry =>
- entry.entity._id.toString()
- )
- await TpdsUpdateSender.promises.deleteEntity({
- projectId,
- path: path.fileSystem,
- projectName: projectBeforeDeletion.name,
- entityId,
- entityType,
- subtreeEntityIds,
- })
- return entityId
- }
- )
- const deleteEntityWithPath = wrapWithLock(
- async (projectId, path, userId, source) => {
- const { element, type } = await ProjectLocator.promises.findElementByPath({
- project_id: projectId,
- path,
- exactCaseMatch: true,
- })
- if (element == null) {
- throw new Errors.NotFoundError('project not found')
- }
- return await ProjectEntityUpdateHandler.promises.deleteEntity.withoutLock(
- projectId,
- element._id,
- type,
- userId,
- source
- )
- }
- )
- const mkdirp = wrapWithLock(async function (projectId, path, userId) {
- for (const folder of path.split('/')) {
- if (folder.length > 0 && !SafePath.isCleanFilename(folder)) {
- throw new Errors.InvalidNameError('invalid element name')
- }
- }
- return await ProjectEntityMongoUpdateHandler.promises.mkdirp(
- projectId,
- path,
- userId,
- { exactCaseMatch: false }
- )
- })
- const mkdirpWithExactCase = wrapWithLock(
- async function (projectId, path, userId) {
- for (const folder of path.split('/')) {
- if (folder.length > 0 && !SafePath.isCleanFilename(folder)) {
- throw new Errors.InvalidNameError('invalid element name')
- }
- }
- return await ProjectEntityMongoUpdateHandler.promises.mkdirp(
- projectId,
- path,
- userId,
- { exactCaseMatch: true }
- )
- }
- )
- const addFolder = wrapWithLock(
- async function (projectId, parentFolderId, folderName, userId) {
- if (!SafePath.isCleanFilename(folderName)) {
- throw new Errors.InvalidNameError('invalid element name')
- }
- return await ProjectEntityMongoUpdateHandler.promises.addFolder(
- projectId,
- parentFolderId,
- folderName,
- userId
- )
- }
- )
- const moveEntity = wrapWithLock(
- async function (
- projectId,
- entityId,
- destFolderId,
- entityType,
- userId,
- source
- ) {
- logger.debug(
- { entityType, entityId, projectId, destFolderId },
- 'moving entity'
- )
- if (entityType == null) {
- logger.warn({ err: 'No entityType set', projectId, entityId })
- throw new Error('No entityType set')
- }
- entityType = entityType.toLowerCase()
- await DocumentUpdaterHandler.promises.flushProjectToMongo(projectId)
- const { project, startPath, endPath, rev, changes } =
- await ProjectEntityMongoUpdateHandler.promises.moveEntity(
- projectId,
- entityId,
- destFolderId,
- entityType,
- userId
- )
- const projectHistoryId = project.overleaf?.history?.id
- try {
- await TpdsUpdateSender.promises.moveEntity({
- projectId,
- projectName: project.name,
- startPath,
- endPath,
- rev,
- entityId,
- entityType,
- folderId: destFolderId,
- })
- } catch (err) {
- logger.error({ err }, 'error sending tpds update')
- }
- return await DocumentUpdaterHandler.promises.updateProjectStructure(
- projectId,
- projectHistoryId,
- userId,
- changes,
- source
- )
- }
- )
- const renameEntity = wrapWithLock(
- async function (projectId, entityId, entityType, newName, userId, source) {
- if (!newName || typeof newName !== 'string') {
- const err = new OError('invalid newName value', {
- value: newName,
- type: typeof newName,
- projectId,
- entityId,
- entityType,
- userId,
- source,
- })
- logger.error({ err }, 'Invalid newName passed to renameEntity')
- throw err
- }
- if (!SafePath.isCleanFilename(newName)) {
- throw new Errors.InvalidNameError('invalid element name')
- }
- logger.debug({ entityId, projectId }, `renaming ${entityType}`)
- if (entityType == null) {
- logger.warn({ err: 'No entityType set', projectId, entityId })
- throw new Error('No entityType set')
- }
- entityType = entityType.toLowerCase()
- await DocumentUpdaterHandler.promises.flushProjectToMongo(projectId)
- const { project, startPath, endPath, rev, changes } =
- await ProjectEntityMongoUpdateHandler.promises.renameEntity(
- projectId,
- entityId,
- entityType,
- newName,
- userId
- )
- const projectHistoryId = project.overleaf?.history?.id
- try {
- await TpdsUpdateSender.promises.moveEntity({
- projectId,
- projectName: project.name,
- startPath,
- endPath,
- rev,
- entityId,
- entityType,
- folderId: null, // this means the folder has not changed
- })
- } catch (err) {
- logger.error({ err }, 'error sending tpds update')
- }
- return await DocumentUpdaterHandler.promises.updateProjectStructure(
- projectId,
- projectHistoryId,
- userId,
- changes,
- source
- )
- }
- )
- // This doesn't directly update project structure, but we need to take the lock
- // to prevent anything else being queued before the resync update
- const resyncProjectHistory = wrapWithLock(
- async (projectId, opts) => {
- const project = await ProjectGetter.promises.getProject(projectId, {
- rootFolder: true,
- overleaf: true,
- })
- const projectHistoryId = project.overleaf?.history?.id
- if (projectHistoryId == null) {
- throw new Errors.ProjectHistoryDisabledError(
- `project history not enabled for ${projectId}`
- )
- }
- const { docs, files, folders } =
- ProjectEntityHandler.getAllEntitiesFromProject(project)
- // _checkFileTree() must be passed the folders before docs and
- // files
- await ProjectEntityUpdateHandler._checkFiletree(
- projectId,
- projectHistoryId,
- [...folders, ...docs, ...files]
- )
- await DocumentUpdaterHandler.promises.resyncProjectHistory(
- projectId,
- projectHistoryId,
- docs,
- files,
- opts
- )
- if (opts.historyRangesMigration) {
- return await ProjectOptionsHandler.promises.setHistoryRangesSupport(
- projectId,
- opts.historyRangesMigration === 'forwards'
- )
- }
- },
- LockManager.withTimeout(6 * 60) // use an extended lock for the resync operations
- )
- const convertDocToFile = wrapWithLock({
- async beforeLock(projectId, docId, userId, source) {
- await DocumentUpdaterHandler.promises.flushDocToMongo(projectId, docId)
- const { element: doc, path } = await ProjectLocator.promises.findElement({
- project_id: projectId,
- element_id: docId,
- type: 'doc',
- })
- const docPath = path.fileSystem
- const { lines, rev, ranges } = await DocstoreManager.promises.getDoc(
- projectId,
- docId
- )
- if (!_.isEmpty(ranges)) {
- throw new Errors.DocHasRangesError({})
- }
- await DocumentUpdaterHandler.promises.deleteDoc(projectId, docId, false)
- const fsPath = await FileWriter.promises.writeLinesToDisk(projectId, lines)
- const {
- url: fileStoreUrl,
- fileRef,
- createdBlob,
- } = await FileStoreHandler.promises.uploadFileFromDisk(
- projectId,
- { name: doc.name, rev: rev + 1 },
- fsPath
- )
- try {
- await fs.promises.unlink(fsPath)
- } catch (err) {
- logger.warn({ err, path: fsPath }, 'failed to clean up temporary file')
- }
- return {
- projectId,
- doc,
- path: docPath,
- fileRef,
- fileStoreUrl,
- userId,
- source,
- createdBlob,
- }
- },
- async withLock({
- projectId,
- doc,
- path,
- fileRef,
- fileStoreUrl,
- userId,
- source,
- createdBlob,
- }) {
- const project =
- await ProjectEntityMongoUpdateHandler.promises.replaceDocWithFile(
- projectId,
- doc._id,
- fileRef,
- userId
- )
- const projectHistoryId = project.overleaf?.history?.id
- await DocumentUpdaterHandler.promises.updateProjectStructure(
- projectId,
- projectHistoryId,
- userId,
- {
- oldDocs: [{ doc, path }],
- newFiles: [{ file: fileRef, path, url: fileStoreUrl, createdBlob }],
- newProject: project,
- },
- source
- )
- const { folder } = await ProjectLocator.promises.findElement({
- project_id: projectId,
- element_id: fileRef._id,
- type: 'file',
- })
- EditorRealTimeController.emitToRoom(
- projectId,
- 'removeEntity',
- doc._id,
- 'convertDocToFile'
- )
- EditorRealTimeController.emitToRoom(
- projectId,
- 'reciveNewFile',
- folder._id,
- fileRef,
- 'convertDocToFile',
- null,
- userId
- )
- return fileRef
- },
- })
- async function setMainBibliographyDoc(projectId, newBibliographyDocId) {
- logger.debug(
- { projectId, mainBibliographyDocId: newBibliographyDocId },
- 'setting main bibliography doc'
- )
- if (projectId == null || newBibliographyDocId == null) {
- throw new Errors.InvalidError('missing arguments (project or doc)')
- }
- const docPath =
- await ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
- projectId,
- newBibliographyDocId
- )
- if (ProjectEntityUpdateHandler.isPathValidForMainBibliographyDoc(docPath)) {
- await Project.updateOne(
- { _id: projectId },
- { mainBibliographyDoc_id: newBibliographyDocId }
- ).exec()
- } else {
- throw new Errors.UnsupportedFileTypeError(
- 'invalid file extension for main bibliography doc'
- )
- }
- }
- const ProjectEntityUpdateHandler = {
- LOCK_NAMESPACE,
- addDoc: callbackifyMultiResult(addDoc, ['doc', 'folderId']),
- addDocWithRanges: callbackifyMultiResult(addDocWithRanges, [
- 'doc',
- 'folderId',
- ]),
- addFile: callbackifyMultiResult(addFile, [
- 'fileRef',
- 'folderId',
- 'createdBlob',
- ]),
- addFolder: callbackifyMultiResult(addFolder, ['folder', 'parentFolderId']),
- convertDocToFile: callbackify(convertDocToFile),
- deleteEntity: callbackify(deleteEntity),
- deleteEntityWithPath: callbackify(deleteEntityWithPath),
- mkdirp: callbackifyMultiResult(mkdirp, [
- 'newFolders',
- 'folder',
- 'parentFolder',
- ]),
- mkdirpWithExactCase: callbackifyMultiResult(mkdirpWithExactCase, [
- 'newFolders',
- 'folder',
- 'parentFolder',
- ]),
- moveEntity: callbackify(moveEntity),
- renameEntity: callbackify(renameEntity),
- resyncProjectHistory: callbackify(resyncProjectHistory),
- setRootDoc: callbackify(setRootDoc),
- unsetRootDoc: callbackify(unsetRootDoc),
- setMainBibliographyDoc: callbackify(setMainBibliographyDoc),
- updateDocLines: callbackify(updateDocLines),
- upsertDoc: callbackifyMultiResult(upsertDoc, ['doc', 'isNew']),
- appendToDoc: callbackify(appendToDoc),
- upsertDocWithPath: callbackifyMultiResult(upsertDocWithPath, [
- 'doc',
- 'isNew',
- 'newFolders',
- 'folder',
- ]),
- upsertFile: callbackifyMultiResult(upsertFile, [
- 'fileRef',
- 'isNew',
- 'oldFileRef',
- ]),
- upsertFileWithPath: callbackifyMultiResult(upsertFileWithPath, [
- 'fileRef',
- 'isNew',
- 'oldFileRef',
- 'newFolders',
- 'folder',
- ]),
- promises: {
- addDoc,
- addDocWithRanges,
- addFile,
- addFolder,
- convertDocToFile,
- deleteEntity,
- deleteEntityWithPath,
- mkdirp,
- mkdirpWithExactCase,
- moveEntity,
- renameEntity,
- resyncProjectHistory,
- setRootDoc,
- unsetRootDoc,
- updateDocLines,
- upsertDoc,
- upsertDocWithPath,
- upsertFile,
- upsertFileWithPath,
- appendToDocWithPath: appendToDoc,
- setMainBibliographyDoc,
- },
- async _addDocAndSendToTpds(projectId, folderId, doc, userId) {
- let result, project
- try {
- ;({ result, project } =
- await ProjectEntityMongoUpdateHandler.promises.addDoc(
- projectId,
- folderId,
- doc,
- userId
- ))
- } catch (err) {
- throw OError.tag(err, 'error adding file with project', {
- projectId,
- folderId,
- doc_name: doc != null ? doc.name : undefined,
- doc_id: doc != null ? doc._id : undefined,
- })
- }
- await TpdsUpdateSender.promises.addDoc({
- projectId,
- docId: doc != null ? doc._id : undefined,
- path: result?.path?.fileSystem,
- projectName: project.name,
- rev: 0,
- folderId,
- })
- return { result, project }
- },
- async _uploadFile(projectId, folderId, fileName, fsPath, linkedFileData) {
- if (!SafePath.isCleanFilename(fileName)) {
- throw new Errors.InvalidNameError('invalid element name')
- }
- const fileArgs = {
- name: fileName,
- linkedFileData,
- }
- try {
- return await FileStoreHandler.promises.uploadFileFromDisk(
- projectId,
- fileArgs,
- fsPath
- )
- } catch (err) {
- throw OError.tag(err, 'error uploading image to s3', {
- projectId,
- folderId,
- file_name: fileName,
- })
- }
- },
- async _addFileAndSendToTpds(projectId, folderId, fileRef, userId) {
- let result, project
- try {
- ;({ result, project } =
- await ProjectEntityMongoUpdateHandler.promises.addFile(
- projectId,
- folderId,
- fileRef,
- userId
- ))
- } catch (err) {
- throw OError.tag(err, 'error adding file with project', {
- projectId,
- folderId,
- file_name: fileRef.name,
- fileRef,
- })
- }
- const historyId = project?.overleaf?.history?.id
- if (!historyId) {
- throw new OError('project does not have a history id', { projectId })
- }
- await TpdsUpdateSender.promises.addFile({
- projectId,
- historyId,
- fileId: fileRef._id,
- hash: fileRef.hash,
- path: result?.path?.fileSystem,
- projectName: project.name,
- rev: fileRef.rev,
- folderId,
- })
- return { result, project }
- },
- async _replaceFile(
- projectId,
- fileId,
- userId,
- newFileRef,
- fileStoreUrl,
- folderId,
- source,
- createdBlob
- ) {
- const {
- oldFileRef,
- project,
- path,
- newProject,
- newFileRef: updatedFileRef,
- } = await ProjectEntityMongoUpdateHandler.promises.replaceFileWithNew(
- projectId,
- fileId,
- newFileRef,
- userId
- )
- const oldFiles = [
- {
- file: oldFileRef,
- path: path.fileSystem,
- },
- ]
- const newFiles = [
- {
- file: updatedFileRef,
- createdBlob,
- path: path.fileSystem,
- url: fileStoreUrl,
- },
- ]
- const projectHistoryId = project.overleaf?.history?.id
- await TpdsUpdateSender.promises.addFile({
- projectId: project._id,
- historyId: projectHistoryId,
- fileId: updatedFileRef._id,
- hash: updatedFileRef.hash,
- path: path.fileSystem,
- rev: updatedFileRef.rev,
- projectName: project.name,
- folderId,
- })
- await DocumentUpdaterHandler.promises.updateProjectStructure(
- projectId,
- projectHistoryId,
- userId,
- { oldFiles, newFiles, newProject },
- source
- )
- return updatedFileRef
- },
- async _checkFiletree(projectId, projectHistoryId, entities) {
- const adjustPathsAfterFolderRename = (oldPath, newPath) => {
- oldPath = oldPath + '/'
- newPath = newPath + '/'
- for (const entity of entities) {
- if (entity.path.startsWith(oldPath)) {
- entity.path = newPath + entity.path.slice(oldPath.length)
- }
- }
- }
- // Data structures for recording pending renames
- const renames = []
- const paths = new Set()
- for (const entity of entities) {
- const originalName = entity.folder
- ? entity.folder.name
- : entity.doc
- ? entity.doc.name
- : entity.file.name
- let newPath = entity.path
- let newName = originalName
- // Clean the filename if necessary
- if (newName === '') {
- newName = 'untitled'
- } else {
- newName = SafePath.clean(newName)
- }
- if (newName !== originalName) {
- newPath = Path.join(
- newPath.slice(0, newPath.length - originalName.length),
- newName
- )
- }
- // Check if we've seen that path already
- if (paths.has(newPath)) {
- newPath = ProjectEntityUpdateHandler.findNextAvailablePath(
- paths,
- newPath
- )
- newName = newPath.split('/').pop()
- }
- // If we've changed the filename, schedule a rename
- if (newName !== originalName) {
- renames.push({ entity, newName, newPath })
- if (entity.folder) {
- // Here, we rely on entities being processed in the right order.
- // Parent folders need to be processed before their children. This is
- // the case only because getAllEntitiesFromProject() returns folders
- // in that order and resyncProjectHistory() calls us with the folders
- // first.
- adjustPathsAfterFolderRename(entity.path, newPath)
- }
- }
- // Remember that we've seen this path
- paths.add(newPath)
- }
- if (renames.length === 0) {
- return
- }
- logger.warn(
- {
- projectId,
- renames: renames.map(rename => ({
- oldPath: rename.entity.path,
- newPath: rename.newPath,
- })),
- },
- 'found conflicts or bad filenames in filetree'
- )
- // Avoid conflicts by processing renames in the reverse order. If we have
- // the following starting situation:
- //
- // somefile.tex
- // somefile.tex
- // somefile.tex (1)
- //
- // somefile.tex would be processed first, and then somefile.tex (1),
- // yielding the following renames:
- //
- // somefile.tex -> somefile.tex (1)
- // somefile.tex (1) -> somefile.tex (2)
- //
- // When the first rename was decided, we didn't know that somefile.tex (1)
- // existed, so that created a conflict. By processing renames in the
- // reverse order, we start with the files that had the most extensive
- // information about existing files.
- renames.reverse()
- for (const rename of renames) {
- // rename the duplicate files
- const entity = rename.entity
- const entityId = entity.folder
- ? entity.folder._id
- : entity.doc
- ? entity.doc._id
- : entity.file._id
- const entityType = entity.folder ? 'folder' : entity.doc ? 'doc' : 'file'
- const { changes } =
- await ProjectEntityMongoUpdateHandler.promises.renameEntity(
- projectId,
- entityId,
- entityType,
- rename.newName,
- null // unset lastUpdatedBy
- )
- // update the renamed entity for the resync
- entity.path = rename.newPath
- if (entityType === 'folder') {
- entity.folder.name = rename.newName
- } else if (entityType === 'doc') {
- entity.doc.name = rename.newName
- } else {
- entity.file.name = rename.newName
- }
- await DocumentUpdaterHandler.promises.updateProjectStructure(
- projectId,
- projectHistoryId,
- null,
- changes,
- 'automatic-fix'
- )
- }
- },
- findNextAvailablePath(allPaths, candidatePath) {
- const incrementReplacer = (match, p1) => {
- return ' (' + (parseInt(p1, 10) + 1) + ')'
- }
- // if the filename was invalid we should normalise it here too. Currently
- // this only handles renames in the same folder, so we will be out of luck
- // if it is the folder name which in invalid. We could handle folder
- // renames by returning the folders list from getAllEntitiesFromProject
- do {
- // does the filename look like "foo (1)" if so, increment the number in parentheses
- if (/ \(\d+\)$/.test(candidatePath)) {
- candidatePath = candidatePath.replace(/ \((\d+)\)$/, incrementReplacer)
- } else {
- // otherwise, add a ' (1)' suffix to the name
- candidatePath = candidatePath + ' (1)'
- }
- } while (allPaths.has(candidatePath)) // keep going until the name is unique
- // add the new name to the set
- allPaths.add(candidatePath)
- return candidatePath
- },
- isPathValidForRootDoc(docPath) {
- const docExtension = Path.extname(docPath)
- return VALID_ROOT_DOC_REGEXP.test(docExtension)
- },
- isPathValidForMainBibliographyDoc(docPath) {
- const docExtension = Path.extname(docPath).toLowerCase()
- return docExtension === '.bib'
- },
- async _cleanUpEntity(
- project,
- newProject,
- entity,
- entityType,
- path,
- userId,
- source
- ) {
- const subtreeListing = _listSubtree(entity, entityType, path)
- await ProjectEntityUpdateHandler._updateProjectStructureWithDeletedEntity(
- project,
- newProject,
- subtreeListing,
- userId,
- source
- )
- for (const entry of subtreeListing) {
- if (entry.type === 'doc') {
- await ProjectEntityUpdateHandler._cleanUpDoc(
- project,
- entry.entity,
- entry.path,
- userId
- )
- } else if (entry.type === 'file') {
- await ProjectEntityUpdateHandler._cleanUpFile(project, entry.entity)
- }
- }
- return subtreeListing
- },
- async _updateProjectStructureWithDeletedEntity(
- project,
- newProject,
- subtreeListing,
- userId,
- source
- ) {
- const changes = { oldDocs: [], oldFiles: [] }
- for (const entry of subtreeListing) {
- if (entry.type === 'doc') {
- changes.oldDocs.push({ doc: entry.entity, path: entry.path })
- } else if (entry.type === 'file') {
- changes.oldFiles.push({ file: entry.entity, path: entry.path })
- }
- }
- // now send the project structure changes to the docupdater
- changes.newProject = newProject
- const projectId = project._id.toString()
- const projectHistoryId =
- project.overleaf &&
- project.overleaf.history &&
- project.overleaf.history.id
- return await DocumentUpdaterHandler.promises.updateProjectStructure(
- projectId,
- projectHistoryId,
- userId,
- changes,
- source
- )
- },
- async _cleanUpDoc(project, doc) {
- const projectId = project._id.toString()
- const docId = doc._id.toString()
- if (project.rootDoc_id != null && project.rootDoc_id.toString() === docId) {
- await ProjectEntityUpdateHandler.promises.unsetRootDoc(projectId)
- }
- const { name } = doc
- const deletedAt = new Date()
- await DocstoreManager.promises.deleteDoc(projectId, docId, name, deletedAt)
- return await DocumentUpdaterHandler.promises.deleteDoc(projectId, docId)
- },
- async _cleanUpFile(project, file) {
- return await ProjectEntityMongoUpdateHandler.promises._insertDeletedFileReference(
- project._id,
- file
- )
- },
- }
- /**
- * List all descendants of an entity along with their type and path. Include
- * the top-level entity as well.
- */
- function _listSubtree(entity, entityType, entityPath) {
- if (entityType.indexOf('file') !== -1) {
- return [{ type: 'file', entity, path: entityPath }]
- } else if (entityType.indexOf('doc') !== -1) {
- return [{ type: 'doc', entity, path: entityPath }]
- } else if (entityType.indexOf('folder') !== -1) {
- const listing = []
- const _recurseFolder = (folder, folderPath) => {
- listing.push({ type: 'folder', entity: folder, path: folderPath })
- for (const doc of iterablePaths(folder, 'docs')) {
- listing.push({
- type: 'doc',
- entity: doc,
- path: Path.join(folderPath, doc.name),
- })
- }
- for (const file of iterablePaths(folder, 'fileRefs')) {
- listing.push({
- type: 'file',
- entity: file,
- path: Path.join(folderPath, file.name),
- })
- }
- for (const childFolder of iterablePaths(folder, 'folders')) {
- _recurseFolder(childFolder, Path.join(folderPath, childFolder.name))
- }
- }
- _recurseFolder(entity, entityPath)
- return listing
- } else {
- // This shouldn't happen, but if it does, fail silently.
- return []
- }
- }
- module.exports = ProjectEntityUpdateHandler
|