From 56321a7a6d4b6efbd6f653adfab37c5ddee3b54e Mon Sep 17 00:00:00 2001 From: Tom Najdek Date: Wed, 30 Mar 2022 18:15:40 +0200 Subject: [PATCH] Fix regression: download() fails for non-http URLs (#2497) This fixes compatiblity with some addons that use Zotero.File.download to extract files from their XPI bundle. --- chrome/content/zotero/xpcom/file.js | 6 ++++-- test/tests/data/fake.xpi | Bin 0 -> 179 bytes test/tests/fileTest.js | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 test/tests/data/fake.xpi diff --git a/chrome/content/zotero/xpcom/file.js b/chrome/content/zotero/xpcom/file.js index 4104e6cc68..f6bb2978e9 100644 --- a/chrome/content/zotero/xpcom/file.js +++ b/chrome/content/zotero/xpcom/file.js @@ -448,10 +448,11 @@ Zotero.File = new function(){ this.download = async function (uri, path) { var uriStr = uri.spec || uri; + const isHTTP = uriStr.startsWith('http'); Zotero.debug(`Saving ${uriStr} to ${path.pathQueryRef || path}`); - if (Zotero.HTTP.browserIsOffline()) { + if (isHTTP && Zotero.HTTP.browserIsOffline()) { let msg = `Download failed: ${Zotero.appName} is currently offline`; Zotero.debug(msg, 2); throw new Error(msg); @@ -488,7 +489,8 @@ Zotero.File = new function(){ deferred.reject(new Error(msg)); return; } - if (responseStatus != 200) { + + if (isHTTP && responseStatus != 200) { let msg = `Download failed with response code ${responseStatus}`; Zotero.logError(msg); deferred.reject(new Error(msg)); diff --git a/test/tests/data/fake.xpi b/test/tests/data/fake.xpi new file mode 100644 index 0000000000000000000000000000000000000000..fd3dbdda26fe1ef58fe7fd5232780bf9dd073d79 GIT binary patch literal 179 zcmWIWW@h1HU}9ik2C3+ T;LXYgQpX5{en8qE#9;sc(aIv( literal 0 HcmV?d00001 diff --git a/test/tests/fileTest.js b/test/tests/fileTest.js index 3fbb4f3508..9739482c22 100644 --- a/test/tests/fileTest.js +++ b/test/tests/fileTest.js @@ -525,5 +525,13 @@ describe("Zotero.File", function () { assert.equal(fileSize, 1024 * 1024 * sizeInMB); } }); + + it("should extract a file from xpi", async function () { + const url = `jar:file://${getTestDataDirectory().path}/fake.xpi!/test.txt`; + const path = OS.Path.join(Zotero.getTempDirectory().path, 'xpi-extracted.txt'); + await Zotero.File.download(url, path); + const contents = await Zotero.File.getContentsAsync(path); + assert.equal(contents, 'Hello Zotero\n'); + }); }); })