From dc73c66d962df46a398d1171b7f2892a0a917d56 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 11 Sep 2007 08:58:59 +0000 Subject: [PATCH] Allow imports to contain missing attachment files, creating placeholder attachments with proper metadata --- chrome/content/zotero/xpcom/attachments.js | 46 +++++++++++++++++++--- chrome/content/zotero/xpcom/translate.js | 22 ++++++----- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 65e7b47a0..dfbb40a83 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -33,6 +33,7 @@ Zotero.Attachments = new function(){ this.linkFromURL = linkFromURL; this.linkFromDocument = linkFromDocument; this.importFromDocument = importFromDocument; + this.createMissingAttachment = createMissingAttachment; this.getFileBaseNameFromItem = getFileBaseNameFromItem; this.createDirectoryForItem = createDirectoryForItem; this.getStorageDirectory = getStorageDirectory; @@ -117,7 +118,7 @@ Zotero.Attachments = new function(){ function importSnapshotFromFile(file, url, title, mimeType, charset, sourceItemID){ Zotero.debug('Importing snapshot from file'); - var charsetID = Zotero.CharacterSets.getID(charset); + var charsetID = charset ? Zotero.CharacterSets.getID(charset) : null; Zotero.DB.beginTransaction(); @@ -687,6 +688,21 @@ Zotero.Attachments = new function(){ */ + /* + * Create a new attachment with a missing file + */ + function createMissingAttachment(linkMode, file, url, title, mimeType, charset, sourceItemID) { + if (linkMode == this.LINK_MODE_LINKED_URL) { + throw ('Zotero.Attachments.createMissingAttachment() cannot be used to create linked URLs'); + } + + var charsetID = charset ? Zotero.CharacterSets.getID(charset) : null; + + return _addToDB(file, url, title, linkMode, mimeType, + charsetID, sourceItemID); + } + + /* * Returns a formatted string to use as the basename of an attachment * based on the metadata of the specified item and a format string @@ -803,6 +819,12 @@ Zotero.Attachments = new function(){ * descriptor for files outside the storage directory */ function getPath(file, linkMode) { + if (!file.exists()) { + throw ('Zotero.Attachments.getPath() cannot be called on non-existent file'); + } + + file.QueryInterface(Components.interfaces.nsILocalFile); + if (linkMode == self.LINK_MODE_IMPORTED_URL || linkMode == self.LINK_MODE_IMPORTED_FILE) { var storageDir = Zotero.getStorageDirectory(); @@ -867,10 +889,6 @@ Zotero.Attachments = new function(){ * Returns the itemID of the new attachment **/ function _addToDB(file, url, title, linkMode, mimeType, charsetID, sourceItemID, itemID){ - if (file) { - var path = getPath(file, linkMode); - } - Zotero.DB.beginTransaction(); if (sourceItemID){ @@ -904,6 +922,24 @@ Zotero.Attachments = new function(){ attachmentItem.save(); } + if (file) { + if (file.exists()) { + var path = getPath(file, linkMode); + } + // If file doesn't exist, create one temporarily so we can get the + // relative path (since getPath() doesn't work on non-existent files) + else if (linkMode == self.LINK_MODE_IMPORTED_URL || + linkMode == self.LINK_MODE_IMPORTED_FILE) { + var missingFile = self.createDirectoryForItem(attachmentItem.getID()); + missingFile.append(file.leafName); + missingFile.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0644); + var path = getPath(missingFile, linkMode); + var parentDir = missingFile.parent; + missingFile.remove(null); + parentDir.remove(null); + } + } + var sql = "INSERT INTO itemAttachments (itemID, sourceItemID, linkMode, " + "mimeType, charsetID, path) VALUES (?,?,?,?,?,?)"; var bindParams = [ diff --git a/chrome/content/zotero/xpcom/translate.js b/chrome/content/zotero/xpcom/translate.js index 193dc7158..14e5b9874 100644 --- a/chrome/content/zotero/xpcom/translate.js +++ b/chrome/content/zotero/xpcom/translate.js @@ -1118,21 +1118,25 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) { var uri = IOService.newURI(item.path, "", null); var file = uri.QueryInterface(Components.interfaces.nsIFileURL).file; - if(item.url) { - // import from nsIFile + if (!file.exists()) { + var myID = Zotero.Attachments.createMissingAttachment( + item.url ? Zotero.Attachments.LINK_MODE_IMPORTED_URL + : Zotero.Attachments.LINK_MODE_IMPORTED_FILE, + file, item.url ? item.url : null, item.title, + item.mimeType, item.charset, attachedTo); + } + else if (item.url) { var myID = Zotero.Attachments.importSnapshotFromFile(file, - item.url, item.title, item.mimeType, - (item.charset ? item.charset : null), attachedTo); - var newItem = Zotero.Items.get(myID); - } else { - // import from nsIFile + item.url, item.title, item.mimeType, item.charset, + attachedTo); + } + else { var myID = Zotero.Attachments.importFromFile(file, attachedTo); - // get attachment item - var newItem = Zotero.Items.get(myID); } } var typeID = Zotero.ItemTypes.getID("attachment"); + var newItem = Zotero.Items.get(myID); // add note if necessary if(item.note) {