Split attachmentProxy into its own package
This commit is contained in:
parent
f312b7c9ee
commit
c503a9539d
@ -2,7 +2,9 @@
|
|||||||
"name": "Gallery",
|
"name": "Gallery",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"description": "Personal photo gallery",
|
"description": "Personal photo gallery",
|
||||||
"keywords": ["javascript"],
|
"keywords": [
|
||||||
|
"javascript"
|
||||||
|
],
|
||||||
"author": "Timothy Farrell <tim@thecookiejar.me> (https://github.com/explorigin)",
|
"author": "Timothy Farrell <tim@thecookiejar.me> (https://github.com/explorigin)",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import core from 'pouchdb-core';
|
import core from 'pouchdb-core';
|
||||||
import { PouchDBAttachmentProxy } from '../utils/attachmentProxy.js';
|
import { PouchDBAttachmentProxy, blobToString, stringToBlob } from 'pouchdb-attachmentproxy';
|
||||||
import { deepAssign, blobToArrayBuffer } from '../utils/conversion.js';
|
import { deepAssign, blobToArrayBuffer } from '../utils/conversion.js';
|
||||||
import { prop, computed, stream } from 'frptools';
|
import { prop, computed, stream } from 'frptools';
|
||||||
import { sha1 } from '../utils/crypto.js';
|
import { sha1 } from '../utils/crypto.js';
|
||||||
@ -72,19 +72,19 @@ export const B2Adapter = function(b2apikey, b2secret, b2bucket) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return PouchDBAttachmentProxy({
|
return PouchDBAttachmentProxy({
|
||||||
getFn: async function getAttachment(id) {
|
getFn: async function getAttachment(blob) {
|
||||||
const res = await fetch(await downloadUrl(id), {
|
const res = await fetch(await downloadUrl(blobToString(blob)), {
|
||||||
headers: await headers()
|
headers: await headers()
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.blob();
|
return res.blob();
|
||||||
},
|
},
|
||||||
remove: async function removeAttachment(id) {
|
remove: async function removeAttachment(blob) {
|
||||||
const s = await session();
|
const s = await session();
|
||||||
const res = await fetch('/api/v1/get_file_info', {
|
const res = await fetch('/api/v1/get_file_info', {
|
||||||
headers: await headers(),
|
headers: await headers(),
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ fileId: id })
|
body: JSON.stringify({ fileId: blobToString(blob) })
|
||||||
});
|
});
|
||||||
const { fileName, fileId } = await res.json();
|
const { fileName, fileId } = await res.json();
|
||||||
return await fetch('/api/v1/remove_file', {
|
return await fetch('/api/v1/remove_file', {
|
||||||
@ -108,7 +108,7 @@ export const B2Adapter = function(b2apikey, b2secret, b2bucket) {
|
|||||||
body: blob
|
body: blob
|
||||||
});
|
});
|
||||||
const { fileId } = await res.json();
|
const { fileId } = await res.json();
|
||||||
return fileId;
|
return stringToBlob(fileId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,119 +0,0 @@
|
|||||||
import core from 'pouchdb-core';
|
|
||||||
import { backgroundTask } from 'backgroundtask';
|
|
||||||
|
|
||||||
import { deepAssign, blobToObj } from '../utils/conversion.js';
|
|
||||||
import { error, log } from '../utils/console.js';
|
|
||||||
|
|
||||||
const pouchBulkDocs = core.prototype.bulkDocs;
|
|
||||||
const pouchGetAttachment = core.prototype.getAttachment;
|
|
||||||
const pouchRemoveAttachment = core.prototype.removeAttachment;
|
|
||||||
const STORAGE_MIMETYPE = 'application/b2storagemap';
|
|
||||||
|
|
||||||
export function PouchDBAttachmentProxy({ save, getFn, remove }) {
|
|
||||||
const override = {
|
|
||||||
$attachmentsProxied: true
|
|
||||||
};
|
|
||||||
|
|
||||||
if (getFn) {
|
|
||||||
override.getAttachment = async function getAttachment(...args) {
|
|
||||||
const att = await pouchGetAttachment.apply(this, args);
|
|
||||||
if (att.type !== STORAGE_MIMETYPE) {
|
|
||||||
return att;
|
|
||||||
}
|
|
||||||
return await getFn(await blobToObj(att));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (remove) {
|
|
||||||
override.removeAttachment = async function removeAttachment(...args) {
|
|
||||||
cleanupFiles(await pouchGetAttachment.apply(this, args));
|
|
||||||
return await pouchRemoveAttachment.apply(this, args);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (save || remove) {
|
|
||||||
override.bulkDocs = function bulkDocs(...args) {
|
|
||||||
let docs;
|
|
||||||
if (Array.isArray(args[0])) {
|
|
||||||
docs = args[0];
|
|
||||||
} else {
|
|
||||||
docs = args[0].docs;
|
|
||||||
}
|
|
||||||
|
|
||||||
// All documents must have a .name field.
|
|
||||||
const deletedFiles = [];
|
|
||||||
const attachments = [];
|
|
||||||
docs.filter(d => d.type === 'file').forEach(f => {
|
|
||||||
if (f._deleted) {
|
|
||||||
deletedFiles.push(pouchGetAttachment.call(this, f._id, 'data'));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (f._attachments && f._attachments.data.data instanceof Blob) {
|
|
||||||
log(`Saving File ${f._id} attachment`);
|
|
||||||
attachments.push([f, 'data', f._attachments.data.data]);
|
|
||||||
delete f._attachments.data;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Promise.all(deletedFiles).then(atts => atts.forEach(cleanupFiles));
|
|
||||||
|
|
||||||
return Promise.all(
|
|
||||||
attachments.map(([doc, attName, blob]) =>
|
|
||||||
save(blob)
|
|
||||||
.then(resData => {
|
|
||||||
deepAssign(doc, {
|
|
||||||
_attachments: {
|
|
||||||
[attName]: {
|
|
||||||
content_type: STORAGE_MIMETYPE,
|
|
||||||
data: btoa(JSON.stringify(resData))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(e => error(`Failed to save attachment ${doc._id}[${attName}]`, resData))
|
|
||||||
)
|
|
||||||
).then(() => {
|
|
||||||
return pouchBulkDocs.call(this, ...args);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const cleanupFiles = backgroundTask(function(att) {
|
|
||||||
if (att.type === STORAGE_MIMETYPE) {
|
|
||||||
blobToObj(att)
|
|
||||||
.then(remove)
|
|
||||||
.catch(e => error(`Failed to remove attachment ${args}`, e));
|
|
||||||
}
|
|
||||||
}, false);
|
|
||||||
|
|
||||||
return override;
|
|
||||||
}
|
|
||||||
|
|
||||||
// export const LocalStorageExampleAdapter = function() {
|
|
||||||
// return PouchDBAttachmentProxy({
|
|
||||||
// get: async function getAttachment(docId, attName) {
|
|
||||||
// const data = localStorage[`${docId}-${attName}`].split(';base64,');
|
|
||||||
// var byteCharacters = atob(data[1]);
|
|
||||||
// var byteNumbers = new Array(byteCharacters.length);
|
|
||||||
// for (var i = 0; i < byteCharacters.length; i++) {
|
|
||||||
// byteNumbers[i] = byteCharacters.charCodeAt(i);
|
|
||||||
// }
|
|
||||||
// var byteArray = new Uint8Array(byteNumbers);
|
|
||||||
// return Promise.resolve(new Blob([byteArray], {type: data[0].substr(5)}));
|
|
||||||
// },
|
|
||||||
// remove: async function removeAttachment(docId, attName, rev) {
|
|
||||||
// delete localStorage[`${docId}-${attName}`];
|
|
||||||
// return Promise.resolve({"ok": true});
|
|
||||||
// },
|
|
||||||
// save: async function saveAttachment(docId, attName, obj) {
|
|
||||||
// return new Promise((resolve) => {
|
|
||||||
// var reader = new FileReader();
|
|
||||||
// reader.onloadend = function() {
|
|
||||||
// localStorage[`${docId}-${attName}`] = reader.result;
|
|
||||||
// resolve({"ok": true});
|
|
||||||
// }
|
|
||||||
// reader.readAsDataURL(obj.data);
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// };
|
|
||||||
@ -26,18 +26,6 @@ export function blobToArrayBuffer(blob) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function blobToString(blob) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const f = new FileReader();
|
|
||||||
f.onload = _ => resolve(f.result);
|
|
||||||
f.readAsText(blob);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function blobToObj(blob) {
|
|
||||||
return blobToString(blob).then(JSON.parse);
|
|
||||||
}
|
|
||||||
|
|
||||||
export const arrayHashWrapper = hash => arr => (Array.isArray(arr) ? arr.map(hash).join('?') : arr);
|
export const arrayHashWrapper = hash => arr => (Array.isArray(arr) ? arr.map(hash).join('?') : arr);
|
||||||
|
|
||||||
export function pouchDocHash(d) {
|
export function pouchDocHash(d) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user