refactor: overrideContentTypes

This commit is contained in:
Bobby Wibowo 2022-07-03 10:23:55 +07:00
parent 1404cf9328
commit ad4c2c2e96
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -120,8 +120,9 @@ safe.use(bodyParser.json())
const cdnPages = [...config.pages] const cdnPages = [...config.pages]
let setHeaders let setHeaders
const contentTypes = config.overrideContentTypes && Object.keys(config.overrideContentTypes) const contentTypes = typeof config.overrideContentTypes === 'object' &&
const overrideContentTypes = (res, path) => { Object.keys(config.overrideContentTypes)
const overrideContentTypes = contentTypes && contentTypes.length && function (res, path) {
// Do only if accessing files from uploads' root directory (i.e. not thumbs, etc.) // Do only if accessing files from uploads' root directory (i.e. not thumbs, etc.)
const relpath = path.replace(paths.uploads, '') const relpath = path.replace(paths.uploads, '')
if (relpath.indexOf('/', 1) === -1) { if (relpath.indexOf('/', 1) === -1) {
@ -201,8 +202,8 @@ if (config.cacheControl) {
if (config.serveFilesWithNode) { if (config.serveFilesWithNode) {
initServeStaticUploads({ initServeStaticUploads({
setHeaders: (res, path) => { setHeaders: (res, path) => {
// Override Content-Type if necessary // Override Content-Type header if necessary
if (contentTypes && contentTypes.length) { if (overrideContentTypes) {
overrideContentTypes(res, path) overrideContentTypes(res, path)
} }
// If using CDN, cache uploads in CDN as well // If using CDN, cache uploads in CDN as well
@ -232,14 +233,12 @@ if (config.cacheControl) {
next() next()
}) })
} else if (config.serveFilesWithNode) { } else if (config.serveFilesWithNode) {
initServeStaticUploads({ const opts = {}
setHeaders: (res, path) => { // Override Content-Type header if necessary
// Override Content-Type if necessary if (overrideContentTypes) {
if (contentTypes && contentTypes.length) { opts.setHeaders = overrideContentTypes
overrideContentTypes(res, path) }
} initServeStaticUploads(opts)
}
})
} }
// Static assets // Static assets