Storage adapter should be document-agnostic.

Got a blob return a key; get a key return a blob.
This commit is contained in:
Timothy Farrell 2018-06-24 19:13:01 -05:00
parent 50582089fb
commit ecdd4cf329
2 changed files with 23 additions and 27 deletions

View File

@ -91,27 +91,24 @@ export const B2Adapter = function(b2apikey, b2secret, b2bucket) {
}); });
}, },
save: async function saveAttachment(blob) { save: async function saveAttachment(blob) {
try {
const uploadAuth = await uploadAuthorization(); const uploadAuth = await uploadAuthorization();
const digest = await sha1(await blobToArrayBuffer(blob)); const digest = await sha1(await blobToArrayBuffer(blob));
const res = await fetch(uploadAuth.uploadUrl, { const res = await fetch(uploadAuth.uploadUrl, {
method: 'POST', method: 'POST',
headers: await headers({ headers: await headers({
Authorization: uploadAuth.authorizationToken, Authorization: uploadAuth.authorizationToken,
'X-Bz-File-Name': encodeURIComponent(blob.name), 'X-Bz-File-Name': encodeURIComponent(blob.name || digest),
'Content-Type': blob.type, 'Content-Type': blob.type,
'Content-Length': blob.size, 'Content-Length': blob.size,
'X-Bz-Content-Sha1': digest 'X-Bz-Content-Sha1': digest
}), }),
body: blob body: blob
}); });
const data = await res.json();
return { return {
ok: true, fileId: data.fileId,
id: await res.json() fileName: data.fileName
}; };
} catch (e) {
return { ok: false, error: e };
}
} }
}); });
}; };

View File

@ -70,8 +70,9 @@ export function PouchDBAttachmentProxy({ save, getFn, remove }) {
return Promise.all( return Promise.all(
attachments.map(([doc, attName, blob]) => attachments.map(([doc, attName, blob]) =>
save.call(this, blob).then(resData => { save
if (resData && resData.ok) { .call(this, blob)
.then(resData => {
deepAssign(doc, { deepAssign(doc, {
_attachments: { _attachments: {
[attName]: { [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(() => { ).then(() => {
return pouchBulkDocs.call(this, ...args); return pouchBulkDocs.call(this, ...args);