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.
This commit is contained in:
Tom Najdek 2022-03-30 18:15:40 +02:00 committed by GitHub
parent 776769f480
commit 56321a7a6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 2 deletions

View file

@ -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));

BIN
test/tests/data/fake.xpi Normal file

Binary file not shown.

View file

@ -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');
});
});
})