Storage adapter should be document-agnostic.
Got a blob return a key; get a key return a blob.
This commit is contained in:
parent
ec1a07dffa
commit
d05b929089
@ -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),
|
||||
'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 {
|
||||
ok: true,
|
||||
id: await res.json()
|
||||
fileId: data.fileId,
|
||||
fileName: data.fileName
|
||||
};
|
||||
} catch (e) {
|
||||
return { ok: false, error: e };
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user