From a72ae1481631dcefe603ef9ff3a1a9f7f5c747d8 Mon Sep 17 00:00:00 2001 From: Fletcher Hazlehurst Date: Wed, 28 Oct 2020 09:12:54 -0600 Subject: [PATCH] Fix bug with double saving of snapshots. --- .../zotero/xpcom/connector/server_connector.js | 17 +++++++++++++++-- .../zotero/xpcom/translation/translate_item.js | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/chrome/content/zotero/xpcom/connector/server_connector.js b/chrome/content/zotero/xpcom/connector/server_connector.js index 179423d6e5..a4b1cddf54 100644 --- a/chrome/content/zotero/xpcom/connector/server_connector.js +++ b/chrome/content/zotero/xpcom/connector/server_connector.js @@ -855,17 +855,30 @@ Zotero.Server.Connector.SaveItems.prototype = { cookieSandbox, proxy }); + // This is a bit tricky. When saving items, the call back`onTopLevelItemsDone` will + // return the HTTP request to the connector. Then it may spend some time fetching + // PDFs. In the meantime, the connector will create a snapshot and send it along to + // the `saveSingleFile` endpoint, which quickly adds the data to the session and + // then saves the pending attachments, without removing them (we need them in case + // the session switches libraries and we need to save again). So the pending + // attachments exist and have already been saved by the time this `saveItems` + // promise resolves and we continue executing. So we save the number of existing + // attachments before that so prevent double saving. + let hasPendingAttachments; let items = await itemSaver.saveItems( data.items, function (attachment, progress, error) { session.onProgress(attachment, progress, error); }, - onTopLevelItemsDone, + (...args) => { + hasPendingAttachments = session.pendingAttachments.length > 0; + if (onTopLevelItemsDone) onTopLevelItemsDone(...args); + }, function (parentItemID, attachment) { session.pendingAttachments.push([parentItemID, attachment]); } ); - if (session.pendingAttachments.length > 0) { + if (hasPendingAttachments) { // If the session has snapshotContent already (from switching to a `filesEditable` library // then we can save `pendingAttachments` now if (data.snapshotContent) { diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js index b68fdd87ba..7e652b9cbb 100644 --- a/chrome/content/zotero/xpcom/translation/translate_item.js +++ b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -364,6 +364,7 @@ Zotero.Translate.ItemSaver.prototype = { */ saveSnapshotAttachments: Zotero.Promise.coroutine(function* (pendingAttachments, snapshotContent, attachmentCallback) { for (let [parentItemID, attachment] of pendingAttachments) { + Zotero.debug('Saving pending attachment: ' + JSON.stringify(attachment)); if (snapshotContent) { attachment.snapshotContent = snapshotContent; }