mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2024-12-12 15:36:22 +00:00
fix: content-type breaking with hyper-express updates
This commit is contained in:
parent
28a1e02330
commit
5d3fc64e3b
@ -108,11 +108,13 @@ class ServeStatic {
|
||||
// Can be programatically called from within in-progress Requests
|
||||
// Essentially stream-based alternative for Response.download() or Response.send()
|
||||
async #handle (req, res, fullPath, stat, setHeaders) {
|
||||
const extname = utils.extname(req.path).substring(1)
|
||||
|
||||
// Set Content-Type
|
||||
res.type(fullPath)
|
||||
res.type(extname)
|
||||
|
||||
// Set header fields
|
||||
await this.#setHeaders(req, res, stat)
|
||||
await this.#setHeaders(req, res, stat, extname)
|
||||
|
||||
// Per-request setHeaders, if required
|
||||
if (typeof setHeaders === 'function') {
|
||||
@ -210,24 +212,16 @@ class ServeStatic {
|
||||
}
|
||||
}
|
||||
|
||||
async #setContentType (req, res) {
|
||||
// Do only if accessing files from uploads' root directory (i.e. not thumbs, etc.)
|
||||
if (req.path.indexOf('/', 1) !== -1) return
|
||||
|
||||
const name = req.path.substring(1)
|
||||
const extname = utils.extname(name).substring(1)
|
||||
const contentType = this.contentTypesMaps.get(extname)
|
||||
if (contentType) {
|
||||
// NOTE: Use lowercase key because the header initially set
|
||||
// with Response.type() is also lowercase
|
||||
res.header('content-type', contentType)
|
||||
}
|
||||
}
|
||||
|
||||
async #setHeaders (req, res, stat) {
|
||||
async #setHeaders (req, res, stat, extname) {
|
||||
// Override Content-Type if required
|
||||
if (this.contentTypesMaps) {
|
||||
this.#setContentType(req, res)
|
||||
if (this.contentTypesMaps && req.path.indexOf('/', 1) === -1) {
|
||||
// Do only if accessing files from uploads' root directory (i.e. not thumbs, etc.)
|
||||
const contentType = this.contentTypesMaps.get(extname)
|
||||
if (contentType) {
|
||||
// NOTE: Use lowercase key because the header initially set
|
||||
// with Response.type() is also lowercase
|
||||
res.header('content-type', contentType)
|
||||
}
|
||||
}
|
||||
|
||||
// Always do external setHeaders function first,
|
||||
|
@ -22,6 +22,7 @@
|
||||
const chokidar = require('chokidar')
|
||||
const etag = require('etag')
|
||||
const jetpack = require('fs-jetpack')
|
||||
const nodePath = require('node:path')
|
||||
const serveUtils = require('./../utils/serveUtils')
|
||||
const logger = require('./../../logger')
|
||||
|
||||
@ -84,19 +85,19 @@ class ServeStaticQuick {
|
||||
}
|
||||
|
||||
get (path) {
|
||||
const stat = this.files.get(path)
|
||||
const data = this.files.get(path)
|
||||
|
||||
if (!stat || stat.isDirectory()) return
|
||||
if (!data?.stat || data?.stat?.isDirectory()) return
|
||||
|
||||
return stat
|
||||
return data
|
||||
}
|
||||
|
||||
handler (req, res, path, stat) {
|
||||
handler (req, res, path, data) {
|
||||
// Set Content-Type
|
||||
res.type(path)
|
||||
res.type(data.extname)
|
||||
|
||||
// Set header fields
|
||||
this.#setHeaders(req, res, stat)
|
||||
this.#setHeaders(req, res, data.stat)
|
||||
|
||||
// Conditional GET support
|
||||
if (serveUtils.assertConditionalGET(req, res)) {
|
||||
@ -104,7 +105,7 @@ class ServeStaticQuick {
|
||||
}
|
||||
|
||||
// ReadStream options with Content-Range support if required
|
||||
const result = serveUtils.buildReadStreamOptions(req, res, stat, this.#options.acceptRanges)
|
||||
const result = serveUtils.buildReadStreamOptions(req, res, data.stat, this.#options.acceptRanges)
|
||||
if (!result) {
|
||||
return res.end()
|
||||
}
|
||||
@ -120,7 +121,7 @@ class ServeStaticQuick {
|
||||
res.end()
|
||||
}
|
||||
|
||||
return this.#stream(req, res, path, stat, result)
|
||||
return this.#stream(req, res, path, data.stat, result)
|
||||
}
|
||||
|
||||
// Returns a promise which resolves to true once ServeStaticQuick is ready
|
||||
@ -148,7 +149,10 @@ class ServeStaticQuick {
|
||||
case 'change':
|
||||
// Ensure relative path does not pass ignore function if set
|
||||
if (!this.#options.ignore || !this.#options.ignore(relPath, stat)) {
|
||||
this.files.set(relPath, stat)
|
||||
this.files.set(relPath, {
|
||||
stat,
|
||||
extname: nodePath.extname(relPath).substring(1)
|
||||
})
|
||||
}
|
||||
break
|
||||
case 'unlink':
|
||||
@ -189,12 +193,12 @@ class ServeStaticQuick {
|
||||
}
|
||||
}
|
||||
|
||||
const stat = this.get(path)
|
||||
if (stat === undefined) {
|
||||
const data = this.get(path)
|
||||
if (data === undefined) {
|
||||
return next()
|
||||
}
|
||||
|
||||
return this.handler(req, res, path, stat)
|
||||
return this.handler(req, res, path, data)
|
||||
}
|
||||
|
||||
#setHeaders (req, res, stat) {
|
||||
|
Loading…
Reference in New Issue
Block a user