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) {
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
};
}
});
};

View File

@ -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);