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:
parent
318e4852e9
commit
85b0c9dbad
3 changed files with 60 additions and 26 deletions
|
@ -297,9 +297,6 @@ Zotero.Attachments = new function(){
|
||||||
else if (contentType == 'text/html') {
|
else if (contentType == 'text/html') {
|
||||||
throw new Error("parentItemID not provided");
|
throw new Error("parentItemID not provided");
|
||||||
}
|
}
|
||||||
else if (!libraryID) {
|
|
||||||
throw new Error("parentItemID or libraryID must be provided");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Webpage snapshots must have parent items
|
// Webpage snapshots must have parent items
|
||||||
if (!parentItemID && contentType == 'text/html') {
|
if (!parentItemID && contentType == 'text/html') {
|
||||||
|
@ -311,7 +308,9 @@ Zotero.Attachments = new function(){
|
||||||
yield Zotero.DB.executeTransaction(function* () {
|
yield Zotero.DB.executeTransaction(function* () {
|
||||||
// Create a new attachment
|
// Create a new attachment
|
||||||
attachmentItem = new Zotero.Item('attachment');
|
attachmentItem = new Zotero.Item('attachment');
|
||||||
attachmentItem.libraryID = libraryID;
|
if (libraryID) {
|
||||||
|
attachmentItem.libraryID = libraryID;
|
||||||
|
}
|
||||||
attachmentItem.setField('title', title);
|
attachmentItem.setField('title', title);
|
||||||
attachmentItem.setField('url', url);
|
attachmentItem.setField('url', url);
|
||||||
attachmentItem.parentID = parentItemID;
|
attachmentItem.parentID = parentItemID;
|
||||||
|
|
|
@ -679,6 +679,7 @@ Zotero.Translate.ItemSaver.prototype = {
|
||||||
newItem = yield Zotero.Attachments.importFromFile({
|
newItem = yield Zotero.Attachments.importFromFile({
|
||||||
file: file,
|
file: file,
|
||||||
parentItemID,
|
parentItemID,
|
||||||
|
libraryID: this._libraryID,
|
||||||
collections: !parentItemID ? this._collections : undefined,
|
collections: !parentItemID ? this._collections : undefined,
|
||||||
saveOptions: this._saveOptions,
|
saveOptions: this._saveOptions,
|
||||||
});
|
});
|
||||||
|
|
|
@ -95,7 +95,7 @@ describe("Import/Export", function () {
|
||||||
assert.sameMembers(newNote2.relatedItems, [newNote1]);
|
assert.sameMembers(newNote2.relatedItems, [newNote1]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should import standalone PDF attachment", async function () {
|
describe("standalone attachments", function () {
|
||||||
var rdf = `<rdf:RDF
|
var rdf = `<rdf:RDF
|
||||||
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
||||||
xmlns:z="http://www.zotero.org/namespaces/export#"
|
xmlns:z="http://www.zotero.org/namespaces/export#"
|
||||||
|
@ -105,40 +105,74 @@ describe("Import/Export", function () {
|
||||||
xmlns:bib="http://purl.org/net/biblio#"
|
xmlns:bib="http://purl.org/net/biblio#"
|
||||||
xmlns:foaf="http://xmlns.com/foaf/0.1/"
|
xmlns:foaf="http://xmlns.com/foaf/0.1/"
|
||||||
xmlns:prism="http://prismstandard.org/namespaces/1.2/basic/">
|
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>
|
<z:itemType>attachment</z:itemType>
|
||||||
<rdf:resource rdf:resource="files/1234/test.pdf"/>
|
<rdf:resource rdf:resource="files/1234/test1.pdf"/>
|
||||||
<dc:identifier>
|
<dc:identifier>
|
||||||
<dcterms:URI>
|
<dcterms:URI>
|
||||||
<rdf:value>https://example.com</rdf:value>
|
<rdf:value>https://example.com</rdf:value>
|
||||||
</dcterms:URI>
|
</dcterms:URI>
|
||||||
</dc:identifier>
|
</dc:identifier>
|
||||||
<dcterms:dateSubmitted>2022-07-22 06:36:31</dcterms:dateSubmitted>
|
<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>
|
<z:linkMode>1</z:linkMode>
|
||||||
<link:type>application/pdf</link:type>
|
<link:type>application/pdf</link:type>
|
||||||
</z:Attachment>
|
</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>
|
</rdf:RDF>
|
||||||
`;
|
`;
|
||||||
var libraryID = Zotero.Libraries.userLibraryID;
|
async function doImport(libraryID) {
|
||||||
var tempDir = await getTempDirectory();
|
var tempDir = await getTempDirectory();
|
||||||
var file = OS.Path.join(tempDir, 'export.rdf');
|
var file = OS.Path.join(tempDir, 'export.rdf');
|
||||||
await Zotero.File.putContentsAsync(file, rdf);
|
await Zotero.File.putContentsAsync(file, rdf);
|
||||||
var folder = OS.Path.join(tempDir, 'files', '1234');
|
var folder1 = OS.Path.join(tempDir, 'files', '1234');
|
||||||
await OS.File.makeDir(folder, { from: tempDir });
|
var folder2 = OS.Path.join(tempDir, 'files', '2345');
|
||||||
await OS.File.copy(
|
await OS.File.makeDir(folder1, { from: tempDir });
|
||||||
OS.Path.join(OS.Path.join(getTestDataDirectory().path, 'test.pdf')),
|
await OS.File.makeDir(folder2, { from: tempDir });
|
||||||
OS.Path.join(folder, 'test.pdf')
|
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();
|
var translation = new Zotero.Translate.Import();
|
||||||
translation.setLocation(Zotero.File.pathToFile(file));
|
translation.setLocation(Zotero.File.pathToFile(file));
|
||||||
let translators = await translation.getTranslators();
|
let translators = await translation.getTranslators();
|
||||||
translation.setTranslator(translators[0]);
|
translation.setTranslator(translators[0]);
|
||||||
var newItem = (await translation.translate({ libraryID }))[0];
|
var newItems = await translation.translate({ libraryID });
|
||||||
assert.equal(newItem.itemType, 'attachment');
|
|
||||||
assert.equal(newItem.getField('title'), 'Test PDF');
|
var newItem1 = newItems.filter(x => x.getField('title') == 'Test 1')[0];
|
||||||
assert.ok(await newItem.getFilePathAsync());
|
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];
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
Loading…
Reference in a new issue