Merge pull request #1431 from lkiesow/x-accel

Implement X-Accel Redirect
This commit is contained in:
advplyr 2023-01-23 17:27:23 -06:00 committed by GitHub
commit 9ebe4b55dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 5 deletions

View File

@ -48,6 +48,7 @@ class Server {
global.ConfigPath = fileUtils.filePathToPOSIX(Path.normalize(CONFIG_PATH)) global.ConfigPath = fileUtils.filePathToPOSIX(Path.normalize(CONFIG_PATH))
global.MetadataPath = fileUtils.filePathToPOSIX(Path.normalize(METADATA_PATH)) global.MetadataPath = fileUtils.filePathToPOSIX(Path.normalize(METADATA_PATH))
global.RouterBasePath = ROUTER_BASE_PATH global.RouterBasePath = ROUTER_BASE_PATH
global.XAccel = process.env.USE_X_ACCEL
if (!fs.pathExistsSync(global.ConfigPath)) { if (!fs.pathExistsSync(global.ConfigPath)) {
fs.mkdirSync(global.ConfigPath) fs.mkdirSync(global.ConfigPath)

View File

@ -201,6 +201,10 @@ class LibraryItemController {
return res.sendStatus(404) 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) return res.sendFile(libraryItem.media.coverPath)
} }

View File

@ -51,6 +51,11 @@ class CacheManager {
// Cache exists // Cache exists
if (await fs.pathExists(path)) { 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 r = fs.createReadStream(path)
const ps = new stream.PassThrough() const ps = new stream.PassThrough()
stream.pipeline(r, ps, (err) => { stream.pipeline(r, ps, (err) => {
@ -72,6 +77,11 @@ class CacheManager {
// Set owner and permissions of cache image // Set owner and permissions of cache image
await filePerms.setDefault(path) 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) var readStream = fs.createReadStream(writtenFile)
readStream.pipe(res) readStream.pipe(res)
} }

View File

@ -1,5 +1,6 @@
const express = require('express') const express = require('express')
const Path = require('path') const Path = require('path')
const Logger = require('../Logger')
const { getAudioMimeTypeFromExtname } = require('../utils/fileUtils') const { getAudioMimeTypeFromExtname } = require('../utils/fileUtils')
class StaticRouter { class StaticRouter {
@ -13,13 +14,18 @@ class StaticRouter {
init() { init() {
// Library Item static file routes // Library Item static file routes
this.router.get('/item/:id/*', (req, res) => { 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) if (!item) return res.status(404).send('Item not found with id ' + req.params.id)
var remainingPath = req.params['0'] const remainingPath = req.params['0']
var fullPath = null const fullPath = item.isFile ? item.path : Path.join(item.path, remainingPath)
if (item.isFile) fullPath = item.path
else fullPath = 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 = {} var opts = {}