From ad4c2c2e96b91a42a9c6bafc54dbfcd9b76e0793 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Sun, 3 Jul 2022 10:23:55 +0700 Subject: [PATCH] refactor: overrideContentTypes --- lolisafe.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lolisafe.js b/lolisafe.js index 5aca5e2..c708cf1 100644 --- a/lolisafe.js +++ b/lolisafe.js @@ -120,8 +120,9 @@ safe.use(bodyParser.json()) const cdnPages = [...config.pages] let setHeaders -const contentTypes = config.overrideContentTypes && Object.keys(config.overrideContentTypes) -const overrideContentTypes = (res, path) => { +const contentTypes = typeof config.overrideContentTypes === 'object' && + 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.) const relpath = path.replace(paths.uploads, '') if (relpath.indexOf('/', 1) === -1) { @@ -201,8 +202,8 @@ if (config.cacheControl) { if (config.serveFilesWithNode) { initServeStaticUploads({ setHeaders: (res, path) => { - // Override Content-Type if necessary - if (contentTypes && contentTypes.length) { + // Override Content-Type header if necessary + if (overrideContentTypes) { overrideContentTypes(res, path) } // If using CDN, cache uploads in CDN as well @@ -232,14 +233,12 @@ if (config.cacheControl) { next() }) } else if (config.serveFilesWithNode) { - initServeStaticUploads({ - setHeaders: (res, path) => { - // Override Content-Type if necessary - if (contentTypes && contentTypes.length) { - overrideContentTypes(res, path) - } - } - }) + const opts = {} + // Override Content-Type header if necessary + if (overrideContentTypes) { + opts.setHeaders = overrideContentTypes + } + initServeStaticUploads(opts) } // Static assets