Jelajahi Sumber

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
andrew rumble 2 tahun lalu
induk
melakukan
d4824b9c5f
1 mengubah file dengan 20 tambahan dan 21 penghapusan
  1. 20 21
      services/clsi/app/js/OutputFileArchiveManager.js

+ 20 - 21
services/clsi/app/js/OutputFileArchiveManager.js

@@ -3,7 +3,6 @@ const OutputCacheManager = require('./OutputCacheManager')
 const OutputFileFinder = require('./OutputFileFinder')
 const OutputFileFinder = require('./OutputFileFinder')
 const Settings = require('@overleaf/settings')
 const Settings = require('@overleaf/settings')
 const { open } = require('node:fs/promises')
 const { open } = require('node:fs/promises')
-const path = require('path')
 const { NotFoundError } = require('./Errors')
 const { NotFoundError } = require('./Errors')
 const logger = require('@overleaf/logger')
 const logger = require('@overleaf/logger')
 
 
@@ -25,7 +24,14 @@ module.exports = {
   async archiveFilesForBuild(projectId, userId, build) {
   async archiveFilesForBuild(projectId, userId, build) {
     logger.debug({ projectId, userId, build }, 'Will create zip file')
     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')
     const archive = archiver('zip')
 
 
@@ -35,23 +41,22 @@ module.exports = {
 
 
     const missingFiles = []
     const missingFiles = []
 
 
-    for (const file of outputFiles) {
+    for (const { path } of outputFiles) {
       let fileHandle
       let fileHandle
-
       try {
       try {
-        fileHandle = await open(file, 'r')
+        fileHandle = await open(
+          `${contentDir}${OutputCacheManager.path(build, path)}`
+        )
       } catch (error) {
       } catch (error) {
         logger.warn(
         logger.warn(
-          { file, error },
+          { path, error, projectId, userId, build },
           'error opening file to add to output files archive'
           'error opening file to add to output files archive'
         )
         )
-        missingFiles.push(file)
+        missingFiles.push(path)
         continue
         continue
       }
       }
       const fileStream = fileHandle.createReadStream()
       const fileStream = fileHandle.createReadStream()
-      archive.append(fileStream, {
-        name: path.basename(file),
-      })
+      archive.append(fileStream, { name: path })
     }
     }
 
 
     if (missingFiles.length > 0) {
     if (missingFiles.length > 0) {
@@ -65,23 +70,17 @@ module.exports = {
     return archive
     return archive
   },
   },
 
 
-  async _getAllOutputFiles(projectId, userId, build) {
-    const contentDir = getContentDir(projectId, userId)
-
+  async _getAllOutputFiles(contentDir, projectId, userId, build) {
     try {
     try {
       const { outputFiles } = await OutputFileFinder.promises.findOutputFiles(
       const { outputFiles } = await OutputFileFinder.promises.findOutputFiles(
         [],
         [],
         `${contentDir}${OutputCacheManager.path(build, '.')}`
         `${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) {
     } catch (error) {
       if (
       if (
         error.code === 'ENOENT' ||
         error.code === 'ENOENT' ||