Skip to content

Commit

Permalink
try to get filename from "Content-Disposition" header if present
Browse files Browse the repository at this point in the history
  • Loading branch information
Touffy committed Mar 11, 2020
1 parent cee9ab2 commit 6864304
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ export function normalizeInput(input: File | Response | BufferLike | StreamLike,
modDate: modDate || new Date(input.lastModified),
bytes: input.stream()
}
if (input instanceof Response) return {
encodedName: encodedName || encodeString(new URL(input.url).pathname.split("/").pop()),
modDate: modDate || new Date(input.headers.get("Last-Modified")),
bytes: input.body
if (input instanceof Response) {
const contentDisposition = input.headers.get("content-disposition")
const filename = contentDisposition && contentDisposition.match(/;\s*filename\*?=["']?(.*?)["']?$/i)
const urlName = filename && filename[1] || new URL(input.url).pathname.split("/").pop()
const decoded = urlName && decodeURIComponent(urlName)
return {
encodedName: encodedName || encodeString(decoded || new URL(input.url).pathname.split("/").pop()),
modDate: modDate || new Date(input.headers.get("Last-Modified")),
bytes: input.body
}
}

if (!encodedName || encodedName.length === 0) throw new Error("The file must have a name.")
Expand Down

0 comments on commit 6864304

Please sign in to comment.