mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-01-31 07:11:33 +00:00
refactor: ServeStatic.middleware -> .handler
also moved it from middlewares to handlers directory reasoning is that this class is better suited to handle routes directly instead of being a global middleware since IO stat to check if request path matches a physical file in the disk every single time is not very performant
This commit is contained in:
parent
06178cc2c4
commit
76a73b7e83
@ -10,8 +10,6 @@ const utils = require('./../utilsController')
|
|||||||
const serveUtils = require('./../utils/serveUtils')
|
const serveUtils = require('./../utils/serveUtils')
|
||||||
const logger = require('./../../logger')
|
const logger = require('./../../logger')
|
||||||
|
|
||||||
// NOTE: This middleware must be set last if on a root path that is also used by other routes
|
|
||||||
|
|
||||||
class ServeStatic {
|
class ServeStatic {
|
||||||
directory
|
directory
|
||||||
contentDispositionStore
|
contentDispositionStore
|
||||||
@ -22,8 +20,6 @@ class ServeStatic {
|
|||||||
#options
|
#options
|
||||||
|
|
||||||
constructor (directory, options = {}) {
|
constructor (directory, options = {}) {
|
||||||
logger.debug(`new ServeStatic(): ${directory}`)
|
|
||||||
|
|
||||||
if (!directory || typeof directory !== 'string') {
|
if (!directory || typeof directory !== 'string') {
|
||||||
throw new TypeError('Root directory must be set')
|
throw new TypeError('Root directory must be set')
|
||||||
}
|
}
|
||||||
@ -140,7 +136,7 @@ class ServeStatic {
|
|||||||
* MIT Licensed
|
* MIT Licensed
|
||||||
*/
|
*/
|
||||||
|
|
||||||
async #middleware (req, res) {
|
async #handler (req, res) {
|
||||||
if (this.#options.ignorePatterns && this.#options.ignorePatterns.some(pattern => req.path.startsWith(pattern))) {
|
if (this.#options.ignorePatterns && this.#options.ignorePatterns.some(pattern => req.path.startsWith(pattern))) {
|
||||||
return errors.handleNotFound(req, res)
|
return errors.handleNotFound(req, res)
|
||||||
}
|
}
|
||||||
@ -279,8 +275,8 @@ class ServeStatic {
|
|||||||
return res.stream(readStream)
|
return res.stream(readStream)
|
||||||
}
|
}
|
||||||
|
|
||||||
get middleware () {
|
get handler () {
|
||||||
return this.#middleware.bind(this)
|
return this.#handler.bind(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
lolisafe.js
10
lolisafe.js
@ -43,12 +43,14 @@ const paths = require('./controllers/pathsController')
|
|||||||
paths.initSync()
|
paths.initSync()
|
||||||
const utils = require('./controllers/utilsController')
|
const utils = require('./controllers/utilsController')
|
||||||
|
|
||||||
// Custom middlewares
|
// Middlewares
|
||||||
const ExpressCompat = require('./controllers/middlewares/expressCompat')
|
const ExpressCompat = require('./controllers/middlewares/expressCompat')
|
||||||
const NunjucksRenderer = require('./controllers/middlewares/nunjucksRenderer')
|
const NunjucksRenderer = require('./controllers/middlewares/nunjucksRenderer')
|
||||||
const RateLimiter = require('./controllers/middlewares/rateLimiter')
|
const RateLimiter = require('./controllers/middlewares/rateLimiter')
|
||||||
const ServeLiveDirectory = require('./controllers/middlewares/serveLiveDirectory')
|
const ServeLiveDirectory = require('./controllers/middlewares/serveLiveDirectory')
|
||||||
const ServeStatic = require('./controllers/middlewares/serveStatic')
|
|
||||||
|
// Handlers
|
||||||
|
const ServeStatic = require('./controllers/handlers/serveStatic')
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
const album = require('./routes/album')
|
const album = require('./routes/album')
|
||||||
@ -273,8 +275,8 @@ safe.use('/api', api)
|
|||||||
overrideContentTypes: config.overrideContentTypes,
|
overrideContentTypes: config.overrideContentTypes,
|
||||||
setContentDisposition: config.setContentDisposition
|
setContentDisposition: config.setContentDisposition
|
||||||
})
|
})
|
||||||
safe.get('/*', serveStaticInstance.middleware)
|
safe.get('/*', serveStaticInstance.handler)
|
||||||
safe.head('/*', serveStaticInstance.middleware)
|
safe.head('/*', serveStaticInstance.handler)
|
||||||
utils.contentDispositionStore = serveStaticInstance.contentDispositionStore
|
utils.contentDispositionStore = serveStaticInstance.contentDispositionStore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user