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()
This commit is contained in:
Dan Stillman 2007-10-12 18:22:02 +00:00
parent cb0e7c16d2
commit cdaaf1ef54
2 changed files with 37 additions and 26 deletions

View File

@ -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){ function linkFromURL(url, sourceItemID, mimeType, title){
Zotero.debug('Linking attachment from URL'); Zotero.debug('Linking attachment from URL');
@ -335,16 +340,6 @@ Zotero.Attachments = new function(){
title = url.substring(url.lastIndexOf('/')+1); 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);
}
// Otherwise do a head request for the mime type
Zotero.Utilities.HTTP.doHead(url, function(obj){
var mimeType = obj.channel.contentType;
// Override MIME type to application/pdf if extension is .pdf -- // Override MIME type to application/pdf if extension is .pdf --
// workaround for sites that respond to the HEAD request with an // workaround for sites that respond to the HEAD request with an
// invalid MIME type (https://www.zotero.org/trac/ticket/460) // invalid MIME type (https://www.zotero.org/trac/ticket/460)
@ -353,11 +348,31 @@ Zotero.Attachments = new function(){
mimeType = 'application/pdf'; mimeType = 'application/pdf';
} }
_addToDB(null, url, title, Zotero.Attachments.LINK_MODE_LINKED_URL, // 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); 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;
if (mimeType) {
var sql = "UPDATE itemAttachments SET mimeType=? WHERE itemID=?";
Zotero.DB.query(sql, [mimeType, itemID]);
}
Zotero.Notifier.trigger('add', 'item', itemID);
}); });
return true; return itemID;
} }
@ -969,11 +984,11 @@ Zotero.Attachments = new function(){
+ "mimeType, charsetID, path) VALUES (?,?,?,?,?,?)"; + "mimeType, charsetID, path) VALUES (?,?,?,?,?,?)";
var bindParams = [ var bindParams = [
attachmentItem.getID(), attachmentItem.getID(),
(sourceItemID ? {int:sourceItemID} : null), sourceItemID ? {int:sourceItemID} : null,
{int:linkMode}, {int:linkMode},
{string:mimeType}, mimeType ? {string:mimeType} : null,
(charsetID ? {int:charsetID} : null), charsetID ? {int:charsetID} : null,
(path ? {string:path} : null) path ? {string:path} : null
]; ];
Zotero.DB.query(sql, bindParams); Zotero.DB.query(sql, bindParams);

View File

@ -1099,11 +1099,6 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
(item.mimeType ? item.mimeType : undefined), (item.mimeType ? item.mimeType : undefined),
(item.title ? item.title : undefined)); (item.title ? item.title : undefined));
Zotero.debug("Translate: created attachment; id is "+myID); 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); var newItem = Zotero.Items.get(myID);
} else { } else {
Zotero.debug("Translate: not adding attachment: no path or url specified"); 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(attachment.snapshot === false) {
// if snapshot is explicitly set to false, attach as link // if snapshot is explicitly set to false, attach as link
if(attachment.document) { 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.mimeType ? attachment.mimeType : attachment.document.contentType),
(attachment.title ? attachment.title : attachment.document.title)); (attachment.title ? attachment.title : attachment.document.title));
} else { } 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"); 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.mimeType ? attachment.mimeType : undefined),
(attachment.title ? attachment.title : undefined)); (attachment.title ? attachment.title : undefined));
} }
@ -1308,6 +1303,7 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
} }
} else if(this.type == "import") { } else if(this.type == "import") {
// create new attachments attached here // create new attachments attached here
attachment.itemType = 'attachment';
this._itemDone(attachment, myID); this._itemDone(attachment, myID);
} }
} }
@ -1880,7 +1876,7 @@ Zotero.Translate.prototype._exportGetItem = function() {
// is off // is off
returnItemArray.attachments = new Array(); returnItemArray.attachments = new Array();
var attachments = returnItem.getAttachments(); var attachments = returnItem.getAttachments();
for each(attachmentID in attachments) { for each(var attachmentID in attachments) {
var attachment = Zotero.Items.get(attachmentID); var attachment = Zotero.Items.get(attachmentID);
var attachmentInfo = this._exportGetAttachment(attachment); var attachmentInfo = this._exportGetAttachment(attachment);