From 341d3d69b32055be6b7091fef5efa10ff1f691a6 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 25 Oct 2016 01:40:13 -0400 Subject: [PATCH] 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. --- chrome/content/zotero/xpcom/file.js | 13 +++++++--- .../xpcom/translation/translate_item.js | 8 +++--- test/tests/translateTest.js | 25 +++++++++++++++++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/chrome/content/zotero/xpcom/file.js b/chrome/content/zotero/xpcom/file.js index d89bd0761..a06493b1e 100644 --- a/chrome/content/zotero/xpcom/file.js +++ b/chrome/content/zotero/xpcom/file.js @@ -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 + "'"); } diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js index 434b18acd..4312c327d 100644 --- a/chrome/content/zotero/xpcom/translation/translate_item.js +++ b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -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; diff --git a/test/tests/translateTest.js b/test/tests/translateTest.js index 525836102..90cd87f59 100644 --- a/test/tests/translateTest.js +++ b/test/tests/translateTest.js @@ -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() {