From 8be83cedec1d1b9df63b2a1abdb4a0afb1ef69dc Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Mon, 19 Jan 2015 15:13:49 -0600 Subject: [PATCH 1/3] Allow file paths to be specified in attachment.url In translators, it may not always be clear whether the given URI is a URL or a local file path. Regression from 849803473a0386aacc86745466d56f0ff3463f48 (that piece of code was introduced by me) Re https://forums.zotero.org/discussion/45710/ris-import-from-endnote-mac-yosemite-to-zotero-standalone/ --- .../xpcom/translation/translate_item.js | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js index 1785b4fce9..922cee5d79 100644 --- a/chrome/content/zotero/xpcom/translation/translate_item.js +++ b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -229,25 +229,25 @@ Zotero.Translate.ItemSaver.prototype = { if(!attachment.path) { let url = Zotero.Attachments.cleanAttachmentURI(attachment.url); if (!url) { - let e = "Translate: Invalid attachment URL specified <" + attachment.url + ">"; - Zotero.debug(e, 2); - attachmentCallback(attachment, false, e); - return false; - } - attachment.url = url; - url = Components.classes["@mozilla.org/network/io-service;1"] - .getService(Components.interfaces.nsIIOService) - .newURI(url, null, null); // This cannot fail, since we check above - - // see if this is actually a file URL - if(url.scheme == "file") { + Zotero.debug("Translate: Invalid attachment.url specified <" + attachment.url + "> Will attempt to treat it as path."); attachment.path = attachment.url; attachment.url = false; - } else if(url.scheme != "http" && url.scheme != "https") { - let e = "Translate: " + url.scheme + " protocol is not allowed for attachments from translators."; - Zotero.debug(e, 2); - attachmentCallback(attachment, false, e); - return false; + } else { + attachment.url = url; + url = Components.classes["@mozilla.org/network/io-service;1"] + .getService(Components.interfaces.nsIIOService) + .newURI(url, null, null); // This cannot fail, since we check above + + // see if this is actually a file URL + if(url.scheme == "file") { + attachment.path = attachment.url; + attachment.url = false; + } else if(url.scheme != "http" && url.scheme != "https") { + let e = "Translate: " + url.scheme + " protocol is not allowed for attachments from translators."; + Zotero.debug(e, 2); + attachmentCallback(attachment, false, e); + return false; + } } } @@ -268,7 +268,12 @@ Zotero.Translate.ItemSaver.prototype = { var newItem = Zotero.Items.get(myID); } else { var file = this._parsePath(attachment.path); - if(!file) return; + if(!file) { + let e = "Translate: Could not parse attachment path <" + attachment.path + ">"; + Zotero.debug(e, 2); + attachmentCallback(attachment, false, e); + return false; + } if (attachment.url) { attachment.linkMode = "imported_url"; From cf9ec26eb4f518ecbe48a4b89d7af2a1b9e2cda7 Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Wed, 4 Feb 2015 02:54:13 -0600 Subject: [PATCH 2/3] Disallow attachment.path in web/search translators --- chrome/content/zotero/xpcom/translation/translate.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js index 9d7f5e27d2..d56cbdac61 100644 --- a/chrome/content/zotero/xpcom/translation/translate.js +++ b/chrome/content/zotero/xpcom/translation/translate.js @@ -631,6 +631,13 @@ Zotero.Translate.Sandbox = { for(var i=0; i Date: Wed, 4 Feb 2015 03:00:13 -0600 Subject: [PATCH 3/3] Don't promote attachment.url to attachment.path ...but allow demoting from path to url if necessary. --- .../xpcom/translation/translate_item.js | 91 +++++++++++-------- 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js index 922cee5d79..e5765763dd 100644 --- a/chrome/content/zotero/xpcom/translation/translate_item.js +++ b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -226,32 +226,64 @@ Zotero.Translate.ItemSaver.prototype = { return false; } - if(!attachment.path) { - let url = Zotero.Attachments.cleanAttachmentURI(attachment.url); - if (!url) { - Zotero.debug("Translate: Invalid attachment.url specified <" + attachment.url + "> Will attempt to treat it as path."); - attachment.path = attachment.url; - attachment.url = false; - } else { - attachment.url = url; - url = Components.classes["@mozilla.org/network/io-service;1"] - .getService(Components.interfaces.nsIIOService) - .newURI(url, null, null); // This cannot fail, since we check above - - // see if this is actually a file URL - if(url.scheme == "file") { - attachment.path = attachment.url; - attachment.url = false; - } else if(url.scheme != "http" && url.scheme != "https") { - let e = "Translate: " + url.scheme + " protocol is not allowed for attachments from translators."; + let done = false; + if (attachment.path) { + var file = this._parsePath(attachment.path); + if(!file) { + let asUrl = Zotero.Attachments.cleanAttachmentURI(attachment.path); + if (!attachment.url && !asUrl) { + let e = "Translate: Could not parse attachment path <" + attachment.path + ">"; Zotero.debug(e, 2); attachmentCallback(attachment, false, e); return false; + } else if (!attachment.url && asUrl) { + Zotero.debug("Translate: attachment path looks like a URI: " + attachment.path); + attachment.url = asUrl; + delete attachment.path; } + } else { + if (attachment.url) { + attachment.linkMode = "imported_url"; + var myID = Zotero.Attachments.importSnapshotFromFile(file, + attachment.url, attachment.title, attachment.mimeType, attachment.charset, + parentID); + } + else { + attachment.linkMode = "imported_file"; + var myID = Zotero.Attachments.importFromFile(file, parentID); + } + attachmentCallback(attachment, 100); + done = true; } } - if(!attachment.path) { + if(!done) { + let url = Zotero.Attachments.cleanAttachmentURI(attachment.url); + if (!url) { + let e = "Translate: Invalid attachment.url specified <" + attachment.url + ">"; + Zotero.debug(e, 2); + attachmentCallback(attachment, false, e); + return false; + } + + attachment.url = url; + url = Components.classes["@mozilla.org/network/io-service;1"] + .getService(Components.interfaces.nsIIOService) + .newURI(url, null, null); // This cannot fail, since we check above + + // see if this is actually a file URL + if(url.scheme == "file") { + let e = "Translate: Local file attachments cannot be specified in attachment.url"; + Zotero.debug(e, 2); + attachmentCallback(attachment, false, e); + return false; + } else if(url.scheme != "http" && url.scheme != "https") { + let e = "Translate: " + url.scheme + " protocol is not allowed for attachments from translators."; + Zotero.debug(e, 2); + attachmentCallback(attachment, false, e); + return false; + } + // At this point, must be a valid HTTP/HTTPS url attachment.linkMode = "linked_file"; try { @@ -265,27 +297,6 @@ Zotero.Translate.ItemSaver.prototype = { } Zotero.debug("Translate: Created attachment; id is "+myID, 4); attachmentCallback(attachment, 100); - var newItem = Zotero.Items.get(myID); - } else { - var file = this._parsePath(attachment.path); - if(!file) { - let e = "Translate: Could not parse attachment path <" + attachment.path + ">"; - Zotero.debug(e, 2); - attachmentCallback(attachment, false, e); - return false; - } - - if (attachment.url) { - attachment.linkMode = "imported_url"; - var myID = Zotero.Attachments.importSnapshotFromFile(file, - attachment.url, attachment.title, attachment.mimeType, attachment.charset, - parentID); - } - else { - attachment.linkMode = "imported_file"; - var myID = Zotero.Attachments.importFromFile(file, parentID); - } - attachmentCallback(attachment, 100); } var newItem = Zotero.Items.get(myID);