From d4824b9c5f875a9bdd7f820a834ca6171e3530e4 Mon Sep 17 00:00:00 2001 From: andrew rumble Date: Wed, 4 Sep 2024 12:19:34 +0100 Subject: [PATCH] Get full path only when adding file to archive This allows us to: * Get files that are in subdirectories (not known to happen but might as well support) * Removes need to sanitise missing_files output GitOrigin-RevId: 4b27b7f1b506578a6d713ea23979aa9370dfdcb0 --- .../clsi/app/js/OutputFileArchiveManager.js | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/services/clsi/app/js/OutputFileArchiveManager.js b/services/clsi/app/js/OutputFileArchiveManager.js index a92a029451..4227ecb7c7 100644 --- a/services/clsi/app/js/OutputFileArchiveManager.js +++ b/services/clsi/app/js/OutputFileArchiveManager.js @@ -3,7 +3,6 @@ const OutputCacheManager = require('./OutputCacheManager') const OutputFileFinder = require('./OutputFileFinder') const Settings = require('@overleaf/settings') const { open } = require('node:fs/promises') -const path = require('path') const { NotFoundError } = require('./Errors') const logger = require('@overleaf/logger') @@ -25,7 +24,14 @@ module.exports = { async archiveFilesForBuild(projectId, userId, build) { logger.debug({ projectId, userId, build }, 'Will create zip file') - const outputFiles = await this._getAllOutputFiles(projectId, userId, build) + const contentDir = getContentDir(projectId, userId) + + const outputFiles = await this._getAllOutputFiles( + contentDir, + projectId, + userId, + build + ) const archive = archiver('zip') @@ -35,23 +41,22 @@ module.exports = { const missingFiles = [] - for (const file of outputFiles) { + for (const { path } of outputFiles) { let fileHandle - try { - fileHandle = await open(file, 'r') + fileHandle = await open( + `${contentDir}${OutputCacheManager.path(build, path)}` + ) } catch (error) { logger.warn( - { file, error }, + { path, error, projectId, userId, build }, 'error opening file to add to output files archive' ) - missingFiles.push(file) + missingFiles.push(path) continue } const fileStream = fileHandle.createReadStream() - archive.append(fileStream, { - name: path.basename(file), - }) + archive.append(fileStream, { name: path }) } if (missingFiles.length > 0) { @@ -65,23 +70,17 @@ module.exports = { return archive }, - async _getAllOutputFiles(projectId, userId, build) { - const contentDir = getContentDir(projectId, userId) - + async _getAllOutputFiles(contentDir, projectId, userId, build) { try { const { outputFiles } = await OutputFileFinder.promises.findOutputFiles( [], `${contentDir}${OutputCacheManager.path(build, '.')}` ) - return outputFiles - .filter( - // Ignore the pdf and also ignore the files ignored by the frontend. - ({ path }) => path !== 'output.pdf' && !ignoreFiles.includes(path) - ) - .map( - ({ path }) => `${contentDir}${OutputCacheManager.path(build, path)}` - ) + return outputFiles.filter( + // Ignore the pdf and also ignore the files ignored by the frontend. + ({ path }) => path !== 'output.pdf' && !ignoreFiles.includes(path) + ) } catch (error) { if ( error.code === 'ENOENT' ||