www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit f7a2ef66983ceaf2e58819fefdc4913e87709486
parent f04ae4ce47e0490c41ddd6de25ceaee6296e6644
Author: Dan Stillman <dstillman@zotero.org>
Date:   Mon,  9 Feb 2015 04:49:42 -0500

Merge pull request #614 from aurimasv/file-attachments

Allow file paths to be specified in attachment.url
Diffstat:
Mchrome/content/zotero/xpcom/translation/translate.js | 7+++++++
Mchrome/content/zotero/xpcom/translation/translate_item.js | 62+++++++++++++++++++++++++++++++++++++++-----------------------
2 files changed, 46 insertions(+), 23 deletions(-)

diff --git 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<item.attachments.length; i++) { var attachment = item.attachments[i]; + + // Web translators are not allowed to use attachment.path + if (attachment.path) { + if (!attachment.url) attachment.url = attachment.path; + delete attachment.path; + } + if(attachment.url) { // Remap attachment (but not link) URLs attachment.url = translate.resolveURL(attachment.url, attachment.snapshot === false); diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -226,14 +226,46 @@ Zotero.Translate.ItemSaver.prototype = { return false; } - if(!attachment.path) { + 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(!done) { let url = Zotero.Attachments.cleanAttachmentURI(attachment.url); if (!url) { - let e = "Translate: Invalid attachment URL specified <" + attachment.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) @@ -241,17 +273,17 @@ Zotero.Translate.ItemSaver.prototype = { // see if this is actually a file URL if(url.scheme == "file") { - attachment.path = attachment.url; - attachment.url = false; + 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; } - } - - if(!attachment.path) { + // At this point, must be a valid HTTP/HTTPS url attachment.linkMode = "linked_file"; try { @@ -265,22 +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) return; - - 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);