Fix importing of standalone attachments without URLs

And allow `importSnapshotFromFile()` without `parentItemID` or
`libraryID`, which is more consistent with other attachment functions

Follow-up to 318e4852e9

https://forums.zotero.org/discussion/comment/414802/#Comment_414802
This commit is contained in:
Dan Stillman 2022-08-14 05:01:00 -04:00
parent b03657add3
commit 6cd1a41a32
3 changed files with 60 additions and 26 deletions

View file

@ -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;

View file

@ -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,
});

View file

@ -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 = `<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:z="http://www.zotero.org/namespaces/export#"
@ -105,40 +105,74 @@ describe("Import/Export", function () {
xmlns:bib="http://purl.org/net/biblio#"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:prism="http://prismstandard.org/namespaces/1.2/basic/">
<z:Attachment rdf:about="#item_66055">
<z:Attachment rdf:about="#item_1234">
<z:itemType>attachment</z:itemType>
<rdf:resource rdf:resource="files/1234/test.pdf"/>
<rdf:resource rdf:resource="files/1234/test1.pdf"/>
<dc:identifier>
<dcterms:URI>
<rdf:value>https://example.com</rdf:value>
</dcterms:URI>
</dc:identifier>
<dcterms:dateSubmitted>2022-07-22 06:36:31</dcterms:dateSubmitted>
<dc:title>Test PDF</dc:title>
<dc:title>Test 1</dc:title>
<z:linkMode>1</z:linkMode>
<link:type>application/pdf</link:type>
</z:Attachment>
<z:Attachment rdf:about="#item_2345">
<z:itemType>attachment</z:itemType>
<rdf:resource rdf:resource="files/2345/test2.pdf"/>
<dc:title>Test 2</dc:title>
<link:type>application/pdf</link:type>
</z:Attachment>
</rdf:RDF>
`;
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);
});
});
});
});