From 57207493a2825035c5049b5335b1730fa420e32b Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Fri, 2 Oct 2020 04:58:35 +0700 Subject: [PATCH] Fixed setContentDisposition throwing errors --- config.sample.js | 2 ++ lolisafe.js | 17 +++++++++-------- package.json | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/config.sample.js b/config.sample.js index d205745..4046c7b 100644 --- a/config.sample.js +++ b/config.sample.js @@ -33,6 +33,8 @@ module.exports = { /* If you serve files with node, you can optionally choose to set Content-Disposition header into their original file names. This allows users to save files into their original file names. + + This will query the DB every time users access uploaded files as there's no caching mechanism. */ setContentDisposition: false, diff --git a/lolisafe.js b/lolisafe.js index dd8f167..4e985fe 100644 --- a/lolisafe.js +++ b/lolisafe.js @@ -69,20 +69,21 @@ let setHeaders = res => { const initServeStaticUploads = (opts = {}) => { if (config.setContentDisposition) { - opts.preSetHeaders = async (res, path) => { - // Do only if accessing files from uploads' root directory (i.e. not thumbs, etc.) - // and only if they're GET requests - if (path.indexOf('/', 1) === -1 && res.req.method === 'GET') { - const name = path.substring(1) - try { + opts.preSetHeaders = async (res, req, path, stat) => { + try { + // Do only if accessing files from uploads' root directory (i.e. not thumbs, etc.) + // and only if they are GET requests + const relpath = path.replace(paths.uploads, '') + if (relpath.indexOf('/', 1) === -1 && req.method === 'GET') { + const name = relpath.substring(1) const file = await db.table('files') .where('name', name) .select('original') .first() res.set('Content-Disposition', contentDisposition(file.original, { type: 'inline' })) - } catch (error) { - logger.error(error) } + } catch (error) { + logger.error(error) } } // serveStatic is just a modified express/serve-static module that allows specifying diff --git a/package.json b/package.json index f1af622..f6a1156 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "randomstring": "~1.1.5", "readline": "~1.3.0", "search-query-parser": "~1.5.5", - "serve-static": "git+https://git@github.com/BobbyWibowo/serve-static#60049dec396615ab738d29576a65432e4f9d7d4a", + "serve-static": "git+https://git@github.com/BobbyWibowo/serve-static#02c26587b25a7156a89dc05b617fce0aa90cefb9", "sharp": "~0.26.1", "sqlite3": "~5.0.0", "systeminformation": "~4.27.5"