Fixed setContentDisposition throwing errors

This commit is contained in:
Bobby Wibowo 2020-10-02 04:58:35 +07:00
parent b589e070d8
commit 57207493a2
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
3 changed files with 12 additions and 9 deletions

View File

@ -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,

View File

@ -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

View File

@ -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"