Merge pull request #4187 from Nachtalb/na/filename-in-contentDisposition

Add proper filename to the content-disposition header
This commit is contained in:
Max Leiter 2021-04-11 20:29:23 -07:00 committed by GitHub
commit db9eb05dfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 23 deletions

View file

@ -45,6 +45,7 @@
"chalk": "4.1.0", "chalk": "4.1.0",
"cheerio": "1.0.0-rc.5", "cheerio": "1.0.0-rc.5",
"commander": "7.2.0", "commander": "7.2.0",
"content-disposition": "0.5.3",
"express": "4.17.1", "express": "4.17.1",
"file-type": "16.2.0", "file-type": "16.2.0",
"filenamify": "4.2.0", "filenamify": "4.2.0",

View file

@ -10,26 +10,28 @@ const readChunk = require("read-chunk");
const crypto = require("crypto"); const crypto = require("crypto");
const isUtf8 = require("is-utf8"); const isUtf8 = require("is-utf8");
const log = require("../log"); const log = require("../log");
const contentDisposition = require("content-disposition");
// List of allowed mime types that can be rendered in browser // Map of allowed mime types to their respecive default filenames
// without forcing it to be downloaded // that will be rendered in browser without forcing them to be downloaded
const inlineContentDispositionTypes = [ const inlineContentDispositionTypes = {
"application/ogg", "application/ogg": "media.ogx",
"audio/midi", "audio/midi": "audio.midi",
"audio/mpeg", "audio/mpeg": "audio.mp3",
"audio/ogg", "audio/ogg": "audio.ogg",
"audio/vnd.wave", "audio/vnd.wave": "audio.wav",
"image/bmp", "audio/flac": "audio.flac",
"image/gif", "image/bmp": "image.bmp",
"image/jpeg", "image/gif": "image.gif",
"image/png", "image/jpeg": "image.jpg",
"image/webp", "image/png": "image.png",
"image/avif", "image/webp": "image.webp",
"text/plain", "image/avif": "image.avif",
"video/mp4", "text/plain": "text.txt",
"video/ogg", "video/mp4": "video.mp4",
"video/webm", "video/ogg": "video.ogv",
]; "video/webm": "video.webm",
};
const uploadTokens = new Map(); const uploadTokens = new Map();
@ -92,9 +94,20 @@ class Uploader {
} }
// Force a download in the browser if it's not an allowed type (binary or otherwise unknown) // Force a download in the browser if it's not an allowed type (binary or otherwise unknown)
const contentDisposition = inlineContentDispositionTypes.includes(detectedMimeType) let slug = req.params.slug;
? "inline" const isInline = detectedMimeType in inlineContentDispositionTypes;
: "attachment"; let disposition = isInline ? "inline" : "attachment";
if (!slug && isInline) {
slug = inlineContentDispositionTypes[detectedMimeType];
}
if (slug) {
disposition = contentDisposition(slug.trim(), {
fallback: false,
type: disposition,
});
}
if (detectedMimeType === "audio/vnd.wave") { if (detectedMimeType === "audio/vnd.wave") {
// Send a more common mime type for wave audio files // Send a more common mime type for wave audio files
@ -102,7 +115,7 @@ class Uploader {
detectedMimeType = "audio/wav"; detectedMimeType = "audio/wav";
} }
res.setHeader("Content-Disposition", contentDisposition); res.setHeader("Content-Disposition", disposition);
res.setHeader("Cache-Control", "max-age=86400"); res.setHeader("Cache-Control", "max-age=86400");
res.contentType(detectedMimeType); res.contentType(detectedMimeType);