- Customizable attachment export, thanks to DBrickShaw
- Fix an issue importing collections
This commit is contained in:
parent
dec907569a
commit
7756620490
|
@ -563,12 +563,12 @@ Zotero.Translate.Sandbox = {
|
||||||
* @param {SandboxCollection} collection
|
* @param {SandboxCollection} collection
|
||||||
*/
|
*/
|
||||||
"_collectionDone":function(translate, collection) {
|
"_collectionDone":function(translate, collection) {
|
||||||
if(this._libraryID == false) {
|
if(translate._libraryID == false) {
|
||||||
this.newCollections.push(collection);
|
translate.newCollections.push(collection);
|
||||||
translate._runHandler("collectionDone", collection);
|
translate._runHandler("collectionDone", collection);
|
||||||
} else {
|
} else {
|
||||||
var newCollection = translate._itemSaver.saveCollection(collection);
|
var newCollection = translate._itemSaver.saveCollection(collection);
|
||||||
this.newCollections.push(newCollection);
|
translate.newCollections.push(newCollection);
|
||||||
translate._runHandler("collectionDone", newCollection);
|
translate._runHandler("collectionDone", newCollection);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -572,9 +572,9 @@ Zotero.Translate.ItemGetter.prototype = {
|
||||||
|
|
||||||
"exportFiles":function(dir, extension) {
|
"exportFiles":function(dir, extension) {
|
||||||
// generate directory
|
// generate directory
|
||||||
var directory = Components.classes["@mozilla.org/file/local;1"].
|
this._exportFileDirectory = Components.classes["@mozilla.org/file/local;1"].
|
||||||
createInstance(Components.interfaces.nsILocalFile);
|
createInstance(Components.interfaces.nsILocalFile);
|
||||||
directory.initWithFile(dir.parent);
|
this._exportFileDirectory.initWithFile(dir.parent);
|
||||||
|
|
||||||
// delete this file if it exists
|
// delete this file if it exists
|
||||||
if(dir.exists()) {
|
if(dir.exists()) {
|
||||||
|
@ -583,25 +583,18 @@ Zotero.Translate.ItemGetter.prototype = {
|
||||||
|
|
||||||
// get name
|
// get name
|
||||||
var name = dir.leafName;
|
var name = dir.leafName;
|
||||||
directory.append(name);
|
this._exportFileDirectory.append(name);
|
||||||
|
|
||||||
// create directory
|
// create directory
|
||||||
directory.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0700);
|
this._exportFileDirectory.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0700);
|
||||||
|
|
||||||
// generate a new location for the exported file, with the appropriate
|
// generate a new location for the exported file, with the appropriate
|
||||||
// extension
|
// extension
|
||||||
var location = Components.classes["@mozilla.org/file/local;1"].
|
var location = Components.classes["@mozilla.org/file/local;1"].
|
||||||
createInstance(Components.interfaces.nsILocalFile);
|
createInstance(Components.interfaces.nsILocalFile);
|
||||||
location.initWithFile(directory);
|
location.initWithFile(this._exportFileDirectory);
|
||||||
location.append(name+"."+extension);
|
location.append(name+"."+extension);
|
||||||
|
|
||||||
// create files directory
|
|
||||||
this._exportFileDirectory = Components.classes["@mozilla.org/file/local;1"].
|
|
||||||
createInstance(Components.interfaces.nsILocalFile);
|
|
||||||
this._exportFileDirectory.initWithFile(directory);
|
|
||||||
this._exportFileDirectory.append("files");
|
|
||||||
this._exportFileDirectory.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0700);
|
|
||||||
|
|
||||||
return location;
|
return location;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -610,40 +603,126 @@ Zotero.Translate.ItemGetter.prototype = {
|
||||||
*/
|
*/
|
||||||
"_attachmentToArray":function(attachment) {
|
"_attachmentToArray":function(attachment) {
|
||||||
var attachmentArray = this._itemToArray(attachment);
|
var attachmentArray = this._itemToArray(attachment);
|
||||||
|
|
||||||
var linkMode = attachment.attachmentLinkMode;
|
var linkMode = attachment.attachmentLinkMode;
|
||||||
|
|
||||||
// get mime type
|
// Get mime type
|
||||||
attachmentArray.mimeType = attachmentArray.uniqueFields.mimeType = attachment.attachmentMIMEType;
|
attachmentArray.mimeType = attachmentArray.uniqueFields.mimeType = attachment.attachmentMIMEType;
|
||||||
// get charset
|
// Get charset
|
||||||
attachmentArray.charset = attachmentArray.uniqueFields.charset = attachment.attachmentCharset;
|
attachmentArray.charset = attachmentArray.uniqueFields.charset = attachment.attachmentCharset;
|
||||||
|
|
||||||
if(linkMode != Zotero.Attachments.LINK_MODE_LINKED_URL && this._exportFileDirectory) {
|
if(linkMode != Zotero.Attachments.LINK_MODE_LINKED_URL && this._exportFileDirectory) {
|
||||||
// add path and filename if not an internet link
|
var exportDir = this._exportFileDirectory;
|
||||||
var file = attachment.getFile();
|
|
||||||
attachmentArray.path = "files/"+attachmentArray.itemID+"/"+file.leafName;
|
|
||||||
|
|
||||||
if(linkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE) {
|
// Add path and filename if not an internet link
|
||||||
// create a new directory
|
var attachFile = attachment.getFile();
|
||||||
var directory = Components.classes["@mozilla.org/file/local;1"].
|
if(attachFile) {
|
||||||
createInstance(Components.interfaces.nsILocalFile);
|
attachmentArray.defaultPath = "files/" + attachmentArray.itemID + "/" + attachFile.leafName;
|
||||||
directory.initWithFile(this._exportFileDirectory);
|
attachmentArray.filename = attachFile.leafName;
|
||||||
directory.append(attachmentArray.itemID);
|
|
||||||
// copy file
|
/**
|
||||||
try {
|
* Copies the attachment file to the specified relative path from the
|
||||||
directory.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0700);
|
* export directory.
|
||||||
file.copyTo(directory, attachmentArray.filename);
|
* @param {String} attachPath The path to which the file should be exported
|
||||||
} catch(e) {
|
* including the filename. If supporting files are included, they will be
|
||||||
attachmentArray.path = undefined;
|
* copied as well without any renaming.
|
||||||
}
|
* @param {Boolean} overwriteExisting Optional - If this is set to false, the
|
||||||
} else {
|
* function will throw an error when exporting a file would require an existing
|
||||||
// copy imported files from the Zotero directory
|
* file to be overwritten. If true, the file will be silently overwritten.
|
||||||
var directory = file.parent;
|
* defaults to false if not provided.
|
||||||
try {
|
*/
|
||||||
directory.copyTo(this._exportFileDirectory, attachmentArray.itemID);
|
attachmentArray.saveFile = function(attachPath, overwriteExisting) {
|
||||||
} catch(e) {
|
// Ensure a valid path is specified
|
||||||
attachmentArray.path = undefined;
|
if(attachPath === undefined || attachPath == "") {
|
||||||
}
|
throw new Error("ERROR_EMPTY_PATH");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the default value of overwriteExisting if it was not provided
|
||||||
|
if (overwriteExisting === undefined) {
|
||||||
|
overwriteExisting = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Separate the path into a list of subdirectories and the attachment filename,
|
||||||
|
// and initialize the required file objects
|
||||||
|
var targetFile = Components.classes["@mozilla.org/file/local;1"].
|
||||||
|
createInstance(Components.interfaces.nsILocalFile);
|
||||||
|
targetFile.initWithFile(exportDir);
|
||||||
|
targetFile.appendRelativePath(attachPath);
|
||||||
|
|
||||||
|
// First, check that we have not gone lower than exportDir in the hierarchy
|
||||||
|
var parent = targetFile, inExportFileDirectory;
|
||||||
|
while((parent = parent.parent)) {
|
||||||
|
if(exportDir.equals(parent)) {
|
||||||
|
inExportFileDirectory = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!inExportFileDirectory) {
|
||||||
|
throw new Error("Invalid path; attachment cannot be placed above export "+
|
||||||
|
"directory in the file hirarchy");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create intermediate directories if they don't exist
|
||||||
|
parent = targetFile;
|
||||||
|
while((parent = parent.parent) && !parent.exists()) {
|
||||||
|
parent.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0700);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete any existing file if overwriteExisting is set, or throw an exception
|
||||||
|
// if it is not
|
||||||
|
if(targetFile.exists()) {
|
||||||
|
if(overwriteExisting) {
|
||||||
|
targetFile.remove(false);
|
||||||
|
} else {
|
||||||
|
throw new Error("ERROR_FILE_EXISTS " + targetFile.leafName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var directory = targetFile.parent;
|
||||||
|
|
||||||
|
// The only attachments that can have multiple supporting files are of mime type
|
||||||
|
// text/html (specified in Attachments.getNumFiles())
|
||||||
|
if(attachment.attachmentMIMEType == "text/html"
|
||||||
|
&& Zotero.Attachments.getNumFiles(attachment) > 1) {
|
||||||
|
// Attachment is a snapshot with supporting files. Check if any of the
|
||||||
|
// supporting files would cause a name conflict, and build a list of transfers
|
||||||
|
// that should be performed
|
||||||
|
var copySrcs = [];
|
||||||
|
var files = attachment.getFile().parent.directoryEntries;
|
||||||
|
while (files.hasMoreElements()) {
|
||||||
|
file = files.getNext();
|
||||||
|
file.QueryInterface(Components.interfaces.nsIFile);
|
||||||
|
|
||||||
|
// Ignore the main attachment file (has already been checked for name conflict)
|
||||||
|
if(attachFile.equals(file)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove any existing files in the target destination if overwriteExisting
|
||||||
|
// is set, or throw an exception if it is not
|
||||||
|
var targetSupportFile = targetFile.parent.clone();
|
||||||
|
targetSupportFile.append(file.leafName);
|
||||||
|
if(targetSupportFile.exists()) {
|
||||||
|
if(overwriteExisting) {
|
||||||
|
targetSupportFile.remove(false);
|
||||||
|
} else {
|
||||||
|
throw new Error("ERROR_FILE_EXISTS " + targetSupportFile.leafName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
copySrcs.push(file.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
// No conflicts were detected or all conflicts were resolved, perform the copying
|
||||||
|
attachFile.copyTo(directory, targetFile.leafName);
|
||||||
|
for(var i = 0; i < copySrcs.length; i++) {
|
||||||
|
copySrcs[i].copyTo(directory, copySrcs[i].leafName);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Attachment is a single file
|
||||||
|
// Copy the file to the specified location
|
||||||
|
attachFile.copyTo(directory, targetFile.leafName);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user