2024-05-01 09:52:12 -04:00
|
|
|
const archiver = require('archiver')
|
|
|
|
const OutputCacheManager = require('./OutputCacheManager')
|
|
|
|
const OutputFileFinder = require('./OutputFileFinder')
|
|
|
|
const Settings = require('@overleaf/settings')
|
2024-06-13 05:13:22 -04:00
|
|
|
const { open } = require('node:fs/promises')
|
2024-05-01 09:52:12 -04:00
|
|
|
const { NotFoundError } = require('./Errors')
|
2024-06-18 04:17:15 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
2024-05-01 09:52:12 -04:00
|
|
|
|
2024-06-12 04:28:34 -04:00
|
|
|
// NOTE: Updating this list requires a corresponding change in
|
|
|
|
// * services/web/frontend/js/features/pdf-preview/util/file-list.js
|
|
|
|
const ignoreFiles = ['output.fls', 'output.fdb_latexmk']
|
|
|
|
|
2024-05-01 09:52:12 -04:00
|
|
|
function getContentDir(projectId, userId) {
|
|
|
|
let subDir
|
|
|
|
if (userId != null) {
|
|
|
|
subDir = `${projectId}-${userId}`
|
|
|
|
} else {
|
|
|
|
subDir = projectId
|
|
|
|
}
|
|
|
|
return `${Settings.path.outputDir}/${subDir}/`
|
|
|
|
}
|
|
|
|
|
2024-06-12 04:27:13 -04:00
|
|
|
module.exports = {
|
|
|
|
async archiveFilesForBuild(projectId, userId, build) {
|
2024-06-18 04:17:15 -04:00
|
|
|
logger.debug({ projectId, userId, build }, 'Will create zip file')
|
2024-06-18 04:19:29 -04:00
|
|
|
|
2024-09-04 07:19:34 -04:00
|
|
|
const contentDir = getContentDir(projectId, userId)
|
|
|
|
|
|
|
|
const outputFiles = await this._getAllOutputFiles(
|
|
|
|
contentDir,
|
|
|
|
projectId,
|
|
|
|
userId,
|
|
|
|
build
|
|
|
|
)
|
2024-05-01 09:52:12 -04:00
|
|
|
|
|
|
|
const archive = archiver('zip')
|
|
|
|
|
2024-06-18 04:17:15 -04:00
|
|
|
archive.on('error', err => {
|
2024-09-04 07:24:05 -04:00
|
|
|
logger.warn(
|
|
|
|
{ err, projectId, userId, build },
|
|
|
|
'error emitted when creating output files archive'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
archive.on('warning', err => {
|
|
|
|
logger.warn(
|
|
|
|
{ err, projectId, userId, build },
|
|
|
|
'warning emitted when creating output files archive'
|
|
|
|
)
|
2024-06-18 04:17:15 -04:00
|
|
|
})
|
2024-06-18 04:19:29 -04:00
|
|
|
|
2024-06-12 04:27:13 -04:00
|
|
|
const missingFiles = []
|
2024-05-01 09:52:12 -04:00
|
|
|
|
2024-09-04 07:19:34 -04:00
|
|
|
for (const { path } of outputFiles) {
|
2024-06-18 04:19:08 -04:00
|
|
|
let fileHandle
|
2024-05-01 09:52:12 -04:00
|
|
|
try {
|
2024-09-04 07:19:34 -04:00
|
|
|
fileHandle = await open(
|
|
|
|
`${contentDir}${OutputCacheManager.path(build, path)}`
|
|
|
|
)
|
2024-05-01 09:52:12 -04:00
|
|
|
} catch (error) {
|
2024-06-18 04:19:08 -04:00
|
|
|
logger.warn(
|
2024-09-04 07:19:34 -04:00
|
|
|
{ path, error, projectId, userId, build },
|
2024-06-18 04:19:08 -04:00
|
|
|
'error opening file to add to output files archive'
|
|
|
|
)
|
2024-09-04 07:19:34 -04:00
|
|
|
missingFiles.push(path)
|
2024-06-18 04:19:08 -04:00
|
|
|
continue
|
2024-05-01 09:52:12 -04:00
|
|
|
}
|
2024-06-18 04:19:08 -04:00
|
|
|
const fileStream = fileHandle.createReadStream()
|
2024-09-04 07:19:34 -04:00
|
|
|
archive.append(fileStream, { name: path })
|
2024-05-01 09:52:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (missingFiles.length > 0) {
|
|
|
|
archive.append(missingFiles.join('\n'), {
|
|
|
|
name: 'missing_files.txt',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-04 07:23:27 -04:00
|
|
|
archive.finalize().catch(error => {
|
|
|
|
logger.error(
|
|
|
|
{ error, projectId, userId, build },
|
|
|
|
'error finalizing output files archive'
|
|
|
|
)
|
|
|
|
})
|
2024-05-01 09:52:12 -04:00
|
|
|
|
|
|
|
return archive
|
|
|
|
},
|
|
|
|
|
2024-09-04 07:19:34 -04:00
|
|
|
async _getAllOutputFiles(contentDir, projectId, userId, build) {
|
2024-05-01 09:52:12 -04:00
|
|
|
try {
|
|
|
|
const { outputFiles } = await OutputFileFinder.promises.findOutputFiles(
|
|
|
|
[],
|
|
|
|
`${contentDir}${OutputCacheManager.path(build, '.')}`
|
|
|
|
)
|
|
|
|
|
2024-09-04 07:19:34 -04:00
|
|
|
return outputFiles.filter(
|
|
|
|
// Ignore the pdf and also ignore the files ignored by the frontend.
|
|
|
|
({ path }) => path !== 'output.pdf' && !ignoreFiles.includes(path)
|
|
|
|
)
|
2024-05-01 09:52:12 -04:00
|
|
|
} catch (error) {
|
|
|
|
if (
|
|
|
|
error.code === 'ENOENT' ||
|
|
|
|
error.code === 'ENOTDIR' ||
|
|
|
|
error.code === 'EACCES'
|
|
|
|
) {
|
|
|
|
throw new NotFoundError('Output files not found')
|
|
|
|
}
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|