From 676abca671bd7712abfdac5709b2865a0a67ca68 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 13 Aug 2022 16:17:57 -0400 Subject: [PATCH] Fix importing of standalone attachments Maybe has been broken for years? https://forums.zotero.org/discussion/99020/warning-data-loss-when-exporting-collections --- chrome/content/zotero/xpcom/attachments.js | 17 +++++-- .../xpcom/translation/translate_item.js | 1 + test/tests/importExportTest.js | 46 +++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index b7b4cd097b..af7313a95c 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -270,7 +270,7 @@ Zotero.Attachments = new function(){ /** - * @param {Object} options - 'file', 'url', 'title', 'contentType', 'charset', 'parentItemID', 'singleFile' + * @param {Object} options - 'file', 'url', 'title', 'contentType', 'charset', 'libraryID', 'parentItemID', 'singleFile' * @param {Object} [options.saveOptions] - Options to pass to Zotero.Item::save() * @return {Promise} */ @@ -287,10 +287,22 @@ Zotero.Attachments = new function(){ var title = options.title; var contentType = options.contentType; var charset = options.charset; + var libraryID = options.libraryID; var parentItemID = options.parentItemID; var saveOptions = options.saveOptions; - if (!parentItemID) { + if (parentItemID) { + libraryID = Zotero.Items.getLibraryAndKeyFromID(parentItemID).libraryID; + } + else if (contentType == 'text/html') { + throw new Error("parentItemID not provided"); + } + else if (!libraryID) { + throw new Error("parentItemID or libraryID must be provided"); + } + + // Webpage snapshots must have parent items + if (!parentItemID && contentType == 'text/html') { throw new Error("parentItemID not provided"); } @@ -299,7 +311,6 @@ Zotero.Attachments = new function(){ yield Zotero.DB.executeTransaction(async function () { // Create a new attachment attachmentItem = new Zotero.Item('attachment'); - let {libraryID, key: parentKey} = Zotero.Items.getLibraryAndKeyFromID(parentItemID); attachmentItem.libraryID = libraryID; attachmentItem.setField('title', title); attachmentItem.setField('url', url); diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js index 0a9b4a0585..9302aa4ac1 100644 --- a/chrome/content/zotero/xpcom/translation/translate_item.js +++ b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -668,6 +668,7 @@ Zotero.Translate.ItemSaver.prototype = { title: attachment.title, contentType: attachment.mimeType, charset: attachment.charset, + libraryID: this._libraryID, parentItemID, collections: !parentItemID ? this._collections : undefined, saveOptions: this._saveOptions, diff --git a/test/tests/importExportTest.js b/test/tests/importExportTest.js index d670774174..e1eead5695 100644 --- a/test/tests/importExportTest.js +++ b/test/tests/importExportTest.js @@ -94,5 +94,51 @@ describe("Import/Export", function () { assert.sameMembers(newNote1.relatedItems, [newNote2]); assert.sameMembers(newNote2.relatedItems, [newNote1]); }); + + it("should import standalone PDF attachment", async function () { + var rdf = ` + + attachment + + + + https://example.com + + + 2022-07-22 06:36:31 + Test PDF + 1 + application/pdf + + +`; + var libraryID = Zotero.Libraries.userLibraryID; + var tempDir = await getTempDirectory(); + var file = OS.Path.join(tempDir, 'export.rdf'); + await Zotero.File.putContentsAsync(file, rdf); + var folder = OS.Path.join(tempDir, 'files', '1234'); + await OS.File.makeDir(folder, { from: tempDir }); + await OS.File.copy( + OS.Path.join(OS.Path.join(getTestDataDirectory().path, 'test.pdf')), + OS.Path.join(folder, 'test.pdf') + ); + + var translation = new Zotero.Translate.Import(); + translation.setLocation(Zotero.File.pathToFile(file)); + let translators = await translation.getTranslators(); + translation.setTranslator(translators[0]); + var newItem = (await translation.translate({ libraryID }))[0]; + assert.equal(newItem.itemType, 'attachment'); + assert.equal(newItem.getField('title'), 'Test PDF'); + assert.ok(await newItem.getFilePathAsync()); + }); }); }); \ No newline at end of file