From 5a6a772ca2aefd72c90430f590b25a19ee5d9ba3 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 8 Jan 2019 02:07:15 -0500 Subject: [PATCH] Fix linked-URL attachments not being saved to groups without files It looks like this may have been broken for years. --- .../xpcom/translation/translate_item.js | 13 ++++-- test/tests/translateTest.js | 46 +++++++++++++++++-- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js index c9fe751858..ad4377c342 100644 --- a/chrome/content/zotero/xpcom/translation/translate_item.js +++ b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -445,7 +445,11 @@ Zotero.Translate.ItemSaver.prototype = { _canSaveAttachment: function (attachment) { - if (this.attachmentMode == Zotero.Translate.ItemSaver.ATTACHMENT_MODE_DOWNLOAD) { + // Always save link attachments + 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) { if (!attachment.url && !attachment.document) { Zotero.debug("Translate: Not adding attachment: no URL specified"); return false; @@ -489,9 +493,12 @@ Zotero.Translate.ItemSaver.prototype = { _saveAttachment: Zotero.Promise.coroutine(function* (attachment, parentItemID, attachmentCallback) { try { let newAttachment; - + // determine whether to save files and attachments - if (this.attachmentMode == Zotero.Translate.ItemSaver.ATTACHMENT_MODE_DOWNLOAD) { + let 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) { newAttachment = yield this._saveAttachmentDownload.apply(this, arguments); } else if (this.attachmentMode == Zotero.Translate.ItemSaver.ATTACHMENT_MODE_FILE) { newAttachment = yield this._saveAttachmentFile.apply(this, arguments); diff --git a/test/tests/translateTest.js b/test/tests/translateTest.js index 3bad97e6f9..e70ef71ec0 100644 --- a/test/tests/translateTest.js +++ b/test/tests/translateTest.js @@ -6,7 +6,7 @@ Components.utils.import("resource://gre/modules/osfile.jsm"); * @param {String} translatorType - "import" or "web" * @param {Object} items - items as translator JSON */ -function saveItemsThroughTranslator(translatorType, items) { +function saveItemsThroughTranslator(translatorType, items, translateOptions = {}) { let tyname; if (translatorType == "web") { tyname = "Web"; @@ -35,7 +35,7 @@ function saveItemsThroughTranslator(translatorType, items) { " item.complete();\n"+ " }\n"+ "}")); - return translate.translate().then(function(items) { + return translate.translate(translateOptions).then(function(items) { if (browser) Zotero.Browser.deleteHiddenBrowser(browser); return items; }); @@ -404,7 +404,47 @@ describe("Zotero.Translate", function() { assert.equal(newItems[0].getField("title"), "Container Item"); assert.equal(newItems[0].getAttachments().length, 0); }); - + + it("import translators should save linked-URL attachments with savingAttachments: false", async function () { + var json = [ + { + itemType: "journalArticle", + title: "Parent Item", + attachments: [ + // snapshot: false + { + title: "Link", + mimeType: "text/html", + url: "http://example.com", + snapshot: false + }, + // linkMode (used by RDF import) + { + title: "Link", + mimeType: "text/html", + url: "http://example.com", + linkMode: Zotero.Attachments.LINK_MODE_LINKED_URL + } + ] + } + ]; + + var newItems = itemsArrayToObject( + await saveItemsThroughTranslator( + "import", + json, + { + saveAttachments: false + } + ) + ); + var attachmentIDs = newItems["Parent Item"].getAttachments(); + assert.lengthOf(attachmentIDs, 2); + var attachments = await Zotero.Items.getAsync(attachmentIDs); + assert.equal(attachments[0].attachmentLinkMode, Zotero.Attachments.LINK_MODE_LINKED_URL); + assert.equal(attachments[1].attachmentLinkMode, Zotero.Attachments.LINK_MODE_LINKED_URL); + }); + it('web translators should set accessDate to current date', function* () { let myItem = { "itemType":"webpage",