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 }
|
query: { width, height, format, raw }
|
||||||
} = req
|
} = 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')
|
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) {
|
if (raw) {
|
||||||
|
const coverPath = await Database.getLibraryItemCoverPath(libraryItemId)
|
||||||
|
if (!coverPath || !(await fs.pathExists(coverPath))) {
|
||||||
|
return res.sendStatus(404)
|
||||||
|
}
|
||||||
// any value
|
// any value
|
||||||
if (global.XAccel) {
|
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}`)
|
Logger.debug(`Use X-Accel to serve static file ${encodedURI}`)
|
||||||
return res.status(204).header({ 'X-Accel-Redirect': encodedURI }).send()
|
return res.status(204).header({ 'X-Accel-Redirect': encodedURI }).send()
|
||||||
}
|
}
|
||||||
return res.sendFile(libraryItem.media.coverPath)
|
return res.sendFile(coverPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
@ -387,7 +368,7 @@ class LibraryItemController {
|
|||||||
height: height ? parseInt(height) : null,
|
height: height ? parseInt(height) : null,
|
||||||
width: width ? parseInt(width) : 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 Logger = require('../Logger')
|
||||||
const { resizeImage } = require('../utils/ffmpegHelpers')
|
const { resizeImage } = require('../utils/ffmpegHelpers')
|
||||||
const { encodeUriPath } = require('../utils/fileUtils')
|
const { encodeUriPath } = require('../utils/fileUtils')
|
||||||
|
const Database = require('../Database')
|
||||||
|
|
||||||
class CacheManager {
|
class CacheManager {
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -29,24 +30,24 @@ class CacheManager {
|
|||||||
await fs.ensureDir(this.ItemCachePath)
|
await fs.ensureDir(this.ItemCachePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleCoverCache(res, libraryItemId, coverPath, options = {}) {
|
async handleCoverCache(res, libraryItemId, options = {}) {
|
||||||
const format = options.format || 'webp'
|
const format = options.format || 'webp'
|
||||||
const width = options.width || 400
|
const width = options.width || 400
|
||||||
const height = options.height || null
|
const height = options.height || null
|
||||||
|
|
||||||
res.type(`image/${format}`)
|
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
|
// Cache exists
|
||||||
if (await fs.pathExists(path)) {
|
if (await fs.pathExists(cachePath)) {
|
||||||
if (global.XAccel) {
|
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}`)
|
Logger.debug(`Use X-Accel to serve static file ${encodedURI}`)
|
||||||
return res.status(204).header({ 'X-Accel-Redirect': encodedURI }).send()
|
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()
|
const ps = new stream.PassThrough()
|
||||||
stream.pipeline(r, ps, (err) => {
|
stream.pipeline(r, ps, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -57,7 +58,13 @@ class CacheManager {
|
|||||||
return ps.pipe(res)
|
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 (!writtenFile) return res.sendStatus(500)
|
||||||
|
|
||||||
if (global.XAccel) {
|
if (global.XAccel) {
|
||||||
|
Loading…
Reference in New Issue
Block a user