diff --git a/index.js b/index.js index 7adaa99..0982aee 100644 --- a/index.js +++ b/index.js @@ -3,11 +3,12 @@ const path = require('node:path') const { fileURLToPath } = require('node:url') const { statSync } = require('node:fs') +const { basename } = require('node:path') const { glob } = require('glob') const fp = require('fastify-plugin') const send = require('@fastify/send') const encodingNegotiator = require('@fastify/accept-negotiator') -const contentDisposition = require('content-disposition') +const { create } = require('content-disposition') const dirList = require('./lib/dirList') @@ -21,6 +22,10 @@ const encodingExtensionMap = { gzip: '.gz' } +function contentDisposition (filename) { + return create(basename(filename)) +} + /** @type {import("fastify").FastifyPluginAsync} */ async function fastifyStatic (fastify, opts) { if (opts.serve !== false || opts.root !== undefined) { @@ -327,7 +332,7 @@ async function fastifyStatic (fastify, opts) { } if (metadata.error.code === 'ENOENT') { - // when preCompress is enabled and the path is a directory without a trailing slash + // when preCompress is enabled and the path is a directory without a trailing slash if (opts.preCompressed && encoding) { if (opts.redirect !== true) { const indexPathname = findIndexFile(pathname, options.root, options.index) diff --git a/package.json b/package.json index 95de475..ce5b326 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "dependencies": { "@fastify/accept-negotiator": "^2.0.0", "@fastify/send": "^4.0.0", - "content-disposition": "^1.0.1", + "content-disposition": "^2.0.1", "fastify-plugin": "^6.0.0", "fastq": "^1.17.1", "glob": "^13.0.0" diff --git a/test/static.test.js b/test/static.test.js index de2d413..0a8486a 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -1117,7 +1117,7 @@ test('download', async (t) => { const response = await fetch('http://localhost:' + fastify.server.address().port + '/foo/bar') t.assert.ok(response.ok) - t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename="index.html"') + t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename=index.html') t.assert.deepStrictEqual(response.status, 200) t.assert.deepStrictEqual(await response.text(), indexContent) genericResponseChecks(t, response) @@ -1129,7 +1129,7 @@ test('download', async (t) => { const response = await fetch('http://localhost:' + fastify.server.address().port + '/foo/bar/change') t.assert.ok(response.ok) t.assert.deepStrictEqual(response.status, 200) - t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename="hello-world.html"') + t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename=hello-world.html') t.assert.deepStrictEqual(await response.text(), indexContent) genericResponseChecks(t, response) }) @@ -1140,7 +1140,7 @@ test('download', async (t) => { const response = await fetch('http://localhost:' + fastify.server.address().port + '/root/path/override/test') t.assert.ok(response.ok) t.assert.deepStrictEqual(response.status, 200) - t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename="foo.html"') + t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename=foo.html') t.assert.deepStrictEqual(await response.text(), deepContent) genericResponseChecks(t, response) }) @@ -1151,7 +1151,7 @@ test('download', async (t) => { const response = await fetch('http://localhost:' + fastify.server.address().port + '/foo/bar/override') t.assert.ok(response.ok) t.assert.deepStrictEqual(response.status, 200) - t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename="hello-world.html"') + t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename=hello-world.html') t.assert.deepStrictEqual(response.headers.get('cache-control'), 'public, max-age=7200, immutable') t.assert.deepStrictEqual(await response.text(), indexContent) genericResponseChecks(t, response) @@ -1163,7 +1163,7 @@ test('download', async (t) => { const response = await fetch('http://localhost:' + fastify.server.address().port + '/foo/bar/override/2') t.assert.ok(response.ok) t.assert.deepStrictEqual(response.status, 200) - t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename="index.html"') + t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename=index.html') t.assert.deepStrictEqual(response.headers.get('accept-ranges'), null) t.assert.deepStrictEqual(await response.text(), indexContent) genericResponseChecks(t, response) @@ -1175,7 +1175,7 @@ test('download', async (t) => { const response = await fetch('http://localhost:' + fastify.server.address().port + '/root/path/override/test/change') t.assert.ok(response.ok) t.assert.deepStrictEqual(response.status, 200) - t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename="hello-world.html"') + t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename=hello-world.html') t.assert.deepStrictEqual(await response.text(), deepContent) genericResponseChecks(t, response) })