mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-08-28 13:46:20 +02:00
Add download route to share controller and public router
This commit is contained in:
parent
7f5a6fc917
commit
5c4271045e
@ -7,6 +7,7 @@ const Database = require('../Database')
|
||||
|
||||
const { PlayMethod } = require('../utils/constants')
|
||||
const { getAudioMimeTypeFromExtname, encodeUriPath } = require('../utils/fileUtils')
|
||||
const zipHelpers = require('../utils/zipHelpers')
|
||||
|
||||
const PlaybackSession = require('../objects/PlaybackSession')
|
||||
const ShareManager = require('../managers/ShareManager')
|
||||
@ -210,6 +211,75 @@ class ShareController {
|
||||
res.sendFile(audioTrackPath)
|
||||
}
|
||||
|
||||
/**
|
||||
* Public route
|
||||
*
|
||||
* GET: /api/share/:slug/download
|
||||
* Downloads media item share
|
||||
*
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async downloadMediaItemShare(req, res) {
|
||||
const { slug } = req.params
|
||||
|
||||
// Find matching MediaItemShare based on slug
|
||||
const mediaItemShare = await ShareManager.findBySlug(slug)
|
||||
// If the file isDownloadable, download the file
|
||||
if (mediaItemShare.isDownloadable) {
|
||||
// Get mediaItemId and type
|
||||
const { mediaItemId, mediaItemType } = mediaItemShare
|
||||
|
||||
// Get the library item from the mediaItemId
|
||||
const libraryItem = await Database.libraryItemModel.findOne({
|
||||
where: {
|
||||
mediaId: mediaItemId
|
||||
}
|
||||
})
|
||||
|
||||
const itemPath = libraryItem.path
|
||||
const itemRelPath = libraryItem.relPath
|
||||
let itemTitle
|
||||
|
||||
// Get the title based on the mediaItemType
|
||||
if (mediaItemType === 'podcastEpisode') {
|
||||
const podcastEpisode = await Database.podcastEpisodeModel.findOne({
|
||||
where: {
|
||||
id: mediaItemId
|
||||
}
|
||||
})
|
||||
itemTitle = podcastEpisode.title
|
||||
} else if (mediaItemType === 'book') {
|
||||
const book = await Database.bookModel.findOne({
|
||||
where: {
|
||||
id: mediaItemId
|
||||
}
|
||||
})
|
||||
itemTitle = book.title
|
||||
}
|
||||
|
||||
Logger.info(`[ShareController] Requested download for book "${itemTitle}" at "${itemPath}"`)
|
||||
|
||||
try {
|
||||
if (libraryItem.isFile) {
|
||||
const audioMimeType = getAudioMimeTypeFromExtname(Path.extname(itemPath))
|
||||
if (audioMimeType) {
|
||||
res.setHeader('Content-Type', audioMimeType)
|
||||
}
|
||||
await new Promise((resolve, reject) => res.download(itemPath, itemRelPath, (error) => (error ? reject(error) : resolve())))
|
||||
} else {
|
||||
const filename = `${itemTitle}.zip`
|
||||
await zipHelpers.zipDirectoryPipe(itemPath, filename, res)
|
||||
}
|
||||
|
||||
Logger.info(`[ShareController] Downloaded item "${itemTitle}" at "${itemPath}"`)
|
||||
} catch (error) {
|
||||
Logger.error(`[ShareController] Download failed for item "${itemTitle}" at "${itemPath}"`, error)
|
||||
res.status(500).send('Failed to download the item')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Public route - requires share_session_id cookie
|
||||
*
|
||||
|
@ -15,6 +15,7 @@ class PublicRouter {
|
||||
this.router.get('/share/:slug', ShareController.getMediaItemShareBySlug.bind(this))
|
||||
this.router.get('/share/:slug/track/:index', ShareController.getMediaItemShareAudioTrack.bind(this))
|
||||
this.router.get('/share/:slug/cover', ShareController.getMediaItemShareCoverImage.bind(this))
|
||||
this.router.get('/share/:slug/download', ShareController.downloadMediaItemShare.bind(this))
|
||||
this.router.patch('/share/:slug/progress', ShareController.updateMediaItemShareProgress.bind(this))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user