Provide filename via Content-Disposition when accessing asset (#16809)

* Provide filename via Content-Disposition when accessing asset

* Remove unused const

---------

Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
Pascal Jufer
2023-04-11 17:52:09 +02:00
committed by GitHub
parent 3028c645d0
commit b4e9afcc88
3 changed files with 17 additions and 3 deletions

View File

@@ -93,8 +93,8 @@
"@directus/utils": "workspace:*",
"@godaddy/terminus": "4.11.2",
"@rollup/plugin-alias": "4.0.3",
"@rollup/plugin-virtual": "3.0.1",
"@rollup/plugin-node-resolve": "15.0.1",
"@rollup/plugin-virtual": "3.0.1",
"argon2": "0.30.3",
"async": "3.2.4",
"axios": "1.3.4",
@@ -104,6 +104,7 @@
"chalk": "4.1.2",
"chokidar": "3.5.3",
"commander": "9.5.0",
"content-disposition": "0.5.4",
"cookie-parser": "1.4.6",
"cors": "2.8.5",
"csv-parser": "3.0.0",
@@ -176,6 +177,7 @@
"@types/async": "3.2.18",
"@types/busboy": "1.5.0",
"@types/bytes": "3.1.1",
"@types/content-disposition": "0.5.5",
"@types/cookie-parser": "1.4.3",
"@types/cors": "2.8.13",
"@types/deep-diff": "1.0.2",

View File

@@ -1,5 +1,6 @@
import type { Range } from '@directus/storage';
import { parseJSON } from '@directus/utils';
import contentDisposition from 'content-disposition';
import { Router } from 'express';
import { merge, pick } from 'lodash-es';
import { ASSET_TRANSFORM_QUERY_KEYS, SYSTEM_ASSET_ALLOW_LIST } from '../constants.js';
@@ -166,7 +167,8 @@ router.get(
const { stream, file, stat } = await service.getAsset(id, transformation, range);
res.attachment(req.params['filename'] ?? file.filename_download);
const filename = req.params['filename'] ?? file.filename_download;
res.attachment(filename);
res.setHeader('Content-Type', file.type);
res.setHeader('Accept-Ranges', 'bytes');
res.setHeader('Cache-Control', getCacheControlHeader(req, getMilliseconds(env['ASSETS_CACHE_TTL']), false, true));
@@ -186,7 +188,7 @@ router.get(
}
if ('download' in req.query === false) {
res.removeHeader('Content-Disposition');
res.setHeader('Content-Disposition', contentDisposition(filename, { type: 'inline' }));
}
if (req.method.toLowerCase() === 'head') {