Added Content-Type override when serving with node

Closes #274
This commit is contained in:
Bobby Wibowo 2020-11-03 23:53:56 +07:00
parent e06d927a2b
commit b63836c89d
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
2 changed files with 37 additions and 2 deletions

View File

@ -38,6 +38,14 @@ module.exports = {
*/ */
setContentDisposition: false, setContentDisposition: false,
/*
If you serve files with node, you can optionally choose to
override Content-Type header for certain extension names.
*/
overrideContentTypes: {
'text/plain': ['html', 'htm', 'shtml', 'xhtml']
},
/* /*
If you are serving your files with a different domain than your lolisafe homepage, If you are serving your files with a different domain than your lolisafe homepage,
then fill this option with your lolisafe homepage, otherwise any falsy value. then fill this option with your lolisafe homepage, otherwise any falsy value.

View File

@ -72,6 +72,21 @@ let setHeaders = res => {
res.set('Access-Control-Allow-Origin', '*') res.set('Access-Control-Allow-Origin', '*')
} }
const contentTypes = config.overrideContentTypes && Object.keys(config.overrideContentTypes)
const overrideContentTypes = (res, path) => {
// Do only if accessing files from uploads' root directory (i.e. not thumbs, etc.)
const relpath = path.replace(paths.uploads, '')
if (relpath.indexOf('/', 1) === -1) {
const name = relpath.substring(1)
const extname = utils.extname(name).substring(1)
for (const contentType of contentTypes) {
if (config.overrideContentTypes[contentType].includes(extname)) {
res.set('Content-Type', contentType)
}
}
}
}
const initServeStaticUploads = (opts = {}) => { const initServeStaticUploads = (opts = {}) => {
if (config.setContentDisposition) { if (config.setContentDisposition) {
opts.preSetHeaders = async (res, req, path, stat) => { opts.preSetHeaders = async (res, req, path, stat) => {
@ -133,8 +148,12 @@ if (config.cacheControl) {
// If serving uploads with node // If serving uploads with node
if (config.serveFilesWithNode) { if (config.serveFilesWithNode) {
initServeStaticUploads({ initServeStaticUploads({
setHeaders: res => { setHeaders: (res, path) => {
res.set('Access-Control-Allow-Origin', '*') res.set('Access-Control-Allow-Origin', '*')
// Override Content-Type if necessary
if (contentTypes && contentTypes.length) {
overrideContentTypes(res, path)
}
// If using CDN, cache uploads in CDN as well // If using CDN, cache uploads in CDN as well
// Use with cloudflare.purgeCache enabled in config file // Use with cloudflare.purgeCache enabled in config file
if (config.cacheControl !== 2) { if (config.cacheControl !== 2) {
@ -164,7 +183,15 @@ if (config.cacheControl) {
next() next()
}) })
} else if (config.serveFilesWithNode) { } else if (config.serveFilesWithNode) {
initServeStaticUploads() initServeStaticUploads({
setHeaders: (res, path) => {
res.set('Access-Control-Allow-Origin', '*')
// Override Content-Type if necessary
if (contentTypes && contentTypes.length) {
overrideContentTypes(res, path)
}
}
})
} }
// Static assets // Static assets