mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2024-12-29 01:58:49 +01:00
Merge pull request #1431 from lkiesow/x-accel
Implement X-Accel Redirect
This commit is contained in:
commit
9ebe4b55dd
@ -48,6 +48,7 @@ class Server {
|
||||
global.ConfigPath = fileUtils.filePathToPOSIX(Path.normalize(CONFIG_PATH))
|
||||
global.MetadataPath = fileUtils.filePathToPOSIX(Path.normalize(METADATA_PATH))
|
||||
global.RouterBasePath = ROUTER_BASE_PATH
|
||||
global.XAccel = process.env.USE_X_ACCEL
|
||||
|
||||
if (!fs.pathExistsSync(global.ConfigPath)) {
|
||||
fs.mkdirSync(global.ConfigPath)
|
||||
|
@ -201,6 +201,10 @@ class LibraryItemController {
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
|
||||
if (global.XAccel) {
|
||||
Logger.debug(`Use X-Accel to serve static file ${libraryItem.media.coverPath}`)
|
||||
return res.status(204).header({'X-Accel-Redirect': global.XAccel + libraryItem.media.coverPath}).send()
|
||||
}
|
||||
return res.sendFile(libraryItem.media.coverPath)
|
||||
}
|
||||
|
||||
|
@ -51,6 +51,11 @@ class CacheManager {
|
||||
|
||||
// Cache exists
|
||||
if (await fs.pathExists(path)) {
|
||||
if (global.XAccel) {
|
||||
Logger.debug(`Use X-Accel to serve static file ${path}`)
|
||||
return res.status(204).header({'X-Accel-Redirect': global.XAccel + path}).send()
|
||||
}
|
||||
|
||||
const r = fs.createReadStream(path)
|
||||
const ps = new stream.PassThrough()
|
||||
stream.pipeline(r, ps, (err) => {
|
||||
@ -72,6 +77,11 @@ class CacheManager {
|
||||
// Set owner and permissions of cache image
|
||||
await filePerms.setDefault(path)
|
||||
|
||||
if (global.XAccel) {
|
||||
Logger.debug(`Use X-Accel to serve static file ${writtenFile}`)
|
||||
return res.status(204).header({'X-Accel-Redirect': global.XAccel + writtenFile}).send()
|
||||
}
|
||||
|
||||
var readStream = fs.createReadStream(writtenFile)
|
||||
readStream.pipe(res)
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
const express = require('express')
|
||||
const Path = require('path')
|
||||
const Logger = require('../Logger')
|
||||
const { getAudioMimeTypeFromExtname } = require('../utils/fileUtils')
|
||||
|
||||
class StaticRouter {
|
||||
@ -13,13 +14,18 @@ class StaticRouter {
|
||||
init() {
|
||||
// Library Item static file routes
|
||||
this.router.get('/item/:id/*', (req, res) => {
|
||||
var item = this.db.libraryItems.find(ab => ab.id === req.params.id)
|
||||
const item = this.db.libraryItems.find(ab => ab.id === req.params.id)
|
||||
if (!item) return res.status(404).send('Item not found with id ' + req.params.id)
|
||||
|
||||
var remainingPath = req.params['0']
|
||||
var fullPath = null
|
||||
if (item.isFile) fullPath = item.path
|
||||
else fullPath = Path.join(item.path, remainingPath)
|
||||
const remainingPath = req.params['0']
|
||||
const fullPath = item.isFile ? item.path : Path.join(item.path, remainingPath)
|
||||
|
||||
// Allow reverse proxy to serve files directly
|
||||
// See: https://www.nginx.com/resources/wiki/start/topics/examples/x-accel/
|
||||
if (global.XAccel) {
|
||||
Logger.debug(`Use X-Accel to serve static file ${fullPath}`)
|
||||
return res.status(204).header({'X-Accel-Redirect': global.XAccel + fullPath}).send()
|
||||
}
|
||||
|
||||
var opts = {}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user