Fix importing of standalone attachments

Maybe has been broken for years?

https://forums.zotero.org/discussion/99020/warning-data-loss-when-exporting-collections
This commit is contained in:
Dan Stillman 2022-08-13 16:17:57 -04:00
parent 3dc3359cff
commit 318e4852e9
3 changed files with 61 additions and 3 deletions

View file

@ -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<Zotero.Item>}
*/
@ -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(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);

View file

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

View file

@ -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 = `<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:z="http://www.zotero.org/namespaces/export#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:link="http://purl.org/rss/1.0/modules/link/"
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:itemType>attachment</z:itemType>
<rdf:resource rdf:resource="files/1234/test.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>
<z:linkMode>1</z:linkMode>
<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')
);
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());
});
});
});