diff --git a/packages/gallery/src/utils/attachmentProxy.js b/packages/gallery/src/utils/attachmentProxy.js index 9136715..5109293 100644 --- a/packages/gallery/src/utils/attachmentProxy.js +++ b/packages/gallery/src/utils/attachmentProxy.js @@ -23,20 +23,13 @@ export function PouchDBAttachmentProxy({ save, getFn, remove }) { if (att.type !== STORAGE_MIMETYPE) { return att; } - return await getFn.call(this, await readStorageMap(att)); + return await getFn(await readStorageMap(att)); }; } if (remove) { override.removeAttachment = async function removeAttachment(...args) { - const att = await pouchGetAttachment.apply(this, args); - if (att.type === STORAGE_MIMETYPE) { - try { - await remove.call(this, await readStorageMap(att)); - } catch (e) { - error(`Failed to remove attachment ${args}`, e); - } - } + cleanupFiles(await pouchGetAttachment.apply(this, args)); return await pouchRemoveAttachment.apply(this, args); }; } @@ -51,12 +44,11 @@ export function PouchDBAttachmentProxy({ save, getFn, remove }) { } // All documents must have a .name field. - const deletedFiles = new Set(); + const deletedFiles = []; const attachments = []; docs.filter(d => d.$$type === 'file').forEach(f => { if (f._deleted) { - const deleteArgs = [f._id, 'data', f._rev]; - deletedFiles.add(deleteArgs.concat(pouchGetAttachment.call(this, f._id, 'data'))); + deletedFiles.push(pouchGetAttachment.call(this, f._id, 'data')); return; } if (f._attachments && f._attachments.data.data instanceof Blob) { @@ -66,18 +58,17 @@ export function PouchDBAttachmentProxy({ save, getFn, remove }) { } }); - deletedFiles.forEach(cleanupFiles); + Promise.all(deletedFiles).then(atts => atts.forEach(cleanupFiles)); return Promise.all( attachments.map(([doc, attName, blob]) => - save - .call(this, blob) + save(blob) .then(resData => { deepAssign(doc, { _attachments: { [attName]: { content_type: STORAGE_MIMETYPE, - data: btoa(JSON.stringify(resData.id)) + data: btoa(JSON.stringify(resData)) } } }); @@ -90,10 +81,13 @@ export function PouchDBAttachmentProxy({ save, getFn, remove }) { }; } - const cleanupFiles = backgroundTask( - ([id, attName, rev, attPromise]) => attPromise.then(att => remove(id, attName, rev, att)), - false - ); + const cleanupFiles = backgroundTask(function(att) { + if (att.type === STORAGE_MIMETYPE) { + readStorageMap(att) + .then(remove) + .catch(e => error(`Failed to remove attachment ${args}`, e)); + } + }, false); return override; }