fix filenames

This will allow for pages without titles to be correctly saved
and for files to be downloaded with their correct filename
This commit is contained in:
David Reed 2023-01-01 16:04:49 -05:00
parent 22f0e12e95
commit 5119555f3f
No known key found for this signature in database
GPG key ID: 2211691D8A1EE72F

View file

@ -154,6 +154,35 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
); );
} }
// downloads
if (request.destination === 'document') {
const header = responseCtx.headers['content-disposition'];
console.log(
{ header },
/filename=/i.test(header),
/^\s*?attachment/i.test(header)
);
// validate header and test for filename
if (!/\s*?((inline|attachment);\s*?)filename=/i.test(header)) {
// if filename= wasn't specified then maybe the remote specified to download this as an attachment?
// if it's invalid then we can still possibly test for the attachment/inline type
const type = /^\s*?attachment/i.test(header)
? 'attachment'
: 'inline';
// set the filename
const [filename] = new URL(response.finalURL).pathname
.split('/')
.slice(-1);
responseCtx.headers[
'content-disposition'
] = `${type}; filename=${JSON.stringify(filename)}`;
}
}
if (responseCtx.headers['set-cookie']) { if (responseCtx.headers['set-cookie']) {
Promise.resolve( Promise.resolve(
ultraviolet.cookie.setCookies( ultraviolet.cookie.setCookies(