Fix error merging items with missing attachment files

Fixes #2453
This commit is contained in:
Dan Stillman 2022-03-24 06:39:01 -04:00
parent 9d91e0d4f7
commit 1d1adbca70
2 changed files with 33 additions and 2 deletions

View file

@ -1195,7 +1195,19 @@ Zotero.Items = function() {
* @return {Promise<String>}
*/
this._hashAttachmentText = async function (attachment) {
if ((await OS.File.stat(await attachment.getFilePathAsync())).size > 5e8) {
var fileInfo;
try {
fileInfo = await OS.File.stat(attachment.getFilePath());
}
catch (e) {
if (e instanceof OS.File.Error && e.becauseNoSuchFile) {
Zotero.debug('_hashAttachmentText: Attachment not found');
return null;
}
Zotero.logError(e);
return null;
}
if (fileInfo.size > 5e8) {
Zotero.debug('_hashAttachmentText: Attachment too large');
return null;
}

View file

@ -516,7 +516,26 @@ describe("Zotero.Items", function () {
assert.isTrue(item2.deleted);
assert.isFalse(attachment2.deleted);
});
it("should ignore attachment with missing file", async function () {
let item1 = await createDataObject('item');
let attachment1 = await importPDFAttachment(item1);
let item2 = item1.clone();
await item2.saveTx();
let attachment2 = await importPDFAttachment(item2);
// Delete the attachment file
await OS.File.remove(await attachment2.getFilePathAsync());
await Zotero.Items.merge(item1, [item2]);
assert.isFalse(item1.deleted);
assert.isFalse(attachment1.deleted);
assert.equal(item1.numAttachments(true), 2);
assert.isTrue(item2.deleted);
assert.isFalse(attachment2.deleted);
});
it("should allow small differences when hashing content", async function () {
let item1 = await createDataObject('item', { setTitle: true });
let attachment1 = await importFileAttachment('duplicatesMerge_JSTOR_1.pdf', { parentItemID: item1.id });