From 8b4bb62efcbe21261b1ed32891ab6c0f043aace4 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 26 Jan 2019 00:11:27 -0500 Subject: [PATCH] Don't load linked URLs during import Regression from 5a6a772ca2ae, I think --- .eslintrc | 1 + .../xpcom/translation/translate_item.js | 8 ++- test/tests/translateTest.js | 60 +++++++++++++++++++ 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/.eslintrc b/.eslintrc index debaefdb24..c884555ba4 100644 --- a/.eslintrc +++ b/.eslintrc @@ -8,6 +8,7 @@ "Zotero": false, "ZOTERO_CONFIG": false, "AddonManager": false, + "assert": false, "Cc": false, "Ci": false, "Components": false, diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js index b1cd971849..1d7b786c9e 100644 --- a/chrome/content/zotero/xpcom/translation/translate_item.js +++ b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -495,7 +495,7 @@ Zotero.Translate.ItemSaver.prototype = { let newAttachment; // determine whether to save files and attachments - let isLink = Zotero.MIME.isWebPageType(attachment.mimeType) + var isLink = Zotero.MIME.isWebPageType(attachment.mimeType) // .snapshot coming from most translators, .linkMode coming from RDF && (attachment.snapshot === false || attachment.linkMode == Zotero.Attachments.LINK_MODE_LINKED_URL); if (isLink || this.attachmentMode == Zotero.Translate.ItemSaver.ATTACHMENT_MODE_DOWNLOAD) { @@ -597,7 +597,7 @@ Zotero.Translate.ItemSaver.prototype = { } // At this point, must be a valid HTTP/HTTPS url - attachment.linkMode = "linked_file"; + attachment.linkMode = "linked_url"; newItem = yield Zotero.Attachments.linkFromURL({ url: attachment.url, parentItemID, @@ -775,7 +775,9 @@ Zotero.Translate.ItemSaver.prototype = { // Commit to saving attachmentCallback(attachment, 0); - if(attachment.snapshot === false || this.attachmentMode === Zotero.Translate.ItemSaver.ATTACHMENT_MODE_IGNORE) { + var isLink = attachment.snapshot === false + || attachment.linkMode == Zotero.Attachments.LINK_MODE_LINKED_URL; + if (isLink || this.attachmentMode === Zotero.Translate.ItemSaver.ATTACHMENT_MODE_IGNORE) { // if snapshot is explicitly set to false, attach as link attachment.linkMode = "linked_url"; let url, mimeType; diff --git a/test/tests/translateTest.js b/test/tests/translateTest.js index 946fffbe88..0e19cbb312 100644 --- a/test/tests/translateTest.js +++ b/test/tests/translateTest.js @@ -1,5 +1,6 @@ new function() { Components.utils.import("resource://gre/modules/osfile.jsm"); +Components.utils.import("resource://zotero-unit/httpd.js"); /** * Create a new translator that saves the specified items @@ -405,6 +406,65 @@ describe("Zotero.Translate", function() { assert.equal(newItems[0].getAttachments().length, 0); }); + it('import translators should save link attachments', async function () { + // Start a local server so we can make sure a web request isn't made for the URL + var port = 16213; + var baseURL = `http://127.0.0.1:${port}/`; + var httpd = new HttpServer(); + httpd.start(port); + var callCount = 0; + var handler = function (_request, response) { + callCount++; + response.setStatusLine(null, 200, "OK"); + response.write("TitleBody"); + }; + httpd.registerPathHandler("/1", { handle: handler }); + httpd.registerPathHandler("/2", { handle: handler }); + + var items = [{ + itemType: "book", + title: "Item", + attachments: [ + // With mimeType + { + itemType: "attachment", + linkMode: Zotero.Attachments.LINK_MODE_LINKED_URL, + title: "Link 1", + url: baseURL + "1", + mimeType: 'text/html' + }, + // Without mimeType + { + itemType: "attachment", + linkMode: Zotero.Attachments.LINK_MODE_LINKED_URL, + title: "Link 2", + url: baseURL + "2" + } + ] + }]; + + var newItems = itemsArrayToObject(await saveItemsThroughTranslator("import", items)); + + assert.equal(callCount, 0); + + var attachments = await Zotero.Items.getAsync(newItems.Item.getAttachments()); + assert.equal(attachments.length, 2); + + assert.equal(attachments[0].getField("title"), "Link 1"); + assert.equal(attachments[0].getField("url"), baseURL + "1"); + assert.equal(attachments[0].attachmentContentType, "text/html"); + assert.equal(attachments[0].attachmentLinkMode, Zotero.Attachments.LINK_MODE_LINKED_URL); + + assert.equal(attachments[1].getField("title"), "Link 2"); + assert.equal(attachments[1].getField("url"), baseURL + "2"); + assert.equal(attachments[1].attachmentLinkMode, Zotero.Attachments.LINK_MODE_LINKED_URL); + assert.equal(attachments[1].attachmentContentType, ''); + + await new Promise(function (resolve) { + httpd.stop(resolve); + }); + }); + it("import translators should save linked-URL attachments with savingAttachments: false", async function () { var json = [ {