mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-08-28 13:46:20 +02:00
Refactor download method in LibraryItemController to handle share items
This commit is contained in:
parent
b5938b618d
commit
5eb51bf8a2
@ -133,10 +133,7 @@ class LibraryItemController {
|
|||||||
* @param {Response} res
|
* @param {Response} res
|
||||||
*/
|
*/
|
||||||
async download(req, res) {
|
async download(req, res) {
|
||||||
if (!req.user.canDownload) {
|
const handleDownload = async (req, res) => {
|
||||||
Logger.warn(`User "${req.user.username}" attempted to download without permission`)
|
|
||||||
return res.sendStatus(403)
|
|
||||||
}
|
|
||||||
const libraryItemPath = req.libraryItem.path
|
const libraryItemPath = req.libraryItem.path
|
||||||
const itemTitle = req.libraryItem.media.metadata.title
|
const itemTitle = req.libraryItem.media.metadata.title
|
||||||
|
|
||||||
@ -158,10 +155,29 @@ class LibraryItemController {
|
|||||||
Logger.info(`[LibraryItemController] Downloaded item "${itemTitle}" at "${libraryItemPath}"`)
|
Logger.info(`[LibraryItemController] Downloaded item "${itemTitle}" at "${libraryItemPath}"`)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
Logger.error(`[LibraryItemController] Download failed for item "${itemTitle}" at "${libraryItemPath}"`, 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
|
* PATCH: /items/:id/media
|
||||||
* Update media for a library item. Will create new authors & series when necessary
|
* Update media for a library item. Will create new authors & series when necessary
|
||||||
|
Loading…
Reference in New Issue
Block a user