Fix bug with double saving of snapshots.

This commit is contained in:
Fletcher Hazlehurst 2020-10-28 09:12:54 -06:00
parent c0ec91f26d
commit a72ae14816
2 changed files with 16 additions and 2 deletions

View file

@ -855,17 +855,30 @@ Zotero.Server.Connector.SaveItems.prototype = {
cookieSandbox, cookieSandbox,
proxy 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( let items = await itemSaver.saveItems(
data.items, data.items,
function (attachment, progress, error) { function (attachment, progress, error) {
session.onProgress(attachment, progress, error); session.onProgress(attachment, progress, error);
}, },
onTopLevelItemsDone, (...args) => {
hasPendingAttachments = session.pendingAttachments.length > 0;
if (onTopLevelItemsDone) onTopLevelItemsDone(...args);
},
function (parentItemID, attachment) { function (parentItemID, attachment) {
session.pendingAttachments.push([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 // If the session has snapshotContent already (from switching to a `filesEditable` library
// then we can save `pendingAttachments` now // then we can save `pendingAttachments` now
if (data.snapshotContent) { if (data.snapshotContent) {

View file

@ -364,6 +364,7 @@ Zotero.Translate.ItemSaver.prototype = {
*/ */
saveSnapshotAttachments: Zotero.Promise.coroutine(function* (pendingAttachments, snapshotContent, attachmentCallback) { saveSnapshotAttachments: Zotero.Promise.coroutine(function* (pendingAttachments, snapshotContent, attachmentCallback) {
for (let [parentItemID, attachment] of pendingAttachments) { for (let [parentItemID, attachment] of pendingAttachments) {
Zotero.debug('Saving pending attachment: ' + JSON.stringify(attachment));
if (snapshotContent) { if (snapshotContent) {
attachment.snapshotContent = snapshotContent; attachment.snapshotContent = snapshotContent;
} }