Fix export failure on missing file attachments

This relies on synchronous file access, but making it async would require
rewriting all export translators that save files.
This commit is contained in:
Dan Stillman 2016-10-25 01:40:13 -04:00
parent 44e48700ef
commit 341d3d69b3
3 changed files with 38 additions and 8 deletions

View File

@ -42,11 +42,16 @@ Zotero.File = new function(){
this.pathToFile = function (pathOrFile) {
if (typeof pathOrFile == 'string') {
return new FileUtils.File(pathOrFile);
try {
if (typeof pathOrFile == 'string') {
return new FileUtils.File(pathOrFile);
}
else if (pathOrFile instanceof Ci.nsIFile) {
return pathOrFile;
}
}
else if (pathOrFile instanceof Ci.nsIFile) {
return pathOrFile;
catch (e) {
Zotero.logError(e);
}
throw new Error("Unexpected value '" + pathOrFile + "'");
}

View File

@ -728,15 +728,15 @@ Zotero.Translate.ItemGetter.prototype = {
var attachmentArray = Zotero.Utilities.Internal.itemToExportFormat(attachment, this.legacy);
var linkMode = attachment.attachmentLinkMode;
if(linkMode != Zotero.Attachments.LINK_MODE_LINKED_URL) {
var attachFile = attachment.getFile();
attachmentArray.localPath = attachFile.path;
attachmentArray.localPath = attachment.getFilePath();
if(this._exportFileDirectory) {
var exportDir = this._exportFileDirectory;
// Add path and filename if not an internet link
var attachFile = attachment.getFile();
if(attachFile) {
var attachFile = Zotero.File.pathToFile(attachmentArray.localPath);
// TODO: Make async, but that will require translator changes
if (attachFile.exists()) {
attachmentArray.defaultPath = "files/" + attachment.id + "/" + attachFile.leafName;
attachmentArray.filename = attachFile.leafName;

View File

@ -595,6 +595,31 @@ describe("Zotero.Translate", function() {
Zotero.Translators.get.restore();
});
});
describe("ItemSaver", function () {
describe("#saveItems()", function () {
it("should handle missing attachment files", function* () {
var item = yield importFileAttachment('test.png');
var path = item.getFilePath();
// Delete attachment file
yield OS.File.remove(path);
var translation = new Zotero.Translate.Export();
var tmpDir = yield getTempDirectory();
var exportDir = OS.Path.join(tmpDir, 'export');
translation.setLocation(Zotero.File.pathToFile(exportDir));
translation.setItems([item]);
translation.setTranslator('14763d24-8ba0-45df-8f52-b8d1108e7ac9'); // Zotero RDF
translation.setDisplayOptions({
exportFileData: true
});
yield translation.translate();
var exportFile = OS.Path.join(exportDir, 'export.rdf');
assert.isAbove((yield OS.File.stat(exportFile)).size, 0);
});
});
});
});
describe("Zotero.Translate.ItemGetter", function() {