mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-05-27 01:18:24 +02:00
Fix multiple download for podcasts & cleanup
This commit is contained in:
parent
1def32aa50
commit
ff36a9327c
@ -1441,29 +1441,27 @@ class LibraryController {
|
|||||||
const itemIds = req.query.ids.split(',')
|
const itemIds = req.query.ids.split(',')
|
||||||
|
|
||||||
const libraryItems = await Database.libraryItemModel.findAll({
|
const libraryItems = await Database.libraryItemModel.findAll({
|
||||||
attributes: ['id', 'libraryId', 'path'],
|
attributes: ['id', 'libraryId', 'path', 'isFile'],
|
||||||
where: {
|
where: {
|
||||||
id: itemIds,
|
id: itemIds
|
||||||
libraryId: req.params.id,
|
|
||||||
mediaType: 'book'
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
Logger.info(`[LibraryItemController] User "${req.user.username}" requested download for items "${itemIds}"`)
|
Logger.info(`[LibraryController] User "${req.user.username}" requested download for items "${itemIds}"`)
|
||||||
|
|
||||||
const filename = `LibraryItems-${Date.now()}.zip`
|
const filename = `LibraryItems-${Date.now()}.zip`
|
||||||
const libraryItemPaths = libraryItems.map((li) => li.path)
|
const pathObjects = libraryItems.map((li) => ({ path: li.path, isFile: li.isFile }))
|
||||||
|
|
||||||
if (!libraryItemPaths.length) {
|
if (!pathObjects.length) {
|
||||||
Logger.warn(`[LibraryItemController] No library items found for ids "${itemIds}"`)
|
Logger.warn(`[LibraryController] No library items found for ids "${itemIds}"`)
|
||||||
return res.status(404).send('Library items not found')
|
return res.status(404).send('Library items not found')
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await zipHelpers.zipDirectoriesPipe(libraryItemPaths, filename, res)
|
await zipHelpers.zipDirectoriesPipe(pathObjects, filename, res)
|
||||||
Logger.info(`[LibraryItemController] Downloaded item "${filename}" at "${libraryItemPaths}"`)
|
Logger.info(`[LibraryController] Downloaded ${pathObjects.length} items "${filename}"`)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
Logger.error(`[LibraryItemController] Download failed for item "${filename}" at "${libraryItemPaths}"`, error)
|
Logger.error(`[LibraryController] Download failed for items "${filename}" at ${pathObjects.map((po) => po.path).join(', ')}`, error)
|
||||||
zipHelpers.handleDownloadError(error, res)
|
zipHelpers.handleDownloadError(error, res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
const Path = require('path')
|
||||||
|
const { Response } = require('express')
|
||||||
const Logger = require('../Logger')
|
const Logger = require('../Logger')
|
||||||
const archiver = require('../libs/archiver')
|
const archiver = require('../libs/archiver')
|
||||||
const { lstatSync } = require('node:fs')
|
|
||||||
|
|
||||||
module.exports.zipDirectoryPipe = (path, filename, res) => {
|
module.exports.zipDirectoryPipe = (path, filename, res) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -55,12 +56,12 @@ 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 and streams it to the response.
|
||||||
*
|
*
|
||||||
* @param {string[]} paths - Array of directory paths to include in the zip archive.
|
* @param {{ path: string, isFile: boolean }[]} pathObjects
|
||||||
* @param {string} filename - Name of the zip file to be sent as attachment.
|
* @param {string} filename - Name of the zip file to be sent as attachment.
|
||||||
* @param {object} res - Response object to pipe the archive data to.
|
* @param {Response} res - Response object to pipe the archive data to.
|
||||||
* @returns {Promise<void>} - Promise that resolves when the zip operation completes.
|
* @returns {Promise<void>} - Promise that resolves when the zip operation completes.
|
||||||
*/
|
*/
|
||||||
module.exports.zipDirectoriesPipe = (paths, filename, res) => {
|
module.exports.zipDirectoriesPipe = (pathObjects, filename, res) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// create a file to stream archive data to
|
// create a file to stream archive data to
|
||||||
res.attachment(filename)
|
res.attachment(filename)
|
||||||
@ -105,20 +106,14 @@ module.exports.zipDirectoriesPipe = (paths, filename, res) => {
|
|||||||
archive.pipe(res)
|
archive.pipe(res)
|
||||||
|
|
||||||
// Add each path as a directory in the zip
|
// Add each path as a directory in the zip
|
||||||
paths.forEach(path => {
|
pathObjects.forEach((pathObject) => {
|
||||||
|
if (!pathObject.isFile) {
|
||||||
const paths = path.split('/')
|
|
||||||
|
|
||||||
// Check if path is file or directory
|
|
||||||
if (lstatSync(path).isDirectory()) {
|
|
||||||
const dirName = path.split('/').pop()
|
|
||||||
|
|
||||||
// Add the directory to the archive with its name as the root folder
|
// Add the directory to the archive with its name as the root folder
|
||||||
archive.directory(path, dirName);
|
archive.directory(pathObject.path, Path.basename(pathObject.path))
|
||||||
} else {
|
} else {
|
||||||
archive.file(path, { name: paths[paths.length - 1] });
|
archive.file(pathObject.path, { name: Path.basename(pathObject.path) })
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
archive.finalize()
|
archive.finalize()
|
||||||
})
|
})
|
||||||
@ -127,8 +122,8 @@ module.exports.zipDirectoriesPipe = (paths, filename, res) => {
|
|||||||
/**
|
/**
|
||||||
* Handles errors that occur during the download process.
|
* Handles errors that occur during the download process.
|
||||||
*
|
*
|
||||||
* @param error
|
* @param {*} error
|
||||||
* @param res
|
* @param {Response} res
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
module.exports.handleDownloadError = (error, res) => {
|
module.exports.handleDownloadError = (error, res) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user