From c503a9539d7fc3c6baae8388893a84d1a422ec45 Mon Sep 17 00:00:00 2001 From: Timothy Farrell Date: Thu, 26 Jul 2018 16:28:00 -0500 Subject: [PATCH] Split attachmentProxy into its own package --- packages/gallery/package.json | 4 +- packages/gallery/src/services/b2.js | 12 +- packages/gallery/src/utils/attachmentProxy.js | 119 ------------------ packages/gallery/src/utils/conversion.js | 12 -- 4 files changed, 9 insertions(+), 138 deletions(-) delete mode 100644 packages/gallery/src/utils/attachmentProxy.js diff --git a/packages/gallery/package.json b/packages/gallery/package.json index 62f97ff..759956e 100644 --- a/packages/gallery/package.json +++ b/packages/gallery/package.json @@ -2,7 +2,9 @@ "name": "Gallery", "version": "0.0.1", "description": "Personal photo gallery", - "keywords": ["javascript"], + "keywords": [ + "javascript" + ], "author": "Timothy Farrell (https://github.com/explorigin)", "license": "Apache-2.0", "scripts": { diff --git a/packages/gallery/src/services/b2.js b/packages/gallery/src/services/b2.js index fdf710f..5c60f9d 100644 --- a/packages/gallery/src/services/b2.js +++ b/packages/gallery/src/services/b2.js @@ -1,5 +1,5 @@ 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 { prop, computed, stream } from 'frptools'; import { sha1 } from '../utils/crypto.js'; @@ -72,19 +72,19 @@ export const B2Adapter = function(b2apikey, b2secret, b2bucket) { } return PouchDBAttachmentProxy({ - getFn: async function getAttachment(id) { - const res = await fetch(await downloadUrl(id), { + getFn: async function getAttachment(blob) { + const res = await fetch(await downloadUrl(blobToString(blob)), { headers: await headers() }); return res.blob(); }, - remove: async function removeAttachment(id) { + remove: async function removeAttachment(blob) { const s = await session(); const res = await fetch('/api/v1/get_file_info', { headers: await headers(), method: 'POST', - body: JSON.stringify({ fileId: id }) + body: JSON.stringify({ fileId: blobToString(blob) }) }); const { fileName, fileId } = await res.json(); return await fetch('/api/v1/remove_file', { @@ -108,7 +108,7 @@ export const B2Adapter = function(b2apikey, b2secret, b2bucket) { body: blob }); const { fileId } = await res.json(); - return fileId; + return stringToBlob(fileId); } }); }; diff --git a/packages/gallery/src/utils/attachmentProxy.js b/packages/gallery/src/utils/attachmentProxy.js deleted file mode 100644 index 71d29ba..0000000 --- a/packages/gallery/src/utils/attachmentProxy.js +++ /dev/null @@ -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); -// }); -// } -// }); -// }; diff --git a/packages/gallery/src/utils/conversion.js b/packages/gallery/src/utils/conversion.js index e1b39b7..874d27b 100644 --- a/packages/gallery/src/utils/conversion.js +++ b/packages/gallery/src/utils/conversion.js @@ -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 function pouchDocHash(d) {