Merge pull request #295 from adriandlam/fix/content-disposition-encoding

This commit is contained in:
Alan Grainger
2026-10-05 09:20:32 +13:00
3 changed files with 28 additions and 6 deletions
+2 -2
View File
@@ -12,6 +12,7 @@ import { isVideoAsset, resolveDownloadEndpoint, resolveImageEndpoint } from '../
import { pipeline } from 'stream/promises'
import { readableFromWeb } from '../utils/webStream'
import { log } from '../utils/log'
import { attachmentDisposition } from './download'
/**
* Stream an asset from Immich back to the client.
@@ -99,8 +100,7 @@ export async function assetBuffer (req: IncomingShareRequest, res: Response, ass
if (attachment) {
res.setHeader('X-Accel-Buffering', 'no')
if (asset.originalFileName) {
const filename = encodeURI(getFilename(asset, servedSize, servedMimeFrom(subpath, data)))
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${filename}`)
res.setHeader('Content-Disposition', attachmentDisposition(getFilename(asset, servedSize, servedMimeFrom(subpath, data))))
}
}
headerList.forEach(header => {
+9 -3
View File
@@ -131,9 +131,7 @@ export async function downloadAssets (res: Response, share: SharedLink, assets:
function startZipResponse (res: Response, share: SharedLink, archive: Archiver) {
res.setHeader('Content-Type', 'application/zip')
let filename = (sanitize(title(share)) || 'photos') + '.zip'
filename = encodeURI(filename)
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${filename}`)
res.setHeader('Content-Disposition', attachmentDisposition((sanitize(title(share)) || 'photos') + '.zip'))
// Hint to intermediate proxies (Nginx, etc.) not to buffer this response.
res.setHeader('X-Accel-Buffering', 'no')
res.setHeader('Cache-Control', 'no-store')
@@ -240,6 +238,14 @@ function enrichFromHeaders (asset: Asset, response: globalThis.Response): Asset
}
}
export function attachmentDisposition (filename: string): string {
// encodeURI leaves `,` and `;` raw, which split the header: Chrome reads a
// comma as a second Content-Disposition and refuses the download.
const encoded = encodeURIComponent(filename)
.replace(/['()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase())
return `attachment; filename*=UTF-8''${encoded}`
}
/**
* Extract a filename from a `Content-Disposition` header. Prefers the RFC 5987
* `filename*=UTF-8''...` form (percent-decoded) over the plain `filename=`.