From 5ba7a3fee5a5553e74a5ca56e88fd0da1e9ba406 Mon Sep 17 00:00:00 2001 From: mikiher Date: Thu, 3 Dec 2026 05:29:22 +0200 Subject: [PATCH] Support custom entry names and abort on disconnect in zipDirectoriesPipe --- server/utils/zipHelpers.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/server/utils/zipHelpers.js b/server/utils/zipHelpers.js index 421ca73cd..b9acedca7 100644 --- a/server/utils/zipHelpers.js +++ b/server/utils/zipHelpers.js @@ -54,9 +54,9 @@ module.exports.zipDirectoryPipe = (path, filename, res) => { } /** - * Creates a zip archive containing multiple directories and streams it to the response. + * Creates a zip archive containing multiple directories or files and streams it to the response. * - * @param {{ path: string, isFile: boolean }[]} pathObjects + * @param {{ path: string, isFile: boolean, name?: string }[]} pathObjects * @param {string} filename - Name of the zip file to be sent as attachment. * @param {Response} res - Response object to pipe the archive data to. * @returns {Promise} - Promise that resolves when the zip operation completes. @@ -73,6 +73,9 @@ module.exports.zipDirectoriesPipe = (pathObjects, filename, res) => { // listen for all archive data to be written // 'close' event is fired only when a file descriptor is involved res.on('close', () => { + if (!res.writableEnded) { + archive.abort() + } Logger.info(archive.pointer() + ' total bytes') Logger.debug('archiver has been finalized and the output file descriptor has closed.') resolve() @@ -105,13 +108,14 @@ module.exports.zipDirectoriesPipe = (pathObjects, filename, res) => { // pipe archive data to the file archive.pipe(res) - // Add each path as a directory in the zip + // Add each path as a directory or file in the zip pathObjects.forEach((pathObject) => { + const entryName = pathObject.name || Path.basename(pathObject.path) if (!pathObject.isFile) { // Add the directory to the archive with its name as the root folder - archive.directory(pathObject.path, Path.basename(pathObject.path)) + archive.directory(pathObject.path, entryName) } else { - archive.file(pathObject.path, { name: Path.basename(pathObject.path) }) + archive.file(pathObject.path, { name: entryName }) } })