Refactor download method in LibraryItemController to handle share items

This commit is contained in:
Greg Lorenzen 2024-12-03 22:05:48 +00:00
parent b5938b618d
commit 5eb51bf8a2

View File

@ -133,10 +133,7 @@ class LibraryItemController {
* @param {Response} res
*/
async download(req, res) {
if (!req.user.canDownload) {
Logger.warn(`User "${req.user.username}" attempted to download without permission`)
return res.sendStatus(403)
}
const handleDownload = async (req, res) => {
const libraryItemPath = req.libraryItem.path
const itemTitle = req.libraryItem.media.metadata.title
@ -158,10 +155,29 @@ class LibraryItemController {
Logger.info(`[LibraryItemController] Downloaded item "${itemTitle}" at "${libraryItemPath}"`)
} catch (error) {
Logger.error(`[LibraryItemController] Download failed for item "${itemTitle}" at "${libraryItemPath}"`, error)
LibraryItemController.handleDownloadError(error, res)
res.status(500).send('Failed to download the item')
}
}
if (req.query.share) {
// Find matching MediaItemShare based on slug
const mediaItemShare = await ShareManager.findBySlug(req.query.share)
if (mediaItemShare) {
// If the isDownloadable bool is true, download the file
if (mediaItemShare.isDownloadable) {
return handleDownload(req, res)
}
}
}
if (!req.user.canDownload) {
Logger.warn(`User "${req.user.username}" attempted to download without permission`)
return res.sendStatus(403)
}
return handleDownload(req, res)
}
/**
* PATCH: /items/:id/media
* Update media for a library item. Will create new authors & series when necessary