From 753ae3d7dc18e821ddf3d49a49aa3a9015fb5947 Mon Sep 17 00:00:00 2001 From: advplyr Date: Tue, 10 Oct 2023 17:51:52 -0500 Subject: [PATCH] Fix:Server crash when downloading single file library items #2199 --- server/controllers/LibraryItemController.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/server/controllers/LibraryItemController.js b/server/controllers/LibraryItemController.js index 2b25474e..ac019a96 100644 --- a/server/controllers/LibraryItemController.js +++ b/server/controllers/LibraryItemController.js @@ -85,12 +85,31 @@ class LibraryItemController { 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) { if (!req.user.canDownload) { Logger.warn('User attempted to download without permission', req.user) 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 itemTitle = req.libraryItem.media.metadata.title Logger.info(`[LibraryItemController] User "${req.user.username}" requested download for item "${itemTitle}" at "${libraryItemPath}"`)