Skip to content

Commit

Permalink
Fixed setContentDisposition throwing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbyWibowo committed Oct 1, 2020
1 parent b589e07 commit 5720749
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 2 additions & 0 deletions config.sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
17 changes: 9 additions & 8 deletions lolisafe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5720749

Please sign in to comment.