From d05b92908997cbd55a333f91abf9b665b8b6e5f5 Mon Sep 17 00:00:00 2001 From: Timothy Farrell Date: Sun, 24 Jun 2018 19:13:01 -0500 Subject: [PATCH] Storage adapter should be document-agnostic. Got a blob return a key; get a key return a blob. --- packages/gallery/src/services/b2.js | 39 +++++++++---------- packages/gallery/src/utils/attachmentProxy.js | 11 +++--- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/packages/gallery/src/services/b2.js b/packages/gallery/src/services/b2.js index 32249d6..a051f89 100644 --- a/packages/gallery/src/services/b2.js +++ b/packages/gallery/src/services/b2.js @@ -91,27 +91,24 @@ export const B2Adapter = function(b2apikey, b2secret, b2bucket) { }); }, save: async function saveAttachment(blob) { - try { - const uploadAuth = await uploadAuthorization(); - const digest = await sha1(await blobToArrayBuffer(blob)); - const res = await fetch(uploadAuth.uploadUrl, { - method: 'POST', - headers: await headers({ - Authorization: uploadAuth.authorizationToken, - 'X-Bz-File-Name': encodeURIComponent(blob.name), - 'Content-Type': blob.type, - 'Content-Length': blob.size, - 'X-Bz-Content-Sha1': digest - }), - body: blob - }); - return { - ok: true, - id: await res.json() - }; - } catch (e) { - return { ok: false, error: e }; - } + const uploadAuth = await uploadAuthorization(); + const digest = await sha1(await blobToArrayBuffer(blob)); + const res = await fetch(uploadAuth.uploadUrl, { + method: 'POST', + headers: await headers({ + Authorization: uploadAuth.authorizationToken, + 'X-Bz-File-Name': encodeURIComponent(blob.name || digest), + 'Content-Type': blob.type, + 'Content-Length': blob.size, + 'X-Bz-Content-Sha1': digest + }), + body: blob + }); + const data = await res.json(); + return { + fileId: data.fileId, + fileName: data.fileName + }; } }); }; diff --git a/packages/gallery/src/utils/attachmentProxy.js b/packages/gallery/src/utils/attachmentProxy.js index 0179a37..9136715 100644 --- a/packages/gallery/src/utils/attachmentProxy.js +++ b/packages/gallery/src/utils/attachmentProxy.js @@ -70,8 +70,9 @@ export function PouchDBAttachmentProxy({ save, getFn, remove }) { return Promise.all( attachments.map(([doc, attName, blob]) => - save.call(this, blob).then(resData => { - if (resData && resData.ok) { + save + .call(this, blob) + .then(resData => { deepAssign(doc, { _attachments: { [attName]: { @@ -80,10 +81,8 @@ export function PouchDBAttachmentProxy({ save, getFn, remove }) { } } }); - } else { - error(`Failed to save attachment ${doc._id}[${attName}]`, resData); - } - }) + }) + .catch(e => error(`Failed to save attachment ${doc._id}[${attName}]`, resData)) ) ).then(() => { return pouchBulkDocs.call(this, ...args);