This commit is contained in:
Vito0912 2025-03-17 20:04:01 +01:00
parent 9b79aab4d5
commit 3c9966e849
No known key found for this signature in database
GPG Key ID: 29A3D509FE70B237
2 changed files with 25 additions and 14 deletions

View File

@ -1433,8 +1433,8 @@ class LibraryController {
return res.sendStatus(403)
}
if(req.query.ids === undefined) {
res.status(400).send('Library not found')
if(req.query.ids === undefined || req.query.ids === '') {
res.status(400).send('Library items not found')
}
const itemIds = req.query.ids.split(',')
@ -1453,23 +1453,17 @@ class LibraryController {
const filename = `LibraryItems-${Date.now()}.zip`
const libraryItemPaths = libraryItems.map((li) => li.path)
if (!libraryItemPaths.length) {
Logger.warn(`[LibraryItemController] No library items found for ids "${itemIds}"`)
return res.status(404).send('Library items not found')
}
try {
await zipHelpers.zipDirectoriesPipe(libraryItemPaths, filename, res)
Logger.info(`[LibraryItemController] Downloaded item "${filename}" at "${libraryItemPaths}"`)
} catch (error) {
Logger.error(`[LibraryItemController] Download failed for item "${filename}" at "${libraryItemPaths}"`, error)
LibraryController.handleDownloadError(error, res)
}
}
static handleDownloadError(error, res) {
if (!res.headersSent) {
if (error.code === 'ENOENT') {
return res.status(404).send('File not found')
} else {
return res.status(500).send('Download failed')
}
zipHelpers.handleDownloadError(error, res)
}
}

View File

@ -123,3 +123,20 @@ module.exports.zipDirectoriesPipe = (paths, filename, res) => {
archive.finalize()
})
}
/**
* Handles errors that occur during the download process.
*
* @param error
* @param res
* @returns {*}
*/
module.exports.handleDownloadError = (error, res) => {
if (!res.headersSent) {
if (error.code === 'ENOENT') {
return res.status(404).send('File not found')
} else {
return res.status(500).send('Download failed')
}
}
}