Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/content-disposition.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/** Build a download disposition with an ASCII fallback and an RFC 5987 filename. */
export function getAttachmentContentDisposition(filename) {
const fallback = filename.replace(/["\\\r\n]/g, "_").replace(/[^\x20-\x7e]/g, "_") || "download"
return `attachment; filename="${fallback}"; filename*=UTF-8''${encodeURIComponent(filename)}`
const encodedFilename = encodeURIComponent(filename).replace(
/[!'()*]/g,
(character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`,
)
return `attachment; filename="${fallback}"; filename*=UTF-8''${encodedFilename}`
}
6 changes: 5 additions & 1 deletion lib/content-disposition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/** Build a download disposition with an ASCII fallback and an RFC 5987 filename. */
export function getAttachmentContentDisposition(filename: string): string {
const fallback = filename.replace(/["\\\r\n]/g, "_").replace(/[^\x20-\x7e]/g, "_") || "download"
return `attachment; filename="${fallback}"; filename*=UTF-8''${encodeURIComponent(filename)}`
const encodedFilename = encodeURIComponent(filename).replace(
/[!'()*]/g,
(character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`,
)
return `attachment; filename="${fallback}"; filename*=UTF-8''${encodedFilename}`
}
7 changes: 7 additions & 0 deletions tests/lib/content-disposition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ test("getAttachmentContentDisposition encodes Unicode and unsafe fallback charac
"attachment; filename=\"__ _final_.zip\"; filename*=UTF-8''%E6%8A%A5%E5%91%8A%20%22final%22.zip",
)
})

test("getAttachmentContentDisposition percent-encodes RFC 8187 delimiters", () => {
assert.equal(
getAttachmentContentDisposition("报告(O'Reilly)*.zip"),
"attachment; filename=\"__(O'Reilly)*.zip\"; filename*=UTF-8''%E6%8A%A5%E5%91%8A%28O%27Reilly%29%2A.zip",
)
})
Loading