mirror of
https://github.com/tridactyl/tridactyl.git
synced 2026-09-10 07:16:33 -04:00
Fix #1091: use content-disposition for downloads
This commit is contained in:
parent
5b78e75a0c
commit
a2b98289b2
|
|
@ -50,7 +50,34 @@ export async function downloadUrl(url: string, saveAs: boolean) {
|
|||
urlToDownload = urlToSave.href
|
||||
}
|
||||
|
||||
const fileName = getDownloadFilenameForUrl(urlToSave)
|
||||
let fileName = getDownloadFilenameForUrl(urlToSave)
|
||||
if (urlToSave.protocol === "http:" || urlToSave.protocol === "https:") {
|
||||
const controller = new AbortController()
|
||||
const timeout = setTimeout(() => controller.abort(), 5000)
|
||||
try {
|
||||
const response = await fetch(urlToSave.href, {
|
||||
method: "HEAD",
|
||||
credentials: "omit",
|
||||
signal: controller.signal,
|
||||
})
|
||||
const disposition =
|
||||
response.ok && response.headers.get("content-disposition")
|
||||
const match = /(?:^|;)\s*filename\s*=\s*"?([^";]+)"?/i.exec(
|
||||
disposition || "",
|
||||
)
|
||||
const basename = match?.[1].trim().replace(
|
||||
/^.*[\\/][. ]*|^[. ]+|[<>:"|?*\p{Cc}]|[. ]+$/gu,
|
||||
"_",
|
||||
)
|
||||
if (basename)
|
||||
fileName = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(
|
||||
basename,
|
||||
)
|
||||
? `_${basename}`
|
||||
: basename
|
||||
} catch {}
|
||||
clearTimeout(timeout)
|
||||
}
|
||||
|
||||
// Save location limitations:
|
||||
// - download() can't save outside the downloads dir without popping
|
||||
|
|
|
|||
Loading…
Reference in a new issue