From 63d733d60b3a7f5474c14035f24eb2f35947692c Mon Sep 17 00:00:00 2001 From: Gerben Date: Mon, 3 Aug 2020 19:06:51 +0200 Subject: [PATCH] Avoid file name errors when downloading Should fix issue #139 --- src/local-storage/download-page.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/local-storage/download-page.js b/src/local-storage/download-page.js index cfbc1b1a1..e6bf0cd7c 100644 --- a/src/local-storage/download-page.js +++ b/src/local-storage/download-page.js @@ -57,6 +57,14 @@ export function makeFilename({ folder, title, timestamp }) { export async function downloadBlob({ blob, filename, saveAs = false }) { const url = URL.createObjectURL(blob) + if (filename && filename.length > 200) { + // At least in Firefox on Linux, it appears a long filename (≥233 chars) can fail without throwing an error, so we cut it down already. + const i = filename.lastIndexOf('.') + const basename = (i !== -1) ? filename.substring(0, i) : filename + const extension = (i !== -1) ? filename.substring(i) : '' + filename = basename.substring(0, 199 - extension.length) + '…' + extension + } + const tryDownload = filename => browser.downloads.download({ url, filename, @@ -67,9 +75,12 @@ export async function downloadBlob({ blob, filename, saveAs = false }) { try { downloadId = await tryDownload(filename) } catch (err) { - // Possibly due to punctuation in the filename (Chromium is picky). if (err.message.includes('filename')) { + // Possibly due to punctuation in the filename (Chromium is picky). filename = filename.replace(/['?:~<>*|]/g, '-') // an empirically composed list. + // …or because Firefox chokes on multiple consecutive spaces. + filename = filename.replace(/\s+/g, ' ') + // Do a second attempt. downloadId = await tryDownload(filename) } }