diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index af7313a95c..9c8d8b00f9 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -297,9 +297,6 @@ Zotero.Attachments = new function(){ 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') { @@ -311,7 +308,9 @@ Zotero.Attachments = new function(){ yield Zotero.DB.executeTransaction(async function () { // Create a new attachment attachmentItem = new Zotero.Item('attachment'); - attachmentItem.libraryID = libraryID; + if (libraryID) { + attachmentItem.libraryID = libraryID; + } attachmentItem.setField('title', title); attachmentItem.setField('url', url); attachmentItem.parentID = parentItemID; diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js index 9302aa4ac1..61ff7913ec 100644 --- a/chrome/content/zotero/xpcom/translation/translate_item.js +++ b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -679,6 +679,7 @@ Zotero.Translate.ItemSaver.prototype = { newItem = yield Zotero.Attachments.importFromFile({ file: file, parentItemID, + libraryID: this._libraryID, collections: !parentItemID ? this._collections : undefined, saveOptions: this._saveOptions, }); diff --git a/test/tests/importExportTest.js b/test/tests/importExportTest.js index e1eead5695..46a26f420c 100644 --- a/test/tests/importExportTest.js +++ b/test/tests/importExportTest.js @@ -95,7 +95,7 @@ describe("Import/Export", function () { assert.sameMembers(newNote2.relatedItems, [newNote1]); }); - it("should import standalone PDF attachment", async function () { + describe("standalone attachments", function () { var rdf = ` - + attachment - + https://example.com 2022-07-22 06:36:31 - Test PDF + Test 1 1 application/pdf + + attachment + + Test 2 + 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') - ); + async function doImport(libraryID) { + var tempDir = await getTempDirectory(); + var file = OS.Path.join(tempDir, 'export.rdf'); + await Zotero.File.putContentsAsync(file, rdf); + var folder1 = OS.Path.join(tempDir, 'files', '1234'); + var folder2 = OS.Path.join(tempDir, 'files', '2345'); + await OS.File.makeDir(folder1, { from: tempDir }); + await OS.File.makeDir(folder2, { from: tempDir }); + await OS.File.copy( + OS.Path.join(OS.Path.join(getTestDataDirectory().path, 'test.pdf')), + OS.Path.join(folder1, 'test1.pdf') + ); + await OS.File.copy( + OS.Path.join(OS.Path.join(getTestDataDirectory().path, 'test.pdf')), + OS.Path.join(folder2, 'test2.pdf') + ); + + var translation = new Zotero.Translate.Import(); + translation.setLocation(Zotero.File.pathToFile(file)); + let translators = await translation.getTranslators(); + translation.setTranslator(translators[0]); + var newItems = await translation.translate({ libraryID }); + + var newItem1 = newItems.filter(x => x.getField('title') == 'Test 1')[0]; + assert.equal(newItem1.itemType, 'attachment'); + assert.ok(await newItem1.getFilePathAsync()); + + var newItem2 = newItems.filter(x => x.getField('title') == 'Test 2')[0]; + assert.equal(newItem2.itemType, 'attachment'); + assert.ok(await newItem1.getFilePathAsync()); + + return [newItem1, newItem2]; + } - 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()); + it("should import into My Library", async function () { + var libraryID = Zotero.Libraries.userLibraryID; + var [newItem1, newItem2] = await doImport(libraryID); + assert.equal(newItem1.libraryID, libraryID); + assert.equal(newItem2.libraryID, libraryID); + }); + + it("should import into group library", async function () { + var libraryID = (await getGroup()).libraryID; + var [newItem1, newItem2] = await doImport(libraryID); + assert.equal(newItem1.libraryID, libraryID); + assert.equal(newItem2.libraryID, libraryID); + }); }); }); }); \ No newline at end of file