Remove deprecated /s/ and /ebook/ api routes

This commit is contained in:
advplyr 2023-06-27 15:56:33 -05:00
parent 18cb394884
commit 5b0d105e21
5 changed files with 4 additions and 102 deletions

View File

@ -71,7 +71,6 @@ module.exports = {
],
proxy: {
'/s/': { target: process.env.NODE_ENV !== 'production' ? 'http://localhost:3333' : '/' },
'/api/': { target: process.env.NODE_ENV !== 'production' ? 'http://localhost:3333' : '/' },
'/dev/': { target: 'http://localhost:3333', pathRewrite: { '^/dev/': '' } }
},

View File

@ -22,7 +22,6 @@ const SocketAuthority = require('./SocketAuthority')
const ApiRouter = require('./routers/ApiRouter')
const HlsRouter = require('./routers/HlsRouter')
const StaticRouter = require('./routers/StaticRouter')
const NotificationManager = require('./managers/NotificationManager')
const EmailManager = require('./managers/EmailManager')
@ -84,7 +83,6 @@ class Server {
// Routers
this.apiRouter = new ApiRouter(this)
this.hlsRouter = new HlsRouter(this.db, this.auth, this.playbackSessionManager)
this.staticRouter = new StaticRouter(this.db)
Logger.logManager = this.logManager
@ -170,38 +168,6 @@ class Server {
router.use('/api', this.authMiddleware.bind(this), this.apiRouter.router)
router.use('/hls', this.authMiddleware.bind(this), this.hlsRouter.router)
// TODO: Deprecated as of 2.2.21 edge
router.use('/s', this.authMiddleware.bind(this), this.staticRouter.router)
// EBook static file routes
// TODO: Deprecated as of 2.2.21 edge
router.get('/ebook/:library/:folder/*', (req, res) => {
const library = this.db.libraries.find(lib => lib.id === req.params.library)
if (!library) return res.sendStatus(404)
const folder = library.folders.find(fol => fol.id === req.params.folder)
if (!folder) return res.status(404).send('Folder not found')
// Replace backslashes with forward slashes
const remainingPath = req.params['0'].replace(/\\/g, '/')
// Prevent path traversal
// e.g. ../../etc/passwd
if (/\/?\.?\.\//.test(remainingPath)) {
Logger.error(`[Server] Invalid path to get ebook "${remainingPath}"`)
return res.sendStatus(403)
}
// Check file ext is a valid ebook file
const filext = (Path.extname(remainingPath) || '').slice(1).toLowerCase()
if (!globals.SupportedEbookTypes.includes(filext)) {
Logger.error(`[Server] Invalid ebook file ext requested "${remainingPath}"`)
return res.sendStatus(403)
}
const fullPath = Path.join(folder.fullPath, remainingPath)
res.sendFile(fullPath)
})
// RSS Feed temp route
router.get('/feed/:id', (req, res) => {
Logger.info(`[Server] Requesting rss feed ${req.params.id}`)

View File

@ -1,6 +1,3 @@
const Path = require('path')
const { encodeUriPath } = require('../../utils/fileUtils')
class AudioTrack {
constructor() {
this.index = null
@ -22,7 +19,7 @@ class AudioTrack {
contentUrl: this.contentUrl,
mimeType: this.mimeType,
codec: this.codec,
metadata: this.metadata ? this.metadata.toJSON() : null
metadata: this.metadata?.toJSON() || null
}
}
@ -31,8 +28,8 @@ class AudioTrack {
this.startOffset = startOffset
this.duration = audioFile.duration
this.title = audioFile.metadata.filename || ''
// TODO: Switch to /api/items/:id/file/:fileid
this.contentUrl = Path.join(`${global.RouterBasePath}/s/item/${itemId}`, encodeUriPath(audioFile.metadata.relPath))
this.contentUrl = `${global.RouterBasePath}/api/items/${itemId}/file/${audioFile.ino}`
this.mimeType = audioFile.mimeType
this.codec = audioFile.codec || null
this.metadata = audioFile.metadata.clone()

View File

@ -28,8 +28,7 @@ class VideoTrack {
this.index = videoFile.index
this.duration = videoFile.duration
this.title = videoFile.metadata.filename || ''
// TODO: Switch to /api/items/:id/file/:fileid
this.contentUrl = Path.join(`${global.RouterBasePath}/s/item/${itemId}`, encodeUriPath(videoFile.metadata.relPath))
this.contentUrl = Path.join(`${global.RouterBasePath}/api/items/${itemId}/file/${videoFile.ino}`, encodeUriPath(videoFile.metadata.relPath))
this.mimeType = videoFile.mimeType
this.codec = videoFile.codec
this.metadata = videoFile.metadata.clone()

View File

@ -1,59 +0,0 @@
const express = require('express')
const Path = require('path')
const Logger = require('../Logger')
const { getAudioMimeTypeFromExtname } = require('../utils/fileUtils')
// TODO: Deprecated as of 2.2.21 edge
class StaticRouter {
constructor(db) {
this.db = db
this.router = express()
this.router.disable('x-powered-by')
this.init()
}
init() {
// Library Item static file routes
this.router.get('/item/:id/*', (req, res) => {
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)
// Replace backslashes with forward slashes
const remainingPath = req.params['0'].replace(/\\/g, '/')
// Check user has access to this library item
if (!req.user.checkCanAccessLibraryItem(item)) {
Logger.error(`[StaticRouter] User attempted to access library item file without access ${remainingPath}`, req.user)
return res.sendStatus(403)
}
// Prevent path traversal
// e.g. ../../etc/passwd
if (/\/?\.?\.\//.test(remainingPath)) {
Logger.error(`[StaticRouter] Invalid path to get library item file "${remainingPath}"`)
return res.sendStatus(403)
}
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()
}
let opts = {}
// Express does not set the correct mimetype for m4b files so use our defined mimetypes if available
const audioMimeType = getAudioMimeTypeFromExtname(Path.extname(fullPath))
if (audioMimeType) {
opts = { headers: { 'Content-Type': audioMimeType } }
}
res.sendFile(fullPath, opts)
})
}
}
module.exports = StaticRouter