Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: content-disposition filename regexp #87

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function normalizeMetadata(input?: File | Response | BufferLike | StreamL
}
if (input instanceof Response) {
const contentDisposition = input.headers.get("content-disposition")
const filename = contentDisposition && contentDisposition.match(/;\s*filename\*?=["']?(.*?)["']?$/i)
const filename = contentDisposition && contentDisposition.match(/filename=['"]?([^'";]+)['"]?(?:;|$)/i)
Touffy marked this conversation as resolved.
Show resolved Hide resolved
const urlName = filename && filename[1] || input.url && new URL(input.url).pathname.split("/").findLast(Boolean)
const decoded = urlName && decodeURIComponent(urlName)
// @ts-ignore allow coercion from null to zero
Expand Down
2 changes: 1 addition & 1 deletion test/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Deno.test("normalizeMetadata needs a filename along Responses with insufficient

Deno.test("normalizeMetadata guesses filename from Content-Disposition", () => {
const metadata = normalizeMetadata(new Response("four", {
headers: { "content-disposition": "attachment; filename=test.txt" }
headers: { "content-disposition": "attachment; filename=test.txt; size=0" }
}))
assertEquals(metadata, { uncompressedSize: 0n, encodedName, nameIsBuffer: false })
})
Expand Down