mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2024-12-20 19:06:06 +01:00
Optimize LibraryItemController.getCover
This commit is contained in:
parent
431ae97593
commit
9e990d7927
@ -342,44 +342,25 @@ class LibraryItemController {
|
||||
query: { width, height, format, raw }
|
||||
} = req
|
||||
|
||||
const libraryItem = await Database.libraryItemModel.findByPk(req.params.id, {
|
||||
attributes: ['id', 'mediaType', 'mediaId', 'libraryId'],
|
||||
include: [
|
||||
{
|
||||
model: Database.bookModel,
|
||||
attributes: ['id', 'coverPath', 'tags', 'explicit']
|
||||
},
|
||||
{
|
||||
model: Database.podcastModel,
|
||||
attributes: ['id', 'coverPath', 'tags', 'explicit']
|
||||
}
|
||||
]
|
||||
})
|
||||
if (!libraryItem) {
|
||||
Logger.warn(`[LibraryItemController] getCover: Library item "${req.params.id}" does not exist`)
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
|
||||
// Check if user can access this library item
|
||||
if (!req.user.checkCanAccessLibraryItem(libraryItem)) {
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
// Check if library item media has a cover path
|
||||
if (!libraryItem.media.coverPath || !(await fs.pathExists(libraryItem.media.coverPath))) {
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
|
||||
if (req.query.ts) res.set('Cache-Control', 'private, max-age=86400')
|
||||
|
||||
const libraryItemId = req.params.id
|
||||
if (!libraryItemId) {
|
||||
return res.sendStatus(400)
|
||||
}
|
||||
|
||||
if (raw) {
|
||||
const coverPath = await Database.getLibraryItemCoverPath(libraryItemId)
|
||||
if (!coverPath || !(await fs.pathExists(coverPath))) {
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
// any value
|
||||
if (global.XAccel) {
|
||||
const encodedURI = encodeUriPath(global.XAccel + libraryItem.media.coverPath)
|
||||
const encodedURI = encodeUriPath(global.XAccel + coverPath)
|
||||
Logger.debug(`Use X-Accel to serve static file ${encodedURI}`)
|
||||
return res.status(204).header({ 'X-Accel-Redirect': encodedURI }).send()
|
||||
}
|
||||
return res.sendFile(libraryItem.media.coverPath)
|
||||
return res.sendFile(coverPath)
|
||||
}
|
||||
|
||||
const options = {
|
||||
@ -387,7 +368,7 @@ class LibraryItemController {
|
||||
height: height ? parseInt(height) : null,
|
||||
width: width ? parseInt(width) : null
|
||||
}
|
||||
return CacheManager.handleCoverCache(res, libraryItem.id, libraryItem.media.coverPath, options)
|
||||
return CacheManager.handleCoverCache(res, libraryItemId, options)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -4,6 +4,7 @@ const stream = require('stream')
|
||||
const Logger = require('../Logger')
|
||||
const { resizeImage } = require('../utils/ffmpegHelpers')
|
||||
const { encodeUriPath } = require('../utils/fileUtils')
|
||||
const Database = require('../Database')
|
||||
|
||||
class CacheManager {
|
||||
constructor() {
|
||||
@ -29,24 +30,24 @@ class CacheManager {
|
||||
await fs.ensureDir(this.ItemCachePath)
|
||||
}
|
||||
|
||||
async handleCoverCache(res, libraryItemId, coverPath, options = {}) {
|
||||
async handleCoverCache(res, libraryItemId, options = {}) {
|
||||
const format = options.format || 'webp'
|
||||
const width = options.width || 400
|
||||
const height = options.height || null
|
||||
|
||||
res.type(`image/${format}`)
|
||||
|
||||
const path = Path.join(this.CoverCachePath, `${libraryItemId}_${width}${height ? `x${height}` : ''}`) + '.' + format
|
||||
const cachePath = Path.join(this.CoverCachePath, `${libraryItemId}_${width}${height ? `x${height}` : ''}`) + '.' + format
|
||||
|
||||
// Cache exists
|
||||
if (await fs.pathExists(path)) {
|
||||
if (await fs.pathExists(cachePath)) {
|
||||
if (global.XAccel) {
|
||||
const encodedURI = encodeUriPath(global.XAccel + path)
|
||||
const encodedURI = encodeUriPath(global.XAccel + cachePath)
|
||||
Logger.debug(`Use X-Accel to serve static file ${encodedURI}`)
|
||||
return res.status(204).header({ 'X-Accel-Redirect': encodedURI }).send()
|
||||
}
|
||||
|
||||
const r = fs.createReadStream(path)
|
||||
const r = fs.createReadStream(cachePath)
|
||||
const ps = new stream.PassThrough()
|
||||
stream.pipeline(r, ps, (err) => {
|
||||
if (err) {
|
||||
@ -57,7 +58,13 @@ class CacheManager {
|
||||
return ps.pipe(res)
|
||||
}
|
||||
|
||||
const writtenFile = await resizeImage(coverPath, path, width, height)
|
||||
// Cached cover does not exist, generate it
|
||||
const coverPath = await Database.getLibraryItemCoverPath(libraryItemId)
|
||||
if (!coverPath || !(await fs.pathExists(coverPath))) {
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
|
||||
const writtenFile = await resizeImage(coverPath, cachePath, width, height)
|
||||
if (!writtenFile) return res.sendStatus(500)
|
||||
|
||||
if (global.XAccel) {
|
||||
|
Loading…
Reference in New Issue
Block a user