Fix:Server crash when downloading single file library items #2199

This commit is contained in:
advplyr 2023-10-10 17:51:52 -05:00
parent c9a2fdcb29
commit 753ae3d7dc

View File

@ -85,12 +85,31 @@ class LibraryItemController {
res.sendStatus(200) res.sendStatus(200)
} }
/**
* GET: /api/items/:id/download
* Download library item. Zip file if multiple files.
*
* @param {import('express').Request} req
* @param {import('express').Response} res
*/
download(req, res) { download(req, res) {
if (!req.user.canDownload) { if (!req.user.canDownload) {
Logger.warn('User attempted to download without permission', req.user) Logger.warn('User attempted to download without permission', req.user)
return res.sendStatus(403) return res.sendStatus(403)
} }
// If library item is a single file in root dir then no need to zip
if (req.libraryItem.isFile) {
// Express does not set the correct mimetype for m4b files so use our defined mimetypes if available
const audioMimeType = getAudioMimeTypeFromExtname(Path.extname(req.libraryItem.path))
if (audioMimeType) {
res.setHeader('Content-Type', audioMimeType)
}
res.download(req.libraryItem.path, req.libraryItem.relPath)
return
}
const libraryItemPath = req.libraryItem.path const libraryItemPath = req.libraryItem.path
const itemTitle = req.libraryItem.media.metadata.title const itemTitle = req.libraryItem.media.metadata.title
Logger.info(`[LibraryItemController] User "${req.user.username}" requested download for item "${itemTitle}" at "${libraryItemPath}"`) Logger.info(`[LibraryItemController] User "${req.user.username}" requested download for item "${itemTitle}" at "${libraryItemPath}"`)