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}"`)