Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

iPhone: photos and videos are kept in the browser as bytes, not as a Blob WebKit may no longer read back, so a large video plays and uploads

Duarte Santos0f27765f76b1
diff --git a/frontend/src/lib/media-store-idb.js b/frontend/src/lib/media-store-idb.js
index 0bbdf060d..49e77ce6d 100644
--- a/frontend/src/lib/media-store-idb.js
+++ b/frontend/src/lib/media-store-idb.js
@@ -19,7 +19,7 @@ const done = tx => new Promise((resolve, reject) => {
tx.onabort = () => reject(tx.error || new Error('aborted'))
})
const meta = r => {
- const { blob, ...rest } = r
+ const { blob, data, ...rest } = r
return rest
}
@@ -43,7 +43,7 @@ export function idbBackend({ idb = globalThis.indexedDB } = {}) {
db.onversionchange = () => { try { db.close() } catch { /* already closed */ } }
// A write test: some browsers open the database and then refuse every Blob put into it.
const t = tx('readwrite')
- t.objectStore(STORE).put({ hash: '.probe', blob: new Blob(['x']), mime: '', size: 1 })
+ t.objectStore(STORE).put({ hash: '.probe', data: new Uint8Array([120]).buffer, mime: '', size: 1 })
t.objectStore(STORE).delete('.probe')
await done(t)
return true
@@ -69,11 +69,28 @@ export function idbBackend({ idb = globalThis.indexedDB } = {}) {
},
async getBlob(hash) {
const r = await req(tx('readonly').objectStore(STORE).get(hash))
- return r?.blob || null
+ if (!r) return null
+ if (r.data) return new Blob([r.data], { type: r.mime || '' })
+ if (!r.blob) return null
+ // A record from before `data`: read it through once. On WebKit a large one may no longer be
+ // readable — then it is missing (the caller fetches it again or shows the fallback tile)
+ // rather than a video that never plays. A readable one is rewritten in the new shape.
+ let buf
+ try { buf = await r.blob.arrayBuffer() } catch { return null }
+ if (typeof r.size === 'number' && buf.byteLength !== r.size) return null
+ try {
+ const t = tx('readwrite')
+ const { blob: _old, ...rest } = r
+ t.objectStore(STORE).put({ ...rest, data: buf })
+ await done(t)
+ } catch { /* still readable this time; the next read tries again */ }
+ return new Blob([buf], { type: r.mime || '' })
},
async put(rec, blob) {
+ // Read before the transaction opens: an await inside it would let it commit early.
+ const data = blob ? await blob.arrayBuffer() : undefined
const t = tx('readwrite')
- t.objectStore(STORE).put({ ...rec, blob })
+ t.objectStore(STORE).put(data ? { ...rec, data } : { ...rec })
await done(t)
},
async patch(hash, fields) {
diff --git a/frontend/src/lib/media-store-idb.test.js b/frontend/src/lib/media-store-idb.test.js
new file mode 100644
index 000000000..f1e0b3b32
--- /dev/null
+++ b/frontend/src/lib/media-store-idb.test.js
@@ -0,0 +1,97 @@
+import { describe, test, expect } from 'vitest'
+import { idbBackend } from './media-store-idb.js'
+
+/* Just enough IndexedDB for the backend: one database, one object store keyed by `hash`, requests
+ that answer on the next tick and transactions that complete after their last request. Values
+ are kept as they are put, so a test can plant a record in the old `blob` shape. */
+function fakeIdb() {
+ const rows = new Map()
+ const later = fn => setTimeout(fn, 0)
+ const request = run => {
+ const r = { onsuccess: null, onerror: null, result: undefined }
+ later(() => { r.result = run(); r.onsuccess?.() })
+ return r
+ }
+ const db = {
+ objectStoreNames: { contains: () => true },
+ onversionchange: null,
+ close() {},
+ transaction() {
+ const t = { oncomplete: null, onerror: null, onabort: null, pending: 0 }
+ const settle = () => { if (--t.pending === 0) later(() => t.oncomplete?.()) }
+ const track = run => { t.pending++; return request(() => { const v = run(); later(settle); return v }) }
+ t.objectStore = () => ({
+ put: v => track(() => { rows.set(v.hash, v) }),
+ get: k => track(() => rows.get(k)),
+ delete: k => track(() => { rows.delete(k) }),
+ openCursor: () => {
+ const list = [...rows.values()]
+ const r = { onsuccess: null, onerror: null, result: null }
+ let i = 0
+ const step = () => later(() => {
+ r.result = i < list.length ? { value: list[i], continue: () => { i++; step() } } : null
+ r.onsuccess?.()
+ })
+ step()
+ return r
+ }
+ })
+ return t
+ }
+ }
+ return {
+ rows,
+ open() {
+ const r = { onsuccess: null, onerror: null, onupgradeneeded: null, onblocked: null, result: db }
+ later(() => r.onsuccess?.())
+ return r
+ }
+ }
+}
+
+const bytesOf = async blob => Array.from(new Uint8Array(await blob.arrayBuffer()))
+
+describe('the IndexedDB media backend', () => {
+ test('keeps the bytes as an ArrayBuffer, never the Blob, and hands back a readable Blob', async () => {
+ const idb = fakeIdb()
+ const be = idbBackend({ idb })
+ expect(await be.open()).toBe(true)
+ await be.put({ hash: 'h1', mime: 'video/quicktime', size: 3 }, new Blob([new Uint8Array([1, 2, 3])]))
+ const row = idb.rows.get('h1')
+ expect(row.blob).toBeUndefined()
+ expect(row.data).toBeInstanceOf(ArrayBuffer)
+ const back = await be.getBlob('h1')
+ expect(back.type).toBe('video/quicktime')
+ expect(await bytesOf(back)).toEqual([1, 2, 3])
+ expect(await be.all()).toEqual([{ hash: 'h1', mime: 'video/quicktime', size: 3 }])
+ })
+
+ test('an old record whose Blob WebKit can no longer read counts as missing', async () => {
+ const idb = fakeIdb()
+ const be = idbBackend({ idb })
+ await be.open()
+ const dead = { size: 37535282, type: 'video/quicktime', arrayBuffer: () => Promise.reject(new Error('WebKitBlobResource error 1')) }
+ idb.rows.set('h2', { hash: 'h2', mime: 'video/quicktime', size: 37535282, blob: dead })
+ expect(await be.getBlob('h2')).toBeNull()
+ })
+
+ test('an old record that reads short counts as missing', async () => {
+ const idb = fakeIdb()
+ const be = idbBackend({ idb })
+ await be.open()
+ idb.rows.set('h3', { hash: 'h3', mime: 'image/webp', size: 10, blob: new Blob([new Uint8Array([9])]) })
+ expect(await be.getBlob('h3')).toBeNull()
+ })
+
+ test('an old readable record is read once and rewritten as data', async () => {
+ const idb = fakeIdb()
+ const be = idbBackend({ idb })
+ await be.open()
+ idb.rows.set('h4', { hash: 'h4', mime: 'image/webp', size: 2, blob: new Blob([new Uint8Array([7, 8])]) })
+ expect(await bytesOf(await be.getBlob('h4'))).toEqual([7, 8])
+ const row = idb.rows.get('h4')
+ expect(row.blob).toBeUndefined()
+ expect(row.data).toBeInstanceOf(ArrayBuffer)
+ expect(await bytesOf(await be.getBlob('h4'))).toEqual([7, 8])
+ })
+})