Allow imports to contain missing attachment files, creating placeholder attachments with proper metadata
This commit is contained in:
parent
9c3349f251
commit
dc73c66d96
|
@ -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 = [
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user