From cdaaf1ef54119269bf1e4822c47340983e8e11fe Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 12 Oct 2007 18:22:02 +0000 Subject: [PATCH] Fixes #759, RIS import creates unlinked webpage items outside of parent Also: - Made linkFromURL() always return the attachment itemID, performing a HEAD request asynchronously to get the MIME type if necessary - Removed test for linkFromURL() return value in Translate::_itemDone(), which was incorrect anyway - Removed a couple unused return assignments from linkFromURL() in _itemDone() --- chrome/content/zotero/xpcom/attachments.js | 51 ++++++++++++++-------- chrome/content/zotero/xpcom/translate.js | 12 ++--- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index e5f3fae09..e5211dbf5 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -327,6 +327,11 @@ Zotero.Attachments = new function(){ } + /* + * Create a link attachment from a URL + * + * Returns the itemID of the created attachment + */ function linkFromURL(url, sourceItemID, mimeType, title){ Zotero.debug('Linking attachment from URL'); @@ -335,29 +340,39 @@ Zotero.Attachments = new function(){ title = url.substring(url.lastIndexOf('/')+1); } - // If we have the title and mime type, skip loading - if (title && mimeType){ - return _addToDB(null, url, title, this.LINK_MODE_LINKED_URL, mimeType, - null, sourceItemID); + // Override MIME type to application/pdf if extension is .pdf -- + // workaround for sites that respond to the HEAD request with an + // invalid MIME type (https://www.zotero.org/trac/ticket/460) + var ext = _getExtensionFromURL(url); + if (ext == 'pdf') { + mimeType = 'application/pdf'; } - // Otherwise do a head request for the mime type + // Disable the Notifier if we're going to do a HEAD for the MIME type + if (!mimeType) { + var disabled = Zotero.Notifier.disable(); + } + + var itemID = _addToDB(null, url, title, this.LINK_MODE_LINKED_URL, + mimeType, null, sourceItemID); + + if (disabled) { + Zotero.Notifier.enable(); + } + + // If we don't have the MIME type, do a HEAD request for it Zotero.Utilities.HTTP.doHead(url, function(obj){ var mimeType = obj.channel.contentType; - // Override MIME type to application/pdf if extension is .pdf -- - // workaround for sites that respond to the HEAD request with an - // invalid MIME type (https://www.zotero.org/trac/ticket/460) - var ext = _getExtensionFromURL(url); - if (ext == 'pdf') { - mimeType = 'application/pdf'; + if (mimeType) { + var sql = "UPDATE itemAttachments SET mimeType=? WHERE itemID=?"; + Zotero.DB.query(sql, [mimeType, itemID]); } - _addToDB(null, url, title, Zotero.Attachments.LINK_MODE_LINKED_URL, - mimeType, null, sourceItemID); + Zotero.Notifier.trigger('add', 'item', itemID); }); - return true; + return itemID; } @@ -969,11 +984,11 @@ Zotero.Attachments = new function(){ + "mimeType, charsetID, path) VALUES (?,?,?,?,?,?)"; var bindParams = [ attachmentItem.getID(), - (sourceItemID ? {int:sourceItemID} : null), + sourceItemID ? {int:sourceItemID} : null, {int:linkMode}, - {string:mimeType}, - (charsetID ? {int:charsetID} : null), - (path ? {string:path} : null) + mimeType ? {string:mimeType} : null, + charsetID ? {int:charsetID} : null, + path ? {string:path} : null ]; Zotero.DB.query(sql, bindParams); diff --git a/chrome/content/zotero/xpcom/translate.js b/chrome/content/zotero/xpcom/translate.js index e3786acd6..3b9347259 100644 --- a/chrome/content/zotero/xpcom/translate.js +++ b/chrome/content/zotero/xpcom/translate.js @@ -1099,11 +1099,6 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) { (item.mimeType ? item.mimeType : undefined), (item.title ? item.title : undefined)); Zotero.debug("Translate: created attachment; id is "+myID); - if(!myID) { - // if we didn't get an ID, don't continue adding - // notes, because we can't without knowing the ID - return; - } var newItem = Zotero.Items.get(myID); } else { Zotero.debug("Translate: not adding attachment: no path or url specified"); @@ -1260,7 +1255,7 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) { if(attachment.snapshot === false) { // if snapshot is explicitly set to false, attach as link if(attachment.document) { - attachmentID = Zotero.Attachments.linkFromURL(attachment.document.location.href, myID, + Zotero.Attachments.linkFromURL(attachment.document.location.href, myID, (attachment.mimeType ? attachment.mimeType : attachment.document.contentType), (attachment.title ? attachment.title : attachment.document.title)); } else { @@ -1268,7 +1263,7 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) { Zotero.debug("Translate: NOTICE: either mimeType or title is missing; attaching file will be slower"); } - attachmentID = Zotero.Attachments.linkFromURL(attachment.url, myID, + Zotero.Attachments.linkFromURL(attachment.url, myID, (attachment.mimeType ? attachment.mimeType : undefined), (attachment.title ? attachment.title : undefined)); } @@ -1308,6 +1303,7 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) { } } else if(this.type == "import") { // create new attachments attached here + attachment.itemType = 'attachment'; this._itemDone(attachment, myID); } } @@ -1880,7 +1876,7 @@ Zotero.Translate.prototype._exportGetItem = function() { // is off returnItemArray.attachments = new Array(); var attachments = returnItem.getAttachments(); - for each(attachmentID in attachments) { + for each(var attachmentID in attachments) { var attachment = Zotero.Items.get(attachmentID); var attachmentInfo = this._exportGetAttachment(attachment);