Skip to content

Node.js based forum software built for the modern web

fix(uploads): ugly filenames on uploaded asset downloading

Julian Lamf96208a0c80a
diff --git a/src/middleware/index.js b/src/middleware/index.js
index b0aaf3dbd4..4151dbbd26 100644
--- a/src/middleware/index.js
+++ b/src/middleware/index.js
@@ -28,6 +28,10 @@ var delayCache = LRU({
var middleware = module.exports;
+middleware.regexes = {
+ timestampedUpload: /^\d+-.+$/,
+};
+
middleware.applyCSRF = csrf();
middleware.ensureLoggedIn = ensureLoggedIn.ensureLoggedIn(nconf.get('relative_path') + '/login');
@@ -218,3 +222,14 @@ middleware.buildSkinAsset = function (req, res, next) {
setImmediate(next);
}
};
+
+middleware.trimUploadTimestamps = (req, res, next) => {
+ // Check match
+ let basename = path.basename(req.path);
+ if (req.path.startsWith('/uploads/files/') && middleware.regexes.timestampedUpload.test(basename)) {
+ basename = basename.slice(14);
+ res.header('Content-Disposition', 'inline; filename="' + basename + '"');
+ }
+
+ return next();
+};
diff --git a/src/routes/index.js b/src/routes/index.js
index d82c6b2b09..0d651db41f 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -159,7 +159,7 @@ module.exports = function (app, middleware, callback) {
});
statics.forEach(function (obj) {
- app.use(relativePath + obj.route, express.static(obj.path, staticOptions));
+ app.use(relativePath + obj.route, middleware.trimUploadTimestamps, express.static(obj.path, staticOptions));
});
app.use(relativePath + '/uploads', function (req, res) {
res.redirect(relativePath + '/assets/uploads' + req.path + '?' + meta.config['cache-buster']);