From 6dd94f0a3ae2be60b0c6ebd4c27559f679adaaad Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Sat, 2 Mar 2013 18:45:38 -0600 Subject: [PATCH 1/2] When mimeType is not supplied, try to fetch it from server and redo checks for automatic snapshots. --- chrome/content/zotero/xpcom/attachments.js | 4 ++- chrome/content/zotero/xpcom/mime.js | 2 ++ .../xpcom/translation/translate_item.js | 28 ++++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 0e10a7465..43ec1dc6f 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -30,6 +30,8 @@ Zotero.Attachments = new function(){ this.LINK_MODE_LINKED_URL = 3; this.BASE_PATH_PLACEHOLDER = 'attachments:'; + this.SNAPSHOT_MIMETYPES = ["text/html", "application/xhtml+xml"]; + this.importFromFile = importFromFile; this.linkFromFile = linkFromFile; this.importSnapshotFromFile = importSnapshotFromFile; @@ -574,7 +576,7 @@ Zotero.Attachments = new function(){ }; } - if (mimeType === 'text/html' || mimeType === 'application/xhtml+xml') { + if (this.SNAPSHOT_MIMETYPES.indexOf(mimeType) != -1) { var sync = true; // Load WebPageDump code diff --git a/chrome/content/zotero/xpcom/mime.js b/chrome/content/zotero/xpcom/mime.js index ca1d50dcf..a89f167d4 100644 --- a/chrome/content/zotero/xpcom/mime.js +++ b/chrome/content/zotero/xpcom/mime.js @@ -330,6 +330,8 @@ Zotero.MIME = new function(){ var mimeType = xmlhttp.channel.contentType; } + if(!mimeType) mimeType = 'application/octet-stream'; //unknown item type according to RFC 2046 section 4.5.1 + var nsIURL = Components.classes["@mozilla.org/network/standard-url;1"] .createInstance(Components.interfaces.nsIURL); nsIURL.spec = url; diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js index ffcd5a35f..e64d7e405 100644 --- a/chrome/content/zotero/xpcom/translation/translate_item.js +++ b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -336,10 +336,16 @@ Zotero.Translate.ItemSaver.prototype = { // Determine whether to save an attachment if(attachment.snapshot !== false) { if(attachment.document - || (attachment.mimeType && attachment.mimeType == "text/html")) { - if(!Zotero.Prefs.get("automaticSnapshots")) return; + || (attachment.mimeType && Zotero.Attachments.SNAPSHOT_MIMETYPES.indexOf(attachment.mimeType) != -1)) { + if(!Zotero.Prefs.get("automaticSnapshots")) { + Zotero.debug("Translate: Automatic snapshots are disabled. Skipping.", 4); + return; + } } else { - if(!Zotero.Prefs.get("downloadAssociatedFiles")) return; + if(!Zotero.Prefs.get("downloadAssociatedFiles")) { + Zotero.debug("Translate: File attachments are disabled. Skipping.", 4); + return; + } } } @@ -399,12 +405,26 @@ Zotero.Translate.ItemSaver.prototype = { // Save attachment if snapshot pref enabled or not HTML // (in which case downloadAssociatedFiles applies) } else { + if(!attachment.mimeType && attachment.mimeType !== '') { //in case '' indicates unknwon mime type at some point + Zotero.debug("Translate: No mimeType specified for a possible snapshot. Trying to determine mimeType.", 4); + var me = this; + try { + Zotero.MIME.getMIMETypeFromURL(attachment.url, function (mimeType, hasNativeHandler) { + attachment.mimeType = mimeType; + me._saveAttachmentDownload(attachment, parentID, attachmentCallback); + }, this._cookieSandbox); + } catch(e) { + Zotero.debug("Translate: Error adding attachment "+attachment.url, 2); + attachmentCallback(attachment, false, e); + } + return; + } var mimeType = (attachment.mimeType ? attachment.mimeType : null); var title = (attachment.title ? attachment.title : null); var fileBaseName = Zotero.Attachments.getFileBaseNameFromItem(parentID); try { - Zotero.debug('Importing attachment from URL'); + Zotero.debug('Translate: Importing attachment from URL', 4); attachment.linkMode = "imported_url"; Zotero.Attachments.importFromURL(attachment.url, parentID, title, fileBaseName, null, mimeType, this._libraryID, function(status, err) { From f64d0879264f7b681c8d27063d35c0df36cdab47 Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Sat, 2 Mar 2013 19:14:38 -0600 Subject: [PATCH 2/2] Don't set unknown mimeTypes to "application/octet-stream" --- chrome/content/zotero/xpcom/mime.js | 2 -- chrome/content/zotero/xpcom/translation/translate_item.js | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/chrome/content/zotero/xpcom/mime.js b/chrome/content/zotero/xpcom/mime.js index a89f167d4..ca1d50dcf 100644 --- a/chrome/content/zotero/xpcom/mime.js +++ b/chrome/content/zotero/xpcom/mime.js @@ -330,8 +330,6 @@ Zotero.MIME = new function(){ var mimeType = xmlhttp.channel.contentType; } - if(!mimeType) mimeType = 'application/octet-stream'; //unknown item type according to RFC 2046 section 4.5.1 - var nsIURL = Components.classes["@mozilla.org/network/standard-url;1"] .createInstance(Components.interfaces.nsIURL); nsIURL.spec = url; diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js index e64d7e405..54df7d162 100644 --- a/chrome/content/zotero/xpcom/translation/translate_item.js +++ b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -405,12 +405,12 @@ Zotero.Translate.ItemSaver.prototype = { // Save attachment if snapshot pref enabled or not HTML // (in which case downloadAssociatedFiles applies) } else { - if(!attachment.mimeType && attachment.mimeType !== '') { //in case '' indicates unknwon mime type at some point + if(!attachment.mimeType && attachment.mimeType !== '') { Zotero.debug("Translate: No mimeType specified for a possible snapshot. Trying to determine mimeType.", 4); var me = this; try { Zotero.MIME.getMIMETypeFromURL(attachment.url, function (mimeType, hasNativeHandler) { - attachment.mimeType = mimeType; + attachment.mimeType = mimeType || ''; me._saveAttachmentDownload(attachment, parentID, attachmentCallback); }, this._cookieSandbox); } catch(e) {