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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Media: one local store for the photos and videos of custom exercises, and the sync that keeps it and the server in step — owed like any change
Duarte Santos54e5977d859a
diff --git a/frontend/src/lib/api.js b/frontend/src/lib/api.jsindex c9d7bfa9e..f7a059d0a 100644--- a/frontend/src/lib/api.js+++ b/frontend/src/lib/api.js@@ -81,6 +81,117 @@ async function exchange(url, init) {return body}+/* ------------------------------------------------ photos and videos of custom exercises ------+ Two transports beside api(), for the one kind of body that is not JSON. Both go to the same+ base api() uses and carry the paired phone's Bearer token the same way (the server's CORS+ answer allows it for GET and PUT); both refuse to run on a phone without a server, like api(). */++const mediaUrl = path => (remoteBase ? remoteBase + path : appBase().replace(/\/$/, '') + path)+const mediaHeaders = () => (remoteToken ? { Authorization: 'Bearer ' + remoteToken } : {})+const notPaired = () => failure(t('This phone is not connected to a server.'), 'not-paired', 0)+const timedOut = () => failure(t('The server did not answer in time.'), 'timeout')++/**+ * GET a file as a Blob (lib/media-sync.js fetchToStore, which checks its hash before keeping it).+ * An answer that announces more than `expectSize` (+1 KB) is refused before a byte is read, and+ * one that turns out longer is cut off: a login page or a proxy's error is never read into+ * memory. There is no total time limit — a video on a slow line takes what it takes — only an+ * idle one: no bytes for `idleMs` and the download counts as offline (code 'timeout').+ * Errors carry { status, code } like api()'s: the server's code on a refusal (media-missing).+ */+export async function apiBlob(path, { expectSize, idleMs = 30000, fetchImpl = globalThis.fetch } = {}) {+ if (MOBILE && !remoteBase) throw notPaired()+ const max = typeof expectSize === 'number' && expectSize >= 0 ? expectSize + 1024 : Infinity+ const ctl = typeof AbortController === 'function' ? new AbortController() : null+ let timer = null+ let idle = false+ // The stall is raced against every await, not left to the abort alone: a WebView whose reader+ // does not settle on abort would otherwise hang the download for good.+ let stalled = null+ const stall = new Promise((_, reject) => { stalled = reject })+ stall.catch(() => {})+ const arm = () => { clearTimeout(timer); timer = setTimeout(() => { idle = true; ctl?.abort(); stalled(timedOut()) }, idleMs) }+ const tooBig = status => failure('HTTP ' + status, 'too-large', status)+ // What loses the race may still reject afterwards, with nobody left listening.+ const orStall = p => { p.catch(() => {}); return Promise.race([p, stall]) }+ arm()+ try {+ const r = await orStall(fetchImpl(mediaUrl(path), { headers: mediaHeaders(), cache: 'no-store', ...(ctl ? { signal: ctl.signal } : {}) }))+ if (!r.ok) {+ let body = null+ try { body = await r.json() } catch { /* not JSON: a proxy's page */ }+ throw Object.assign(new Error((body && body.error) || 'HTTP ' + r.status), { status: r.status, code: (body && body.code) || 'http', data: body || {} })+ }+ const announced = Number(r.headers?.get?.('content-length'))+ if (Number.isFinite(announced) && announced > max) { ctl?.abort(); throw tooBig(r.status) }+ const reader = r.body && typeof r.body.getReader === 'function' ? r.body.getReader() : null+ if (!reader) {+ const b = await orStall(r.blob())+ if (b.size > max) throw tooBig(r.status)+ return b+ }+ const parts = []+ let n = 0+ for (;;) {+ arm()+ const { done, value } = await orStall(reader.read())+ if (done) break+ n += value.length+ if (n > max) { ctl?.abort(); throw tooBig(r.status) }+ parts.push(value)+ }+ return new Blob(parts)+ } catch (e) {+ if (idle) throw timedOut()+ throw e+ } finally { clearTimeout(timer) }+}++/**+ * PUT a file (XMLHttpRequest, because only it reports upload progress). `mime` is the type the+ * MediaRef records, sent explicitly — a Blob read back from a phone's file carries whatever the+ * local server guessed. Resolves with the server's JSON answer. Rejects with { status, code }:+ * the server's own code on a refusal, plus `retryAfter` on a 429; 'proxy-too-large' for a 413+ * that is not the API's JSON (nginx's client_max_body_size, Cloudflare's 100 MB); 'timeout' after+ * `idleMs` without progress; no status at all when the network failed.+ */+export function apiUpload(path, blob, mime, { onProgress, idleMs = 60000, XHR = globalThis.XMLHttpRequest } = {}) {+ if (MOBILE && !remoteBase) return Promise.reject(notPaired())+ return new Promise((resolve, reject) => {+ const xhr = new XHR()+ let timer = null+ let idle = false+ const arm = () => { clearTimeout(timer); timer = setTimeout(() => { idle = true; xhr.abort() }, idleMs) }+ xhr.open('PUT', mediaUrl(path))+ xhr.setRequestHeader('Content-Type', mime)+ for (const [k, v] of Object.entries(mediaHeaders())) xhr.setRequestHeader(k, v)+ if (xhr.upload) xhr.upload.onprogress = e => { arm(); if (onProgress && e.lengthComputable) onProgress(e.loaded, e.total) }+ xhr.onprogress = arm+ xhr.onload = () => {+ clearTimeout(timer)+ let body = null+ try { body = JSON.parse(xhr.responseText) } catch { /* not JSON */ }+ if (body && typeof body !== 'object') body = null+ if (xhr.status >= 200 && xhr.status < 300) {+ if (body) resolve(body)+ else reject(failure(t('The server answered with something other than openGym data.'), 'bad-response', xhr.status))+ return+ }+ if (xhr.status === 413 && !body) { reject(failure('HTTP 413', 'proxy-too-large', 413)); return }+ const e = Object.assign(new Error((body && body.error) || 'HTTP ' + xhr.status), { status: xhr.status, code: (body && body.code) || 'http', data: body || {} })+ if (xhr.status === 429) {+ const header = Number(xhr.getResponseHeader && xhr.getResponseHeader('Retry-After'))+ e.retryAfter = Number(body && body.retryAfter) || (Number.isFinite(header) && header > 0 ? header : 60)+ }+ reject(e)+ }+ xhr.onerror = () => { clearTimeout(timer); reject(Object.assign(new Error('network'), { code: 'network' })) }+ xhr.onabort = () => { clearTimeout(timer); reject(idle ? timedOut() : Object.assign(new Error('aborted'), { code: 'network' })) }+ arm()+ xhr.send(blob)+ })+}+// Bootstraps the connection itself: the base isn't configured yet (that's what this call decides),// so it talks straight to the server the user typed in, no Authorization header.export async function pairRedeem(serverBase, code) {diff --git a/frontend/src/lib/api.media.test.js b/frontend/src/lib/api.media.test.jsnew file mode 100644index 000000000..116e0a3b0--- /dev/null+++ b/frontend/src/lib/api.media.test.js@@ -0,0 +1,94 @@+// @vitest-environment happy-dom+import { describe, it, expect, vi, afterEach } from 'vitest'+import { apiBlob, apiUpload, setRemoteAuth } from './api.js'++afterEach(() => setRemoteAuth('', null))++const stream = (chunks, { stall = false } = {}) => ({+ getReader() {+ let i = 0+ return { read: () => (i < chunks.length ? Promise.resolve({ done: false, value: chunks[i++] }) : stall ? new Promise(() => {}) : Promise.resolve({ done: true })) }+ }+})+const answer = (status, { length, body, json } = {}) => ({+ ok: status >= 200 && status < 300, status,+ headers: { get: k => (k.toLowerCase() === 'content-length' && length != null ? String(length) : null) },+ body, json: async () => { if (json === undefined) throw new Error('not json'); return json }, blob: async () => new Blob([])+})++describe('apiBlob', () => {+ it('reads the body into a Blob, with the Bearer token of a paired phone', async () => {+ setRemoteAuth('https://gym.example.com', 'TOKEN')+ const fetchImpl = vi.fn(async () => answer(200, { length: 6, body: stream([new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])]) }))+ const b = await apiBlob('/api/media/abc', { expectSize: 6, fetchImpl })+ expect(new Uint8Array(await b.arrayBuffer())).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6]))+ expect(fetchImpl.mock.calls[0][0]).toBe('https://gym.example.com/api/media/abc')+ expect(fetchImpl.mock.calls[0][1].headers).toEqual({ Authorization: 'Bearer TOKEN' })+ })++ it('refuses an answer that announces more than the file can be, before reading it', async () => {+ const read = vi.fn()+ const fetchImpl = async () => answer(200, { length: 50 * 1024 * 1024, body: { getReader: () => ({ read }) } })+ await expect(apiBlob('/api/media/abc', { expectSize: 1000, fetchImpl })).rejects.toMatchObject({ code: 'too-large' })+ expect(read).not.toHaveBeenCalled()+ })++ it('cuts off an answer that turns out longer than announced', async () => {+ const fetchImpl = async () => answer(200, { body: stream([new Uint8Array(900), new Uint8Array(900)]) })+ await expect(apiBlob('/api/media/abc', { expectSize: 500, fetchImpl })).rejects.toMatchObject({ code: 'too-large' })+ })++ it('carries the server\'s code on a refusal, and counts a stall as a timeout', async () => {+ await expect(apiBlob('/api/media/abc', { fetchImpl: async () => answer(404, { json: { error: 'no such file', code: 'media-missing' } }) }))+ .rejects.toMatchObject({ status: 404, code: 'media-missing' })+ const stalled = async () => answer(200, { body: stream([new Uint8Array(3)], { stall: true }) })+ await expect(apiBlob('/api/media/abc', { idleMs: 20, fetchImpl: stalled })).rejects.toMatchObject({ code: 'timeout' })+ })+})++// XMLHttpRequest, as far as apiUpload uses it.+function fakeXHR(respond) {+ const made = []+ class X {+ constructor() { this.headers = {}; this.upload = {}; made.push(this) }+ open(method, url) { this.method = method; this.url = url }+ setRequestHeader(k, v) { this.headers[k] = v }+ getResponseHeader(k) { return (this.resHeaders || {})[k] ?? null }+ abort() { this.onabort?.() }+ send(body) { this.body = body; setTimeout(() => respond(this), 0) }+ }+ return { X, made }+}+const reply = (status, text, headers = {}) => x => { x.status = status; x.responseText = text; x.resHeaders = headers; x.onload() }++describe('apiUpload', () => {+ it('PUTs the file with its recorded type and answers the server\'s JSON', async () => {+ setRemoteAuth('https://gym.example.com', 'TOKEN')+ const { X, made } = fakeXHR(reply(201, JSON.stringify({ ok: true, existed: false })))+ const blob = new Blob(['bytes'], { type: 'application/octet-stream' })+ await expect(apiUpload('/api/media/abc', blob, 'video/quicktime', { XHR: X })).resolves.toEqual({ ok: true, existed: false })+ expect(made[0]).toMatchObject({ method: 'PUT', url: 'https://gym.example.com/api/media/abc', body: blob })+ expect(made[0].headers).toEqual({ 'Content-Type': 'video/quicktime', Authorization: 'Bearer TOKEN' })+ })++ it('names a proxy\'s own 413 apart from the API\'s', async () => {+ await expect(apiUpload('/p', new Blob(['x']), 'image/png', { XHR: fakeXHR(reply(413, '<html>Request Entity Too Large</html>')).X }))+ .rejects.toMatchObject({ status: 413, code: 'proxy-too-large' })+ await expect(apiUpload('/p', new Blob(['x']), 'image/png', { XHR: fakeXHR(reply(413, JSON.stringify({ error: 'full', code: 'media-quota', usedMB: 1, quotaMB: 2 }))).X }))+ .rejects.toMatchObject({ status: 413, code: 'media-quota', data: { usedMB: 1, quotaMB: 2 } })+ })++ it('gives a 429 its retryAfter, from the body or the header', async () => {+ await expect(apiUpload('/p', new Blob(['x']), 'image/png', { XHR: fakeXHR(reply(429, JSON.stringify({ code: 'busy', retryAfter: 5 }))).X }))+ .rejects.toMatchObject({ status: 429, code: 'busy', retryAfter: 5 })+ await expect(apiUpload('/p', new Blob(['x']), 'image/png', { XHR: fakeXHR(reply(429, JSON.stringify({ code: 'locked' }), { 'Retry-After': '1800' })).X }))+ .rejects.toMatchObject({ code: 'locked', retryAfter: 1800 })+ })++ it('a network failure has no status, and a stall is a timeout', async () => {+ await expect(apiUpload('/p', new Blob(['x']), 'image/png', { XHR: fakeXHR(x => x.onerror()).X })).rejects.toMatchObject({ code: 'network' })+ const e = await apiUpload('/p', new Blob(['x']), 'image/png', { idleMs: 20, XHR: fakeXHR(() => {}).X }).catch(err => err)+ expect(e.code).toBe('timeout')+ expect(e.status).toBeUndefined()+ })+})diff --git a/frontend/src/lib/media-owed.js b/frontend/src/lib/media-owed.jsnew file mode 100644index 000000000..54bcb66c8--- /dev/null+++ b/frontend/src/lib/media-owed.js@@ -0,0 +1,93 @@+/* Photos and videos this device holds that its server has not confirmed — owed, exactly like a+ * change that has not been pushed yet.+ *+ * The store (useStore.js) asks two things of this module before it lets a sign-out, a+ * disconnect or an account switch wipe the device: how many of the state's media are still+ * waiting (pendingRefCount), and to try once more to send them (settleMedia). Media that have not+ * reached the server are never deleted silently — they go into the stash with the state, whose+ * refs then keep the files alive (lib/media-sync.js localMediaGc).+ *+ * Kept tiny on purpose: the store imports it at start-up, so it must not pull the sync, the+ * ingest or any screen along. lib/media-sync.js registers itself here once it runs, and+ * publishes what it last learned (usage on the server, what is waiting) for Settings and the+ * sign-out sheet.+ */+import { HASH_RE } from './media-refs.js'+import { getMediaStore } from './media-store.js'++let pending = new Set()+let watching = false+let runner = null+let status = { usage: null, pending: 0, deferred: 0, rejected: 0, unavailable: 0, at: 0 }+const listeners = new Set()++// The pending set follows the local store: every put, upload and removal moves it.+function watch() {+ if (watching) return+ watching = true+ const s = getMediaStore()+ const refresh = () => { pending = s.pendingNow() }+ s.subscribe(refresh)+ refresh() // already right when the store was opened before (the editor put a file into it)+ s.ready().then(refresh).catch(() => {})+}++/** Starts following the local store — the store calls it at start-up, so the first sign-out+ * check already knows what is waiting. */+export function loadPending() { watch() }++/**+ * How many custom exercises of `S` have a photo or video (or its poster) that the server has not+ * confirmed. Counted per exercise, not per file, because that is what the sentence counts+ * ("2 photos or videos have not reached your server yet"); one exercise's main file and poster+ * are one photo to the person holding it.+ */+const hasMedia = S => (Array.isArray(S?.customEx) ? S.customEx : []).some(c => c && typeof c === 'object' && c.media && typeof c.media === 'object')++export function pendingRefCount(S) {+ const list = Array.isArray(S?.customEx) ? S.customEx : []+ // A copy without media has nothing to count, and does not need the store opened to say so.+ if (!hasMedia(S)) return 0+ watch()+ if (!pending.size) return 0+ const seen = new Set()+ for (const c of list) {+ const m = c && typeof c === 'object' ? c.media : null+ if (!m || typeof m !== 'object') continue+ const main = typeof m.hash === 'string' && HASH_RE.test(m.hash) ? m.hash : null+ const poster = m.poster && typeof m.poster.hash === 'string' && HASH_RE.test(m.poster.hash) ? m.poster.hash : null+ if ((main && pending.has(main)) || (poster && pending.has(poster))) seen.add(main || poster)+ }+ return seen.size+}++/** lib/media-sync.js, once started: { settle() } — one more forced run, bounded in time. */+export function registerMediaRunner(r) { runner = r }++/** Waits for the upload in flight, then makes one more forced attempt (up to 60 s), and leaves+ * the pending set read from an opened store. Resolves, never rejects: what did not go stays+ * owed. Without a running sync (the tests, a build that never started it) only the reading. */+export async function settleMedia(S) {+ // A copy without media owes none, and leaves the store unopened.+ if (!hasMedia(S)) return+ watch()+ if (runner) { try { await runner.settle() } catch { /* still owed */ } }+ // The count that follows (unsyncedChanges) cannot wait for the store to open; by now it has.+ try { const s = getMediaStore(); await s.ready(); pending = s.pendingNow() } catch { /* the last known set */ }+}++/* What the last run learned, for Settings ("12 of 200 MB used on your server · 1 waiting to+ upload") and the sign-out sheet. */+export function publishMediaStatus(patch) {+ status = { ...status, ...patch, at: Date.now() }+ for (const fn of listeners) { try { fn(status) } catch { /* a listener's own problem */ } }+}+export const getMediaStatus = () => status+export function subscribeMediaStatus(fn) { listeners.add(fn); return () => listeners.delete(fn) }++/** Tests only. */+export function _resetMediaOwed() {+ pending = new Set(); watching = false; runner = null+ status = { usage: null, pending: 0, deferred: 0, rejected: 0, unavailable: 0, at: 0 }+ listeners.clear()+}diff --git a/frontend/src/lib/media-prefetch.js b/frontend/src/lib/media-prefetch.jsindex 372e31a4b..20897a374 100644--- a/frontend/src/lib/media-prefetch.js+++ b/frontend/src/lib/media-prefetch.js@@ -35,7 +35,9 @@ export function planMediaUrls(S, base = globalThis.location?.href, index = EXIDXconst urls = new Set()for (const id of ids) {const ex = index[id]- if (!ex) continue // a custom exercise has no media; an unknown id has nothing to fetch+ // A custom exercise's photo or video is not a file of the shipped dataset: it lives in the+ // local media store and has its own prefetch (lib/media-sync.js). An unknown id has nothing.+ if (!ex || ex.custom) continuefor (const src of [ex.gif && gifSrc(ex), ex.img && imgSrc(ex)]) {if (!src) continueconst u = new URL(src, base)diff --git a/frontend/src/lib/media-store-fs.js b/frontend/src/lib/media-store-fs.jsnew file mode 100644index 000000000..209477bdf--- /dev/null+++ b/frontend/src/lib/media-store-fs.js@@ -0,0 +1,179 @@+/* The phone's backend for lib/media-store.js: plain files in the app's own folder.+ *+ * Library/opengym-media/<hash>.<playExt> one file per photo, GIF or video+ * Library/opengym-media/index.json { hash: { mime, size, putAt, shownAt, pending, rejected } }+ *+ * Directory.Library is iOS's Library folder (kept in device backups, never shown in Files) and+ * Android's files directory. Android's cloud backup is told to leave the folder out+ * (res/xml/backup_rules.xml): Auto Backup skips an app's WHOLE backup once it passes 25 MB, and+ * that would take the state mirror down with the videos.+ *+ * Files are shown by their file URL through the WebView's local server — no copy into memory,+ * and a long video can seek. That server types a file by its extension and will not hand a+ * .mov to <video> as something it plays, so a MOV is stored as .mp4 (MEDIA_MIMES playExt).+ *+ * Crash safety: a file is written to <name>.part and renamed once complete, and the index is+ * written after the files it lists (1 s debounce, flushed when the app goes to the background).+ * At start the two are reconciled: an index entry without its file is dropped, and a file+ * without an entry is checked against its name's sha256 and kept as pending — the upload is+ * idempotent, and the server's /missing answer decides whether it is needed at all.+ *+ * Big files cross the native bridge as base64 in 3 MB pieces (writeFile, then appendFile), never+ * as one 50 MB string.+ */+import { MEDIA_MIMES } from './media-refs.js'+import { sniffKind } from './media-sniff.js'+import { sha256Hex } from './sha256.js'++const DIR = 'opengym-media'+const INDEX = DIR + '/index.json'+const CHUNK = 3 * 1024 * 1024 // a multiple of 3, so no piece but the last has base64 padding+const FILE_RE = /^([0-9a-f]{64})\.(jpg|png|webp|gif|mp4|webm)$/++/** Uint8Array → base64, in slices small enough for String.fromCharCode's argument limit. */+export function toBase64(u8) {+ let s = ''+ for (let i = 0; i < u8.length; i += 0x8000) s += String.fromCharCode.apply(null, u8.subarray(i, i + 0x8000))+ return btoa(s)+}++const fileName = (hash, mime) => hash + '.' + (MEDIA_MIMES[mime]?.playExt || 'bin')++export function fsBackend({+ load = () => import('@capacitor/filesystem'),+ core = () => import('@capacitor/core'),+ app = () => import('@capacitor/app'),+ fetchImpl = (...a) => globalThis.fetch(...a),+ hash = sha256Hex,+ debounceMs = 1000+} = {}) {+ let FS = null, Directory = null, Encoding = null, convert = s => s+ let recs = {}+ let writeTm = null+ let writing = Promise.resolve()+ const uris = new Map()++ const opts = path => ({ path, directory: Directory.Library })+ const uriOf = async name => {+ if (uris.has(name)) return uris.get(name)+ const { uri } = await FS.getUri(opts(DIR + '/' + name))+ const u = convert(uri)+ uris.set(name, u)+ return u+ }+ const readFileBlob = async name => {+ const r = await fetchImpl(await uriOf(name))+ if (!r.ok) throw new Error('read ' + r.status)+ return r.blob()+ }+ const saveIndex = () => {+ writing = writing.then(() => FS.writeFile({ ...opts(INDEX), data: JSON.stringify(recs), encoding: Encoding.UTF8, recursive: true })).catch(() => {})+ return writing+ }+ const scheduleIndex = () => {+ clearTimeout(writeTm)+ writeTm = setTimeout(() => { writeTm = null; saveIndex() }, debounceMs)+ }+ const flush = () => { if (writeTm) { clearTimeout(writeTm); writeTm = null; return saveIndex() } return writing }++ return {+ name: 'fs',+ persistent: true,+ flush,+ async open() {+ try {+ const m = await load()+ FS = m.Filesystem; Directory = m.Directory; Encoding = m.Encoding+ try { const c = await core(); if (c?.Capacitor?.convertFileSrc) convert = u => c.Capacitor.convertFileSrc(u) } catch { /* the raw URI then */ }+ } catch { return false }+ try { await FS.mkdir({ ...opts(DIR), recursive: true }) } catch { /* already there */ }+ try {+ const r = await FS.readFile({ ...opts(INDEX), encoding: Encoding.UTF8 })+ const parsed = JSON.parse(r.data)+ recs = parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {}+ } catch { recs = {} }+ let names = []+ try {+ const r = await FS.readdir(opts(DIR))+ names = (r.files || []).map(f => (typeof f === 'string' ? f : f?.name)).filter(Boolean)+ } catch { names = [] }+ const present = new Set(names)+ let changed = false+ // Entries whose file is gone: the file was never renamed into place, or was deleted.+ for (const [h, r] of Object.entries(recs)) {+ if (!r || !MEDIA_MIMES[r.mime] || !present.has(fileName(h, r.mime))) { delete recs[h]; changed = true }+ }+ for (const name of names) {+ if (name.endsWith('.part')) { FS.deleteFile(opts(DIR + '/' + name)).catch(() => {}); continue }+ const m = FILE_RE.exec(name)+ if (!m || recs[m[1]]) continue+ // A file the index never got to list (the app died within the debounce). Its name is+ // its hash; one whose bytes do not match is half a write and goes.+ try {+ const blob = await readFileBlob(name)+ const head = new Uint8Array(await blob.slice(0, 64).arrayBuffer())+ const sniffed = sniffKind(head)+ if ((await hash(blob)) !== m[1] || !sniffed || !MEDIA_MIMES[sniffed.mime]) throw new Error('mismatch')+ recs[m[1]] = { hash: m[1], mime: sniffed.mime, size: blob.size, putAt: Date.now(), shownAt: 0, pending: true, rejected: false }+ changed = true+ } catch {+ FS.deleteFile(opts(DIR + '/' + name)).catch(() => {})+ }+ }+ if (changed) await saveIndex()+ app().then(({ App }) => App.addListener('appStateChange', ({ isActive }) => { if (!isActive) flush() })).catch(() => {})+ return true+ },+ async all() { return Object.entries(recs).map(([h, r]) => ({ ...r, hash: h })) },+ async getBlob(h) {+ const r = recs[h]+ if (!r) return null+ try { return await readFileBlob(fileName(h, r.mime)) } catch { return null }+ },+ async put(rec, blob) {+ const name = fileName(rec.hash, rec.mime)+ const part = DIR + '/' + name + '.part'+ for (let o = 0; o < blob.size || o === 0; o += CHUNK) {+ const data = toBase64(new Uint8Array(await blob.slice(o, o + CHUNK).arrayBuffer()))+ if (o === 0) await FS.writeFile({ ...opts(part), data, recursive: true })+ else await FS.appendFile({ ...opts(part), data })+ if (!blob.size) break+ }+ try { await FS.deleteFile(opts(DIR + '/' + name)) } catch { /* not there, as expected */ }+ await FS.rename({ from: part, to: DIR + '/' + name, directory: Directory.Library, toDirectory: Directory.Library })+ uris.delete(name)+ recs[rec.hash] = { ...rec }+ scheduleIndex()+ },+ async patch(h, fields) {+ if (!recs[h]) return+ recs[h] = { ...recs[h], ...fields }+ scheduleIndex()+ },+ async remove(h) {+ const r = recs[h]+ if (!r) return+ delete recs[h]+ const name = fileName(h, r.mime)+ uris.delete(name)+ // The file first: a crash in between leaves an index entry without its file, which the+ // next start drops. The other order would leave a file without an entry, which that start+ // would take for an unlisted new one and keep.+ try { await FS.deleteFile(opts(DIR + '/' + name)) } catch { /* already gone */ }+ scheduleIndex()+ },+ async clear() {+ recs = {}+ uris.clear()+ clearTimeout(writeTm)+ writeTm = null+ await writing+ try { await FS.rmdir({ ...opts(DIR), recursive: true }) } catch { /* nothing there */ }+ try { await FS.mkdir({ ...opts(DIR), recursive: true }) } catch { /* the next put makes it */ }+ },+ async fileUrl(h) {+ const r = recs[h]+ return r ? uriOf(fileName(h, r.mime)) : null+ }+ }+}diff --git a/frontend/src/lib/media-store-fs.test.js b/frontend/src/lib/media-store-fs.test.jsnew file mode 100644index 000000000..36cc023d5--- /dev/null+++ b/frontend/src/lib/media-store-fs.test.js@@ -0,0 +1,117 @@+import { describe, it, expect, vi } from 'vitest'+import { fsBackend, toBase64 } from './media-store-fs.js'+import { sha256Hex } from './sha256.js'+import { jpeg, mp4 } from './media-samples.test-util.js'++// Capacitor's Filesystem, in memory: files as base64 (or UTF-8 text for index.json), keyed by+// path. Each call is recorded, so the tests can see how a big file crossed the bridge.+function fakeFs() {+ const files = new Map()+ const calls = []+ const fromB64 = s => new Uint8Array(Buffer.from(s, 'base64'))+ const Filesystem = {+ async mkdir() {},+ async readFile({ path, encoding }) {+ calls.push(['readFile', path])+ if (!files.has(path)) throw new Error('File does not exist')+ const v = files.get(path)+ return { data: encoding ? new TextDecoder().decode(v) : toBase64(v) }+ },+ async writeFile({ path, data, encoding }) {+ calls.push(['writeFile', path, encoding ? data.length : fromB64(data).length])+ files.set(path, encoding ? new TextEncoder().encode(data) : fromB64(data))+ },+ async appendFile({ path, data }) {+ calls.push(['appendFile', path, fromB64(data).length])+ const cur = files.get(path) || new Uint8Array(0)+ const add = fromB64(data)+ const next = new Uint8Array(cur.length + add.length)+ next.set(cur); next.set(add, cur.length)+ files.set(path, next)+ },+ async rename({ from, to }) { calls.push(['rename', from, to]); files.set(to, files.get(from)); files.delete(from) },+ async deleteFile({ path }) { calls.push(['deleteFile', path]); if (!files.delete(path)) throw new Error('missing') },+ async readdir({ path }) { return { files: [...files.keys()].filter(k => k.startsWith(path + '/')).map(k => ({ name: k.slice(path.length + 1), type: 'file' })) } },+ async getUri({ path }) { return { uri: 'file:///lib/' + path } },+ async rmdir({ path }) { for (const k of [...files.keys()]) if (k.startsWith(path + '/')) files.delete(k) }+ }+ const mod = { Filesystem, Directory: { Library: 'LIBRARY' }, Encoding: { UTF8: 'utf8' } }+ // The WebView's local server: a file URL answers the file's bytes.+ const fetchImpl = async url => {+ const path = String(url).replace('capacitor://localhost/_capacitor_file_/lib/', '')+ const v = files.get(path)+ return v ? { ok: true, blob: async () => new Blob([v]) } : { ok: false, status: 404 }+ }+ const core = async () => ({ Capacitor: { convertFileSrc: u => u.replace('file:///', 'capacitor://localhost/_capacitor_file_/') } })+ const app = async () => ({ App: { addListener: vi.fn() } })+ return { files, calls, deps: { load: async () => mod, core, app, fetchImpl, debounceMs: 5 } }+}+const tick = ms => new Promise(r => setTimeout(r, ms))++describe('the phone backend', () => {+ it('writes a big file in 3 MB pieces, renames it into place, and lists it after the index', async () => {+ const fs = fakeFs()+ const be = fsBackend(fs.deps)+ expect(await be.open()).toBe(true)+ const big = new Uint8Array(7 * 1024 * 1024)+ for (let i = 0; i < big.length; i++) big[i] = i & 0xff+ const hash = await sha256Hex(big)+ await be.put({ hash, mime: 'video/quicktime', size: big.length, putAt: 1, shownAt: 0, pending: true, rejected: false }, new Blob([big]))+ const writes = fs.calls.filter(c => c[0] === 'writeFile' || c[0] === 'appendFile')+ expect(writes.map(c => [c[0], c[2]])).toEqual([['writeFile', 3 * 1024 * 1024], ['appendFile', 3 * 1024 * 1024], ['appendFile', 1024 * 1024]])+ expect(writes.every(c => c[1] === `opengym-media/${hash}.mp4.part`)).toBe(true) // a MOV is named .mp4+ // Compared by hash: a deep equal over 7 MB element by element takes minutes.+ expect(await sha256Hex(fs.files.get(`opengym-media/${hash}.mp4`))).toBe(hash)+ expect(fs.files.has(`opengym-media/${hash}.mp4.part`)).toBe(false)+ expect(await be.fileUrl(hash)).toBe(`capacitor://localhost/_capacitor_file_/lib/opengym-media/${hash}.mp4`)+ expect(await sha256Hex(await be.getBlob(hash))).toBe(hash)+ await tick(20)+ expect(JSON.parse(new TextDecoder().decode(fs.files.get('opengym-media/index.json')))[hash]).toMatchObject({ mime: 'video/quicktime', pending: true })+ })++ it('on start, keeps a file the index never listed as pending, and drops what points at nothing', async () => {+ const fs = fakeFs()+ const photo = jpeg()+ const video = mp4().file+ const ph = await sha256Hex(photo)+ const vh = await sha256Hex(video)+ const gone = 'f'.repeat(64)+ fs.files.set(`opengym-media/${ph}.jpg`, photo)+ fs.files.set(`opengym-media/${vh}.mp4`, video)+ fs.files.set(`opengym-media/${'e'.repeat(64)}.jpg`, photo) // bytes that do not match the name+ fs.files.set(`opengym-media/${'d'.repeat(64)}.webp.part`, photo) // half a write+ fs.files.set('opengym-media/index.json', new TextEncoder().encode(JSON.stringify({+ [vh]: { hash: vh, mime: 'video/mp4', size: video.length, putAt: 5, shownAt: 0, pending: false, rejected: false },+ [gone]: { hash: gone, mime: 'image/webp', size: 10, putAt: 5, shownAt: 0, pending: false, rejected: false }+ })))+ const be = fsBackend(fs.deps)+ await be.open()+ const recs = Object.fromEntries((await be.all()).map(r => [r.hash, r]))+ expect(Object.keys(recs).sort()).toEqual([ph, vh].sort())+ expect(recs[ph]).toMatchObject({ mime: 'image/jpeg', size: photo.length, pending: true })+ expect(recs[vh]).toMatchObject({ pending: false })+ await tick(5)+ expect(fs.files.has(`opengym-media/${'e'.repeat(64)}.jpg`)).toBe(false)+ expect(fs.files.has(`opengym-media/${'d'.repeat(64)}.webp.part`)).toBe(false)+ })++ it('removes the file before the index entry, and clear empties the folder', async () => {+ const fs = fakeFs()+ const be = fsBackend(fs.deps)+ await be.open()+ const photo = jpeg()+ const h = await sha256Hex(photo)+ await be.put({ hash: h, mime: 'image/jpeg', size: photo.length, putAt: 1, shownAt: 0, pending: true, rejected: false }, new Blob([photo]))+ await be.remove(h)+ expect(fs.files.has(`opengym-media/${h}.jpg`)).toBe(false)+ expect(await be.all()).toEqual([])+ await be.put({ hash: h, mime: 'image/jpeg', size: photo.length, putAt: 1, shownAt: 0, pending: true, rejected: false }, new Blob([photo]))+ await be.clear()+ expect([...fs.files.keys()].filter(k => k.startsWith('opengym-media/') && !k.endsWith('index.json'))).toEqual([])+ })++ it('answers false when there is no Filesystem plugin at all', async () => {+ const be = fsBackend({ load: async () => { throw new Error('not a Capacitor build') } })+ expect(await be.open()).toBe(false)+ })+})diff --git a/frontend/src/lib/media-store-idb.js b/frontend/src/lib/media-store-idb.jsnew file mode 100644index 000000000..0bbdf060d--- /dev/null+++ b/frontend/src/lib/media-store-idb.js@@ -0,0 +1,97 @@+/* The browser's backend for lib/media-store.js: IndexedDB database 'opengym-media', object store+ * 'blobs', one record per file keyed by its hash — { hash, blob, mime, size, putAt, shownAt,+ * pending, rejected }.+ *+ * IndexedDB rather than the Cache API or localStorage: it holds Blobs as they are (a browser keeps+ * them on disk and hands back a reference, so listing records does not read the bytes), it is+ * there for guests and the demo as much as for a signed-in browser, and it is per origin like+ * the rest of the profile. Opening can fail — some private windows block it, an old Firefox+ * refuses Blobs in it — and then open() answers false and the store falls back to memory.+ */++const DB = 'opengym-media'+const STORE = 'blobs'++const req = r => new Promise((resolve, reject) => { r.onsuccess = () => resolve(r.result); r.onerror = () => reject(r.error) })+const done = tx => new Promise((resolve, reject) => {+ tx.oncomplete = () => resolve()+ tx.onerror = () => reject(tx.error)+ tx.onabort = () => reject(tx.error || new Error('aborted'))+})+const meta = r => {+ const { blob, ...rest } = r+ return rest+}++export function idbBackend({ idb = globalThis.indexedDB } = {}) {+ let db = null+ const tx = mode => db.transaction(STORE, mode)+ return {+ name: 'idb',+ persistent: true,+ async open() {+ if (!idb) return false+ try {+ db = await new Promise((resolve, reject) => {+ const r = idb.open(DB, 1)+ r.onupgradeneeded = () => { if (!r.result.objectStoreNames.contains(STORE)) r.result.createObjectStore(STORE, { keyPath: 'hash' }) }+ r.onsuccess = () => resolve(r.result)+ r.onerror = () => reject(r.error)+ r.onblocked = () => reject(new Error('blocked'))+ })+ // Another tab upgrading a later version asks this one to let go.+ 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).delete('.probe')+ await done(t)+ return true+ } catch {+ try { db?.close() } catch { /* nothing to close */ }+ db = null+ return false+ }+ },+ async all() {+ const out = []+ await new Promise((resolve, reject) => {+ const r = tx('readonly').objectStore(STORE).openCursor()+ r.onsuccess = () => {+ const c = r.result+ if (!c) return resolve()+ if (typeof c.value?.hash === 'string' && !c.value.hash.startsWith('.')) out.push(meta(c.value))+ c.continue()+ }+ r.onerror = () => reject(r.error)+ })+ return out+ },+ async getBlob(hash) {+ const r = await req(tx('readonly').objectStore(STORE).get(hash))+ return r?.blob || null+ },+ async put(rec, blob) {+ const t = tx('readwrite')+ t.objectStore(STORE).put({ ...rec, blob })+ await done(t)+ },+ async patch(hash, fields) {+ const t = tx('readwrite')+ const s = t.objectStore(STORE)+ const r = s.get(hash)+ r.onsuccess = () => { if (r.result) s.put({ ...r.result, ...fields }) }+ await done(t)+ },+ async remove(hash) {+ const t = tx('readwrite')+ t.objectStore(STORE).delete(hash)+ await done(t)+ },+ async clear() {+ const t = tx('readwrite')+ t.objectStore(STORE).clear()+ await done(t)+ }+ }+}diff --git a/frontend/src/lib/media-store.js b/frontend/src/lib/media-store.jsnew file mode 100644index 000000000..04cdd3360--- /dev/null+++ b/frontend/src/lib/media-store.js@@ -0,0 +1,261 @@+/* The one local store for the photos, GIFs and videos of custom exercises.+ *+ * Every mode shows media from here: a signed-in browser, a paired phone, a phone in local mode, a+ * guest and the demo. A file gets here either from the editor (picked on this device) or from the+ * server (lib/media-sync.js fetchToStore, checked against its sha256 first); it is then shown from+ * here, offline included. That one path is also what lets a paired phone show a file at all — an+ * <img src> cannot carry the phone's Authorization header, a fetch can.+ *+ * Backends: IndexedDB in a browser (media-store-idb.js), the app's own folder on a phone+ * (media-store-fs.js), and memory — for the tests, and for display only when IndexedDB is+ * blocked (a private window in some browsers). The store says which through `persistent`: the+ * editor refuses new files when nothing here would outlive the tab.+ *+ * A record is { hash, mime, size, putAt, shownAt, pending, rejected }:+ * pending the server has not confirmed it holds the file (picked here, or imported)+ * rejected the server refused it for good (too large, wrong type): it stays here, still+ * pending, and is not offered again until someone asks (Settings → Photos & videos)+ * shownAt when it was last displayed, written at most once an hour — what eviction reads+ *+ * url(hash) hands out an object URL (browser) or a file URL (phone), counted per hash: each+ * caller releases it when done, and an object URL is revoked 30 s after the last release, so a+ * list scrolling back and forth does not decode the same file again and again. Object URLs are+ * only ever made from `new Blob([blob], { type })` with the stored, allowlisted media type: a+ * blob: URL typed text/html would run as this origin if anyone navigated to it.+ */+import { MEDIA_MIMES } from './media-refs.js'+import { MOBILE } from './mobile.js'+import { idbBackend } from './media-store-idb.js'+import { fsBackend } from './media-store-fs.js'++const HOUR = 3600000++/** A backend that forgets everything with the page — the tests', and the fallback's. */+export function memoryBackend() {+ const recs = new Map()+ const blobs = new Map()+ return {+ name: 'memory',+ persistent: false,+ async open() { return true },+ async all() { return [...recs.values()].map(r => ({ ...r })) },+ async getBlob(hash) { return blobs.get(hash) || null },+ async put(rec, blob) { recs.set(rec.hash, { ...rec }); if (blob) blobs.set(rec.hash, blob) },+ async patch(hash, fields) { const r = recs.get(hash); if (r) recs.set(hash, { ...r, ...fields }) },+ async remove(hash) { recs.delete(hash); blobs.delete(hash) },+ async clear() { recs.clear(); blobs.clear() }+ }+}++// The type a <video> can play a file as. A MOV is an MP4 in all that matters to a browser, and+// most refuse "video/quicktime" outright (Chrome, Firefox, Android) while playing the same bytes+// typed video/mp4. Safari plays it as what it is.+export function playType(mime, canPlayType = defaultCanPlay) {+ if (mime !== 'video/quicktime') return mime+ return canPlayType('video/quicktime') ? mime : 'video/mp4'+}+function defaultCanPlay(type) {+ try { return typeof document !== 'undefined' ? document.createElement('video').canPlayType(type) : '' } catch { return '' }+}++export function createMediaStore(backend, {+ fallback = memoryBackend,+ now = () => Date.now(),+ objectURL = globalThis.URL,+ revokeDelayMs = 30000,+ canPlayType = defaultCanPlay+} = {}) {+ let be = backend+ let opening = null+ let persistent = !!backend.persistent+ const index = new Map()+ const listeners = new Set()+ // hash → { url, refs, timer, file } (file: a phone's file URL, which is never revoked)+ const urls = new Map()+ const making = new Map() // hash → the url() in progress, so two callers make one++ const emit = () => { for (const fn of listeners) { try { fn() } catch { /* a listener's own problem */ } } }++ const ready = () => {+ if (!opening) {+ opening = (async () => {+ let ok = false+ try { ok = await be.open() } catch { ok = false }+ if (!ok) { be = fallback(); persistent = false; await be.open() }+ else persistent = !!be.persistent+ try { for (const r of await be.all()) index.set(r.hash, r) } catch { /* an empty index then */ }+ emit()+ })()+ }+ return opening+ }++ const revokeNow = hash => {+ const u = urls.get(hash)+ if (!u) return+ clearTimeout(u.timer)+ urls.delete(hash)+ if (!u.file) { try { objectURL.revokeObjectURL(u.url) } catch { /* already gone */ } }+ }++ const store = {+ ready,+ get persistent() { return persistent },+ get backendName() { return be.name },+ subscribe(fn) { listeners.add(fn); return () => listeners.delete(fn) },++ async has(hash) { await ready(); return index.has(hash) },+ /** The record with its blob, or null. */+ async get(hash) {+ await ready()+ const r = index.get(hash)+ if (!r) return null+ const blob = await be.getBlob(hash).catch(() => null)+ if (!blob) return null+ return { blob, mime: r.mime, size: r.size, pending: !!r.pending, rejected: !!r.rejected }+ },+ /** Keeps `blob` under `hash`. The same hash is the same bytes, so a second put only updates+ * the record — `pending` as the caller says: false when the server has just sent it. */+ async put(hash, blob, { mime, pending = true } = {}) {+ if (!MEDIA_MIMES[mime]) throw new Error('media-store: not a media type: ' + mime)+ await ready()+ const t = now()+ const cur = index.get(hash)+ if (cur) {+ const fields = { pending: !!pending, rejected: pending ? !!cur.rejected : false, putAt: t }+ index.set(hash, { ...cur, ...fields })+ await be.patch(hash, fields)+ } else {+ const rec = { hash, mime, size: blob.size, putAt: t, shownAt: 0, pending: !!pending, rejected: false }+ await be.put(rec, blob)+ index.set(hash, rec)+ }+ emit()+ },+ /** A URL to show the file by, or null when it is not here. Release it when done. */+ async url(hash) {+ await ready()+ const have = urls.get(hash)+ if (have) { have.refs++; clearTimeout(have.timer); have.timer = null; return have.url }+ if (making.has(hash)) {+ const u = await making.get(hash)+ if (u) { const e = urls.get(hash); if (e) { e.refs++; clearTimeout(e.timer); e.timer = null } }+ return u+ }+ const r = index.get(hash)+ if (!r) return null+ const job = (async () => {+ let url = null, file = false+ if (typeof be.fileUrl === 'function') { url = await be.fileUrl(hash, r).catch(() => null); file = !!url }+ if (!url) {+ const blob = await be.getBlob(hash).catch(() => null)+ if (!blob) return null+ url = objectURL.createObjectURL(new Blob([blob], { type: playType(r.mime, canPlayType) }))+ }+ urls.set(hash, { url, refs: 0, timer: null, file })+ return url+ })()+ making.set(hash, job)+ let url+ try { url = await job } finally { making.delete(hash) }+ const entry = url && urls.get(hash)+ if (!entry) return null // not here, or removed while the URL was being made+ entry.refs+++ // When it was last shown decides what goes first when the cache is full; an hour is+ // precise enough, and a write per display would be one per scroll.+ if (!t0(r.shownAt) || t0(r.shownAt) + HOUR <= now()) {+ r.shownAt = now()+ be.patch(hash, { shownAt: r.shownAt }).catch(() => {})+ }+ return url+ },+ release(hash) {+ const u = urls.get(hash)+ if (!u) return+ u.refs = Math.max(0, u.refs - 1)+ if (u.refs || u.timer) return+ u.timer = setTimeout(() => { const cur = urls.get(hash); if (cur && !cur.refs) revokeNow(hash) }, revokeDelayMs)+ },+ async markSynced(hash) {+ await ready()+ const r = index.get(hash)+ if (!r || (!r.pending && !r.rejected)) return+ Object.assign(r, { pending: false, rejected: false })+ await be.patch(hash, { pending: false, rejected: false })+ emit()+ },+ async markRejected(hash) {+ await ready()+ const r = index.get(hash)+ if (!r || r.rejected) return+ r.rejected = true+ await be.patch(hash, { rejected: true })+ emit()+ },+ async remove(hash) {+ await ready()+ if (!index.has(hash)) return+ revokeNow(hash)+ index.delete(hash)+ await be.remove(hash).catch(() => {})+ emit()+ },+ /** Deletes every record not in `keep`, except those put at or after `keepPutAfter` — a file+ * picked meanwhile in an editor that is still open, or by another tab. */+ async retainOnly(keep, { keepPutAfter = now() } = {}) {+ await ready()+ let removed = 0+ for (const [hash, r] of [...index]) {+ if (keep.has(hash) || (r.putAt || 0) >= keepPutAfter) continue+ revokeNow(hash)+ index.delete(hash)+ await be.remove(hash).catch(() => {})+ removed+++ }+ if (removed) emit()+ return removed+ },+ async clearAll() {+ await ready()+ for (const hash of [...urls.keys()]) revokeNow(hash)+ index.clear()+ await be.clear().catch(() => {})+ emit()+ },+ async list() { await ready(); return [...index.values()].map(r => ({ ...r })) },+ async usage() {+ await ready()+ let bytes = 0+ for (const r of index.values()) bytes += r.size || 0+ return { bytes, count: index.size }+ },+ async pendingHashes() { await ready(); return store.pendingNow() },+ /** The pending hashes as the index holds them right now — empty before it has loaded. For+ * the sign-out check (lib/media-owed.js), which cannot wait. */+ pendingNow() { return new Set([...index.values()].filter(r => r.pending).map(r => r.hash)) },+ /** The rejected hashes, for Settings to say what the server would not take. */+ rejectedNow() { return new Set([...index.values()].filter(r => r.rejected).map(r => r.hash)) }+ }+ return store+}+const t0 = v => (typeof v === 'number' ? v : 0)++let shared = null+/** The app's store: the phone's folder in the mobile build, IndexedDB everywhere else. Made on+ * first use, so importing this module opens nothing. */+export function getMediaStore() {+ if (!shared) shared = createMediaStore(MOBILE ? fsBackend() : idbBackend())+ return shared+}+/** Tests only: swap the shared store (null makes a fresh one on next use). */+export function _setMediaStore(s) { shared = s }++/** The shared store under the name the rest of the app uses. A thin forwarder, so that importing+ * it is free and a test can swap what it forwards to (_setMediaStore). */+export const mediaStore = new Proxy({}, {+ get(_, key) {+ const s = getMediaStore()+ const v = s[key]+ return typeof v === 'function' ? v.bind(s) : v+ }+})diff --git a/frontend/src/lib/media-store.test.js b/frontend/src/lib/media-store.test.jsnew file mode 100644index 000000000..0e7253eb7--- /dev/null+++ b/frontend/src/lib/media-store.test.js@@ -0,0 +1,143 @@+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'+import { createMediaStore, memoryBackend, playType } from './media-store.js'++const H1 = '1'.repeat(64)+const H2 = '2'.repeat(64)+const H3 = '3'.repeat(64)++function objectURLs() {+ let n = 0+ const live = new Set()+ const made = []+ return {+ live, made,+ createObjectURL: blob => { const u = 'blob:test/' + (++n); live.add(u); made.push({ u, type: blob.type }); return u },+ revokeObjectURL: u => { live.delete(u) }+ }+}++describe('media store (memory backend)', () => {+ let clock, urls, store+ beforeEach(() => {+ vi.useFakeTimers()+ clock = 1_000_000+ urls = objectURLs()+ store = createMediaStore(memoryBackend(), { now: () => clock, objectURL: urls, canPlayType: () => '' })+ })+ afterEach(() => vi.useRealTimers())++ it('keeps a file as pending, and a second put of the same hash only updates the record', async () => {+ await store.put(H1, new Blob(['abc']), { mime: 'image/webp' })+ expect(await store.list()).toEqual([{ hash: H1, mime: 'image/webp', size: 3, putAt: clock, shownAt: 0, pending: true, rejected: false }])+ clock += 5+ await store.put(H1, new Blob(['abc']), { mime: 'image/webp', pending: false })+ expect((await store.get(H1)).pending).toBe(false)+ expect((await store.list())[0].putAt).toBe(clock)+ expect(await store.usage()).toEqual({ bytes: 3, count: 1 })+ })++ it('refuses a type outside the allowlist', async () => {+ await expect(store.put(H1, new Blob(['<html>']), { mime: 'text/html' })).rejects.toThrow()+ await expect(store.put(H1, new Blob(['<svg/>']), { mime: 'image/svg+xml' })).rejects.toThrow()+ })++ it('hands out one object URL per file, typed as stored, and revokes it 30 s after the last release', async () => {+ await store.put(H1, new Blob(['abc'], { type: 'text/html' }), { mime: 'image/gif' })+ const a = await store.url(H1)+ const b = await store.url(H1)+ expect(a).toBe(b)+ expect(urls.made).toEqual([{ u: a, type: 'image/gif' }])+ store.release(H1)+ vi.advanceTimersByTime(60000)+ expect(urls.live.has(a)).toBe(true) // one holder left+ store.release(H1)+ vi.advanceTimersByTime(29000)+ expect(urls.live.has(a)).toBe(true)+ const again = await store.url(H1) // taken again within the 30 s: kept+ expect(again).toBe(a)+ store.release(H1)+ vi.advanceTimersByTime(30001)+ expect(urls.live.has(a)).toBe(false)+ expect(await store.url(H2)).toBeNull()+ })++ it('two callers asking at once get one URL', async () => {+ await store.put(H1, new Blob(['abc']), { mime: 'image/webp' })+ const [a, b] = await Promise.all([store.url(H1), store.url(H1)])+ expect(a).toBe(b)+ expect(urls.made).toHaveLength(1)+ store.release(H1); store.release(H1)+ vi.advanceTimersByTime(30001)+ expect(urls.live.size).toBe(0)+ })++ it('types a MOV as video/mp4 where the browser would not play it as what it is', async () => {+ await store.put(H1, new Blob(['mov']), { mime: 'video/quicktime' })+ await store.url(H1)+ expect(urls.made[0].type).toBe('video/mp4')+ expect(playType('video/quicktime', () => 'maybe')).toBe('video/quicktime')+ expect(playType('video/webm', () => '')).toBe('video/webm')+ })++ it('writes when a file was shown at most once an hour', async () => {+ await store.put(H1, new Blob(['abc']), { mime: 'image/webp' })+ await store.url(H1); store.release(H1)+ const first = (await store.list())[0].shownAt+ expect(first).toBe(clock)+ vi.advanceTimersByTime(30001) // revoked, so the next url() makes a new one+ clock += 10 * 60000+ await store.url(H1); store.release(H1)+ expect((await store.list())[0].shownAt).toBe(first)+ vi.advanceTimersByTime(30001)+ clock += 60 * 60000+ await store.url(H1); store.release(H1)+ expect((await store.list())[0].shownAt).toBe(clock)+ })++ it('retainOnly keeps the set, and whatever was put at or after the given moment', async () => {+ await store.put(H1, new Blob(['1']), { mime: 'image/webp' })+ await store.put(H2, new Blob(['2']), { mime: 'image/webp' })+ const since = clock+ clock += 1+ await store.put(H3, new Blob(['3']), { mime: 'image/webp' })+ const removed = await store.retainOnly(new Set([H1]), { keepPutAfter: since + 1 })+ expect(removed).toBe(1)+ expect((await store.list()).map(r => r.hash).sort()).toEqual([H1, H3])+ })++ it('marks synced and rejected, and lists what is pending', async () => {+ await store.put(H1, new Blob(['1']), { mime: 'image/webp' })+ await store.put(H2, new Blob(['2']), { mime: 'image/webp' })+ expect([...await store.pendingHashes()].sort()).toEqual([H1, H2])+ await store.markSynced(H1)+ await store.markRejected(H2)+ expect([...store.pendingNow()]).toEqual([H2])+ expect([...store.rejectedNow()]).toEqual([H2])+ expect((await store.get(H2)).rejected).toBe(true)+ // A rejected file put again (the user picked it anew) is still refused until the server+ // says otherwise; one the server sends is simply synced.+ await store.put(H2, new Blob(['2']), { mime: 'image/webp', pending: false })+ expect((await store.get(H2))).toMatchObject({ pending: false, rejected: false })+ })++ it('tells its listeners about every change, and clearAll empties it', async () => {+ const seen = vi.fn()+ store.subscribe(seen)+ await store.put(H1, new Blob(['1']), { mime: 'image/webp' })+ await store.remove(H1)+ expect(seen.mock.calls.length).toBeGreaterThanOrEqual(2)+ await store.put(H2, new Blob(['2']), { mime: 'image/webp' })+ await store.clearAll()+ expect(await store.list()).toEqual([])+ })++ it('falls back to memory, and says so, when its backend cannot open', async () => {+ const broken = { name: 'idb', persistent: true, open: async () => false }+ const s = createMediaStore(broken, { objectURL: urls })+ await s.ready()+ expect(s.persistent).toBe(false)+ expect(s.backendName).toBe('memory')+ await s.put(H1, new Blob(['x']), { mime: 'image/png' })+ expect(await s.has(H1)).toBe(true)+ })+})diff --git a/frontend/src/lib/media-sync.js b/frontend/src/lib/media-sync.jsnew file mode 100644index 000000000..266eb8ee8--- /dev/null+++ b/frontend/src/lib/media-sync.js@@ -0,0 +1,378 @@+/* Photos and videos of custom exercises between this device and its server.+ *+ * ONE mechanism for every move between modes, keyed by content hash and safe to repeat: ask the+ * server which of the state's files it lacks (POST /api/media/missing), then send the ones this+ * device holds (PUT /api/media/{hash}). That covers, without a special case each:+ * - a guest signing up or in (adoptProfile keeps the guest's exercises, their refs point at+ * files in this browser's store, and they go up),+ * - a phone in local mode being paired (the same, from the phone's folder),+ * - a stash handed back after a forced sign-out (applyStash, then its files go up),+ * - a backup imported from a zip (its files come in as pending),+ * - the server having dropped a file after its grace period (any device that still has it+ * sends it again),+ * - a server without media: nothing is sent, the files stay here and pending, and Settings+ * says they are waiting.+ * Downloads go the other way (fetchToStore): each is checked against its hash and its type before+ * it is kept, so a login page or a proxy's answer is never stored under a file's name.+ *+ * It also keeps the local store tidy (localMediaGc) and makes the plan's custom media local ahead+ * of time (startCustomMediaPrefetch), so a workout opened without a network still shows them.+ */+import { referencedHashes, referencedFiles, MEDIA_MIMES } from './media-refs.js'+import { sniffKind } from './media-sniff.js'+import { sha256Hex } from './sha256.js'+import { mediaStore as sharedStore } from './media-store.js'+import { api, apiBlob, apiUpload } from './api.js'+import { prefetchAllowed } from './media-prefetch.js'+import { MB, BG_UPLOAD_METERED_MAX_MB, LOCAL_CACHE_MAX_MB, LOCAL_GC_GRACE_MS } from './media-limits.js'+import { registerMediaRunner, publishMediaStatus, pendingRefCount, loadPending } from './media-owed.js'+import { t } from './i18n-core.js'++const MIN = 60000+const DEDUPE_MS = 10 * MIN+const SETTLE_BUDGET_MS = 60000+const MISSING_CHUNK = 1000+const UPLOAD_BACKOFF = [1, 2, 5, 15, 60].map(m => m * MIN)+const MISSING_BACKOFF = [30000, 2 * MIN, 10 * MIN, 30 * MIN]+// Refusals that will not change by trying again: the file stays here, pending, and is not+// offered again until someone asks (the Settings row).+const REJECTED = new Set(['media-too-large', 'media-type', 'media-invalid', 'media-too-long', 'proxy-too-large', 'hash-mismatch'])+const PERSISTED_KEY = 'gym_state_v1'++const sleep = ms => new Promise(r => setTimeout(r, ms))+const isNetwork = e => !e || e.status == null || e.code === 'timeout' || e.code === 'network'++/**+ * One sync. `deps` are what it talks to — the app store (getState/subscribe), the local media+ * store, the transports — all injectable, which is how media-sync.test.js runs it.+ */+export function createMediaSync(deps = {}) {+ const d = {+ media: sharedStore,+ // Reached through arrows, so a test that mocks lib/api.js without these never trips over+ // them before a sync actually runs.+ api: (...a) => api(...a),+ apiBlob: (...a) => apiBlob(...a),+ apiUpload: (...a) => apiUpload(...a),+ sha256: sha256Hex,+ allowed: () => prefetchAllowed(),+ now: () => Date.now(),+ toast: msg => import('../store/useUI.js').then(({ useUI }) => useUI.getState().toast(msg)).catch(() => {}),+ locks: globalThis.navigator?.locks,+ storage: (() => { try { return globalThis.localStorage } catch { return null } })(),+ ...deps+ }+ let store = d.store || null+ let running = null+ let again = null+ let lastRun = { refs: '', at: 0, complete: false }+ let retryNotBefore = 0+ let quotaTold = false+ const rejectTold = new Set()+ const upBackoff = new Map() // hash → { step, until }+ const missBackoff = new Map() // hash → { step, until }+ const fetching = new Map() // hash → the download in flight++ const bump = (map, hash, steps) => {+ const cur = map.get(hash)+ const step = cur ? Math.min(cur.step + 1, steps.length - 1) : 0+ map.set(hash, { step, until: d.now() + steps[step] })+ }+ const active = (map, hash) => (map.get(hash)?.until || 0) > d.now()+ const withLock = (name, fn) => (d.locks && typeof d.locks.request === 'function' ? d.locks.request(name, fn) : fn())++ const publishPending = async extra => {+ const S = store?.getState().S+ publishMediaStatus({ pending: S ? pendingRefCount(S) : 0, ...extra })+ }++ async function run({ force = false, retryRejected = false } = {}) {+ if (!store) return+ const st = store.getState()+ if (!st.user) { await publishPending({ usage: null }); return }+ // A boot without a network leaves the config unknown; ask again rather than decide on nothing.+ let cfg = st.config+ if (!cfg && typeof st.loadConfig === 'function') cfg = await st.loadConfig()+ if (!cfg?.media) { await publishPending({ usage: null }); return }+ const files = referencedFiles(st.S)+ const refs = files.map(f => f.hash)+ if (!refs.length) { await publishPending({ deferred: 0, rejected: 0, unavailable: 0 }); return }+ const key = refs.join(',')+ if (!force && lastRun.complete && lastRun.refs === key && d.now() - lastRun.at < DEDUPE_MS) return+ if (!force && d.now() < retryNotBefore) return+ if (retryRejected) { missBackoff.clear(); rejectTold.clear() }++ let missing = []+ let usage = null+ try {+ for (let i = 0; i < refs.length; i += MISSING_CHUNK) {+ const r = await d.api('/api/media/missing', { method: 'POST', body: JSON.stringify({ hashes: refs.slice(i, i + MISSING_CHUNK) }) })+ if (Array.isArray(r?.missing)) missing.push(...r.missing)+ if (r?.usage) usage = r.usage+ }+ } catch (e) {+ lastRun = { refs: key, at: d.now(), complete: false }+ if (e?.status === 429) retryNotBefore = d.now() + (Number(e.data?.retryAfter) || 60) * 1000+ return+ }+ const missingSet = new Set(missing)+ for (const h of refs) {+ if (missingSet.has(h)) continue+ missBackoff.delete(h) // the server has it: a device showing it as missing may ask again+ upBackoff.delete(h)+ await d.media.markSynced(h)+ }+ let deferred = 0, unavailable = 0, rejected = 0, skipped = 0, stopped = false+ let added = 0+ for (const h of missing) {+ const rec = await d.media.get(h)+ if (!rec) { unavailable++; continue } // nobody here has it: shown as missing until someone sends it+ if (rec.rejected && !retryRejected) { rejected++; continue }+ if (!force && rec.size > BG_UPLOAD_METERED_MAX_MB * MB && !d.allowed()) { deferred++; continue } // big files wait for Wi-Fi+ if (!force && active(upBackoff, h)) { skipped++; continue }+ try {+ const res = await d.apiUpload('/api/media/' + h, rec.blob, rec.mime)+ await d.media.markSynced(h)+ upBackoff.delete(h)+ quotaTold = false+ if (res && res.existed === false) added += rec.size+ } catch (e) {+ const code = e?.code+ if (code === 'media-quota') {+ if (!quotaTold) {+ quotaTold = true+ d.toast(t('Your photo and video space on the server is full ({0} of {1} MB).', Math.round(Number(e.data?.usedMB) || 0), Math.round(Number(e.data?.quotaMB) || 0)))+ }+ stopped = true+ break+ }+ if (e?.status === 429) {+ retryNotBefore = d.now() + (Number(e.retryAfter) || 60) * 1000+ stopped = true+ break+ }+ if (REJECTED.has(code) || e?.status === 413 || e?.status === 415) {+ await d.media.markRejected(h)+ rejected+++ if (!rejectTold.has(h)) {+ rejectTold.add(h)+ d.toast(code === 'media-type' || code === 'media-invalid'+ ? t('That file type is not supported — use a photo, a GIF, or an MP4, MOV or WebM video.')+ : code === 'media-too-long' ? t('That video is too long — up to {0} seconds.', Number(e.data?.maxSec) || 60)+ : t('The server refused the file as too large.'))+ }+ continue+ }+ // No network, a stall, a server error, a full disk, a refused session: this one waits,+ // and so does the rest of the run — the next trigger (the network coming back, the next+ // sync) tries again.+ bump(upBackoff, h, UPLOAD_BACKOFF)+ stopped = true+ break+ }+ }+ lastRun = { refs: key, at: d.now(), complete: !stopped && !deferred && !skipped }+ if (usage && added) usage = { ...usage, bytes: (usage.bytes || 0) + added }+ await publishPending({ usage, deferred, rejected, unavailable })+ }++ /** Makes the state's files and the server agree, once at a time (a second call while one runs+ * is folded into one more run after it). `force` skips the 10-minute dedupe, the upload+ * backoff and the wait for Wi-Fi; `retryRejected` also offers again what the server refused. */+ function syncMedia(opts = {}) {+ if (running) {+ again = { force: !!(again?.force || opts.force), retryRejected: !!(again?.retryRejected || opts.retryRejected) }+ return running+ }+ running = (async () => {+ try { await withLock('opengym-media-sync', () => run(opts)) }+ catch { /* best effort: the next trigger tries again */ }+ finally {+ running = null+ if (again) { const a = again; again = null; syncMedia(a) }+ }+ })()+ return running+ }++ /** Before a sign-out decides: the run in flight, then one forced run, at most 60 s. */+ async function settle() {+ if (running) await running.catch(() => {})+ await Promise.race([syncMedia({ force: true }), sleep(SETTLE_BUDGET_MS)])+ }++ /**+ * Downloads one file into the local store, checked: its sha256 must be its name and its+ * first bytes must be the type the state says (`expect.mime`), and an answer longer than the+ * state says (`expect.size`) is not even read. Resolves true once it is stored. Rejects with+ * code 'missing' (the server does not have it — the per-hash backoff then keeps devices from+ * asking every render), 'offline', 'integrity' or 'unavailable' (no server to ask). `force`+ * asks past that backoff — a tap on a tile that failed.+ */+ function fetchToStore(hash, expect = {}, { force = false } = {}) {+ if (fetching.has(hash)) return fetching.get(hash)+ const job = (async () => {+ if (!force && active(missBackoff, hash)) throw Object.assign(new Error('missing'), { code: 'missing' })+ let blob+ try { blob = await d.apiBlob('/api/media/' + hash, { expectSize: typeof expect.size === 'number' ? expect.size : undefined }) }+ catch (e) {+ if (e?.status === 404) { bump(missBackoff, hash, MISSING_BACKOFF); throw Object.assign(new Error('missing'), { code: 'missing' }) }+ if (e?.code === 'not-paired' || e?.status === 401) throw Object.assign(new Error('unavailable'), { code: 'unavailable' })+ if (isNetwork(e)) throw Object.assign(new Error('offline'), { code: 'offline' })+ throw Object.assign(new Error('integrity'), { code: 'integrity' })+ }+ const bytes = new Uint8Array(await blob.arrayBuffer())+ const sniffed = sniffKind(bytes.subarray(0, 64))+ const mime = sniffed?.mime+ if (!mime || !MEDIA_MIMES[mime] || (expect.mime && mime !== expect.mime) || (await d.sha256(bytes)) !== hash) {+ throw Object.assign(new Error('integrity'), { code: 'integrity' })+ }+ await d.media.put(hash, new Blob([bytes], { type: mime }), { mime, pending: false })+ missBackoff.delete(hash)+ return true+ })()+ fetching.set(hash, job)+ job.catch(() => {}).finally(() => fetching.delete(hash))+ return job+ }+ /** Whether a download of `hash` would be tried now (not inside a 'missing' backoff). */+ const mayFetch = hash => !active(missBackoff, hash)++ /**+ * Deletes the local files nothing needs any more. Live = what the state in memory references,+ * what the saved copy references (another tab may be on an older or newer one), what any stash+ * references, and anything put in the last hour (a file picked in an editor that is still+ * open). Signed in, the store is then kept under LOCAL_CACHE_MAX_MB by evicting main files+ * that are safely on the server, least recently shown first; posters the state references and+ * pending files are never evicted, and a guest's or a local phone's referenced files are never+ * evicted at all — for them this is the only copy.+ */+ async function localMediaGc() {+ if (!store) return+ await withLock('opengym-media', async () => {+ const st = store.getState()+ const live = referencedHashes(st.S)+ try {+ const raw = d.storage?.getItem(PERSISTED_KEY)+ if (raw) for (const h of referencedHashes(JSON.parse(raw))) live.add(h)+ } catch { /* an unreadable copy keeps nothing extra */ }+ try {+ if (typeof st.stashedMediaHashes === 'function') for (const h of await st.stashedMediaHashes()) live.add(h)+ } catch { return } // the stash could not be read: delete nothing rather than guess+ const cutoff = d.now() - LOCAL_GC_GRACE_MS+ const all = await d.media.list()+ for (const r of all) if (!live.has(r.hash) && (r.putAt || 0) < cutoff) await d.media.remove(r.hash)+ if (!st.user) return+ const left = (await d.media.list()).filter(r => live.has(r.hash) || (r.putAt || 0) >= cutoff)+ let bytes = left.reduce((n, r) => n + (r.size || 0), 0)+ if (bytes <= LOCAL_CACHE_MAX_MB * MB) return+ const refs = referencedFiles(st.S)+ const posters = new Set(refs.filter(f => f.poster).map(f => f.hash))+ const inState = new Set(refs.map(f => f.hash))+ const candidates = left+ .filter(r => !r.pending && !posters.has(r.hash) && (r.putAt || 0) < cutoff)+ .sort((a, b) => (inState.has(a.hash) - inState.has(b.hash)) || ((a.shownAt || 0) - (b.shownAt || 0)))+ for (const r of candidates) {+ if (bytes <= LOCAL_CACHE_MAX_MB * MB) break+ await d.media.remove(r.hash)+ bytes -= r.size || 0+ }+ })+ }++ /**+ * Makes the posters and files of every custom exercise in the plan and in the session in+ * progress local, so they show without a network. Signed in only (nothing to fetch from+ * otherwise), and only on a connection that is not metered (prefetchAllowed).+ */+ function startCustomMediaPrefetch({ delay = 8000 } = {}) {+ if (!store) return () => {}+ let timer = null+ let busy = false+ const want = () => {+ const S = store.getState().S+ const ids = new Set()+ ;(S?.routines || []).forEach(r => (r?.ex || []).forEach(e => { if (e?.id) ids.add(e.id) }))+ ;(S?.active?.entries || []).forEach(en => { if (en?.id) ids.add(en.id) })+ const used = { customEx: (S?.customEx || []).filter(c => c && ids.has(c.id)) }+ return referencedFiles(used)+ }+ const runPrefetch = async () => {+ timer = null+ const st = store.getState()+ if (busy || !st.user || !st.config?.media || !d.allowed()) return+ busy = true+ try {+ for (const f of want()) {+ if (!d.allowed()) break+ if (await d.media.has(f.hash) || !mayFetch(f.hash)) continue+ try { await fetchToStore(f.hash, f) } catch (e) { if (e?.code === 'offline' || e?.code === 'unavailable') break }+ }+ } finally { busy = false }+ }+ const schedule = () => { clearTimeout(timer); timer = setTimeout(runPrefetch, delay) }+ let seen = store.getState().S+ const unsub = store.subscribe(s => {+ if (s.S?.routines === seen?.routines && s.S?.active === seen?.active && s.S?.customEx === seen?.customEx) return+ seen = s.S+ schedule()+ })+ globalThis.addEventListener?.('online', schedule)+ schedule()+ return () => { unsub(); clearTimeout(timer); globalThis.removeEventListener?.('online', schedule) }+ }++ /**+ * Wires it all to the app store: a run 2 s after the referenced files change, a sync lands+ * (sync.lastSynced moves) or the account changes; a forced one when the network comes back;+ * the local clean-up once the app is idle and 10 s after the references change. The references+ * are compared as a joined string, because every update clones the state and S.customEx is a+ * new array each time.+ */+ function start(appStore) {+ store = appStore+ loadPending()+ registerMediaRunner({ settle })+ let timer = null+ let pendingForce = false+ const schedule = (ms, force = false) => {+ pendingForce = pendingForce || force+ clearTimeout(timer)+ timer = setTimeout(() => { const f = pendingForce; pendingForce = false; timer = null; syncMedia({ force: f }) }, ms)+ }+ let gcTimer = null+ const scheduleGc = ms => { clearTimeout(gcTimer); gcTimer = setTimeout(() => { gcTimer = null; localMediaGc().catch(() => {}) }, ms) }+ const refsKey = S => [...referencedHashes(S)].sort().join(',')+ let prev = store.getState()+ let prevRefs = refsKey(prev.S)+ const unsub = store.subscribe(s => {+ const refs = s.S === prev.S ? prevRefs : refsKey(s.S)+ const moved = refs !== prevRefs+ if (moved || s.sync?.lastSynced !== prev.sync?.lastSynced || s.user?.id !== prev.user?.id) schedule(2000)+ if (moved) scheduleGc(10000)+ prev = s+ prevRefs = refs+ })+ const online = () => schedule(500, true)+ globalThis.addEventListener?.('online', online)+ const idle = cb => (typeof globalThis.requestIdleCallback === 'function' ? globalThis.requestIdleCallback(cb, { timeout: 30000 }) : setTimeout(cb, 5000))+ idle(() => localMediaGc().catch(() => {}))+ schedule(3000)+ const stopPrefetch = startCustomMediaPrefetch()+ return () => { unsub(); stopPrefetch(); clearTimeout(timer); clearTimeout(gcTimer); globalThis.removeEventListener?.('online', online) }+ }++ return { syncMedia, settle, fetchToStore, mayFetch, localMediaGc, startCustomMediaPrefetch, start, _setStore: s => { store = s } }+}++// The app's own instance, made on first use so importing this module costs nothing.+let shared = null+const inst = () => (shared || (shared = createMediaSync()))+export const startMediaSync = appStore => inst().start(appStore)+export const syncMedia = opts => inst().syncMedia(opts)+export const fetchToStore = (hash, expect, opts) => inst().fetchToStore(hash, expect, opts)+export const mayFetch = hash => inst().mayFetch(hash)+export const localMediaGc = () => inst().localMediaGc()+export const settleMediaSync = () => inst().settle()+/** Tests only. */+export const _setSharedMediaSync = s => { shared = s }diff --git a/frontend/src/lib/media-sync.test.js b/frontend/src/lib/media-sync.test.jsnew file mode 100644index 000000000..ab502a108--- /dev/null+++ b/frontend/src/lib/media-sync.test.js@@ -0,0 +1,275 @@+// @vitest-environment happy-dom+import { describe, it, expect, vi, beforeEach } from 'vitest'+import { createMediaSync } from './media-sync.js'+import { createMediaStore, memoryBackend } from './media-store.js'+import { sha256Hex } from './sha256.js'+import { _resetMediaOwed, getMediaStatus } from './media-owed.js'+import { jpeg, png } from './media-samples.test-util.js'++const MB = 1024 * 1024+const hex = c => c.repeat(64)+const refOf = (hash, poster, over = {}) => ({ kind: 'image', hash, mime: 'image/webp', size: 1000, width: 800, height: 600, ...(poster ? { poster: { hash: poster, mime: 'image/webp', size: 100, width: 480, height: 360 } } : {}), at: 1, ...over })++function appStore(init) {+ let st = { user: { id: 'u1' }, config: { media: { imageMB: 2, gifMB: 8, videoMB: 40, videoSec: 60, quotaMB: 200 } }, sync: { lastSynced: 0 }, S: { customEx: [] }, stashedMediaHashes: async () => new Set(), ...init }+ const subs = new Set()+ return {+ getState: () => st,+ setState: patch => { st = { ...st, ...patch }; for (const f of subs) f(st) },+ subscribe: f => { subs.add(f); return () => subs.delete(f) }+ }+}++let clock, media, toast, api, apiUpload, apiBlob, allowed, storage+const make = (store, over = {}) => createMediaSync({ store, media, api, apiUpload, apiBlob, toast, allowed: () => allowed, now: () => clock, locks: null, storage, ...over })+const put = (hash, size = 10, mime = 'image/webp') => media.put(hash, new Blob([new Uint8Array(size)]), { mime, pending: true })++beforeEach(() => {+ _resetMediaOwed()+ clock = 10_000_000+ media = createMediaStore(memoryBackend(), { now: () => clock })+ toast = vi.fn()+ allowed = true+ api = vi.fn(async (path, { body }) => ({ missing: JSON.parse(body).hashes, usage: { bytes: 0, count: 0, quotaBytes: 200 * MB } }))+ apiUpload = vi.fn(async () => ({ ok: true, existed: false }))+ apiBlob = vi.fn()+ const mem = new Map()+ storage = { getItem: k => (mem.has(k) ? mem.get(k) : null), setItem: (k, v) => mem.set(k, v) }+})++describe('syncMedia', () => {+ it('asks /missing about every referenced file, posters first, and sends only what this device holds', async () => {+ const S = { customEx: [{ id: 'a', media: refOf(hex('a'), hex('b')) }, { id: 'c', media: refOf(hex('c'), hex('d')) }] }+ await put(hex('a')); await put(hex('b')); await put(hex('d'))+ const sync = make(appStore({ S }))+ await sync.syncMedia()+ expect(JSON.parse(api.mock.calls[0][1].body).hashes).toEqual([hex('b'), hex('d'), hex('a'), hex('c')])+ expect(apiUpload.mock.calls.map(c => c[0])).toEqual([hex('b'), hex('d'), hex('a')].map(h => '/api/media/' + h))+ expect(apiUpload.mock.calls[0][2]).toBe('image/webp')+ expect([...media.pendingNow()]).toEqual([])+ expect(getMediaStatus()).toMatchObject({ unavailable: 1, pending: 0 })+ })++ it('marks what the server already has as synced without sending it', async () => {+ const S = { customEx: [{ id: 'a', media: refOf(hex('a')) }] }+ await put(hex('a'))+ api.mockResolvedValueOnce({ missing: [], usage: { bytes: 10, count: 1, quotaBytes: 0 } })+ await make(appStore({ S })).syncMedia()+ expect(apiUpload).not.toHaveBeenCalled()+ expect([...media.pendingNow()]).toEqual([])+ })++ it('does nothing for a guest, or signed in to a server without media', async () => {+ const S = { customEx: [{ id: 'a', media: refOf(hex('a')) }] }+ await put(hex('a'))+ await make(appStore({ S, user: null })).syncMedia()+ await make(appStore({ S, config: { invite_only: false } })).syncMedia()+ expect(api).not.toHaveBeenCalled()+ expect([...media.pendingNow()]).toEqual([hex('a')])+ })++ it('asks for the config when the start had none (an offline boot)', async () => {+ const S = { customEx: [{ id: 'a', media: refOf(hex('a')) }] }+ await put(hex('a'))+ const loadConfig = vi.fn(async () => ({ media: { imageMB: 2 } }))+ await make(appStore({ S, config: null, loadConfig })).syncMedia()+ expect(loadConfig).toHaveBeenCalledTimes(1)+ expect(apiUpload).toHaveBeenCalledTimes(1)+ })++ it('stops on a full quota and says so once per streak', async () => {+ const S = { customEx: [{ id: 'a', media: refOf(hex('a')) }, { id: 'c', media: refOf(hex('c')) }] }+ await put(hex('a')); await put(hex('c'))+ apiUpload.mockRejectedValue(Object.assign(new Error('full'), { status: 413, code: 'media-quota', data: { usedMB: 199.6, quotaMB: 200 } }))+ const sync = make(appStore({ S }))+ await sync.syncMedia()+ await sync.syncMedia({ force: true })+ expect(apiUpload).toHaveBeenCalledTimes(2) // one per run: the run stops at the first+ expect(toast).toHaveBeenCalledTimes(1)+ expect(toast.mock.calls[0][0]).toContain('200')+ apiUpload.mockResolvedValue({ ok: true })+ await sync.syncMedia({ force: true })+ apiUpload.mockRejectedValue(Object.assign(new Error('full'), { status: 413, code: 'media-quota', data: {} }))+ await put(hex('e'))+ await sync.syncMedia({ force: true })+ expect([...media.pendingNow()]).toEqual([hex('e')])+ })++ it('a network stop waits out its backoff, but the network coming back (a forced run) tries at once', async () => {+ const S = { customEx: [{ id: 'a', media: refOf(hex('a')) }] }+ await put(hex('a'))+ apiUpload.mockRejectedValueOnce(Object.assign(new Error('network'), { code: 'network' }))+ const sync = make(appStore({ S }))+ await sync.syncMedia()+ await sync.syncMedia() // within the backoff and the dedupe+ expect(apiUpload).toHaveBeenCalledTimes(1)+ await sync.syncMedia({ force: true }) // what 'online' does+ expect(apiUpload).toHaveBeenCalledTimes(2)+ expect([...media.pendingNow()]).toEqual([])+ })++ it('a finished run is not repeated for ten minutes unless something changes', async () => {+ const S = { customEx: [{ id: 'a', media: refOf(hex('a')) }] }+ await put(hex('a'))+ const sync = make(appStore({ S }))+ await sync.syncMedia()+ await sync.syncMedia()+ expect(api).toHaveBeenCalledTimes(1)+ clock += 11 * 60000+ await sync.syncMedia()+ expect(api).toHaveBeenCalledTimes(2)+ })++ it('a 429 is honoured for as long as it says', async () => {+ const S = { customEx: [{ id: 'a', media: refOf(hex('a')) }] }+ await put(hex('a'))+ apiUpload.mockRejectedValueOnce(Object.assign(new Error('locked'), { status: 429, code: 'locked', retryAfter: 120 }))+ const sync = make(appStore({ S }))+ await sync.syncMedia()+ clock += 60000+ await sync.syncMedia()+ expect(api).toHaveBeenCalledTimes(1)+ clock += 61000+ await sync.syncMedia()+ expect(apiUpload).toHaveBeenCalledTimes(2)+ })++ it('a file the server refuses for good stays here, is said once, and is not offered again', async () => {+ const S = { customEx: [{ id: 'a', media: refOf(hex('a')) }] }+ await put(hex('a'))+ apiUpload.mockRejectedValue(Object.assign(new Error('HTTP 413'), { status: 413, code: 'proxy-too-large' }))+ const sync = make(appStore({ S }))+ await sync.syncMedia({ force: true })+ await sync.syncMedia({ force: true })+ expect(apiUpload).toHaveBeenCalledTimes(1)+ expect(toast).toHaveBeenCalledTimes(1)+ expect((await media.get(hex('a')))).toMatchObject({ pending: true, rejected: true })+ // The Settings row asks for it again.+ apiUpload.mockResolvedValue({ ok: true })+ await sync.syncMedia({ force: true, retryRejected: true })+ expect((await media.get(hex('a')))).toMatchObject({ pending: false, rejected: false })+ })++ it('a big file waits for a connection that is not metered, unless the run is forced', async () => {+ const S = { customEx: [{ id: 'v', media: refOf(hex('a'), null, { kind: 'video', mime: 'video/mp4', size: 6 * MB }) }] }+ await put(hex('a'), 6 * MB, 'video/mp4')+ allowed = false+ const sync = make(appStore({ S }))+ await sync.syncMedia()+ expect(apiUpload).not.toHaveBeenCalled()+ expect(getMediaStatus().deferred).toBe(1)+ await sync.syncMedia({ force: true })+ expect(apiUpload).toHaveBeenCalledTimes(1)+ })++ it('runs one at a time and folds what is asked meanwhile into one more run', async () => {+ const S = { customEx: [{ id: 'a', media: refOf(hex('a')) }] }+ await put(hex('a'))+ let release+ apiUpload.mockImplementationOnce(() => new Promise(r => { release = r }))+ const sync = make(appStore({ S }))+ const first = sync.syncMedia()+ await new Promise(r => setTimeout(r, 5))+ sync.syncMedia({ force: true }); sync.syncMedia({ force: true })+ release({ ok: true })+ await first+ await new Promise(r => setTimeout(r, 10))+ expect(api).toHaveBeenCalledTimes(2)+ })+})++describe('fetchToStore', () => {+ const blobOf = b => new Blob([b])+ it('keeps a download whose hash and type match, as synced', async () => {+ const file = jpeg()+ const h = await sha256Hex(file)+ apiBlob.mockResolvedValue(blobOf(file))+ const sync = make(appStore({}))+ await sync.fetchToStore(h, { mime: 'image/jpeg', size: file.length })+ expect(apiBlob.mock.calls[0]).toEqual(['/api/media/' + h, { expectSize: file.length }])+ expect(await media.get(h)).toMatchObject({ mime: 'image/jpeg', pending: false })+ })++ it('refuses bytes that do not hash to the name, a login page, and the wrong type', async () => {+ const file = jpeg()+ const h = await sha256Hex(file)+ const sync = make(appStore({}))+ apiBlob.mockResolvedValueOnce(blobOf(png()))+ await expect(sync.fetchToStore(h, { mime: 'image/jpeg' })).rejects.toMatchObject({ code: 'integrity' })+ const page = new TextEncoder().encode('<!doctype html><title>Sign in</title>')+ apiBlob.mockResolvedValueOnce(blobOf(page))+ await expect(sync.fetchToStore(await sha256Hex(page), {})).rejects.toMatchObject({ code: 'integrity' })+ apiBlob.mockResolvedValueOnce(blobOf(file))+ await expect(sync.fetchToStore(h, { mime: 'image/png' })).rejects.toMatchObject({ code: 'integrity' })+ expect(await media.list()).toEqual([])+ })++ it('a 404 backs off per file — 30 s, then 2 min — and a /missing answer that has it lifts that', async () => {+ const h = hex('a')+ apiBlob.mockRejectedValue(Object.assign(new Error('no'), { status: 404, code: 'media-missing' }))+ const sync = make(appStore({ S: { customEx: [{ id: 'a', media: refOf(h) }] } }))+ await expect(sync.fetchToStore(h, {})).rejects.toMatchObject({ code: 'missing' })+ expect(sync.mayFetch(h)).toBe(false)+ await expect(sync.fetchToStore(h, {})).rejects.toMatchObject({ code: 'missing' })+ expect(apiBlob).toHaveBeenCalledTimes(1)+ clock += 31000+ await expect(sync.fetchToStore(h, {})).rejects.toMatchObject({ code: 'missing' })+ expect(apiBlob).toHaveBeenCalledTimes(2)+ clock += 31000+ expect(sync.mayFetch(h)).toBe(false) // now two minutes+ // A tap asks past it.+ await expect(sync.fetchToStore(h, {}, { force: true })).rejects.toMatchObject({ code: 'missing' })+ expect(apiBlob).toHaveBeenCalledTimes(3)+ // Another device uploaded it: the server's answer to /missing says so.+ api.mockResolvedValueOnce({ missing: [], usage: null })+ await sync.syncMedia({ force: true })+ expect(sync.mayFetch(h)).toBe(true)+ })++ it('no network is offline, not missing', async () => {+ apiBlob.mockRejectedValue(new TypeError('Failed to fetch'))+ await expect(make(appStore({})).fetchToStore(hex('a'), {})).rejects.toMatchObject({ code: 'offline' })+ })+})++describe('localMediaGc', () => {+ it('keeps what the state, the saved copy, a stash or the last hour refer to, and nothing else', async () => {+ for (const c of 'abcdef') await put(hex(c))+ clock += 2 * 3600000+ await put(hex('9')) // picked a moment ago+ const S = { customEx: [{ id: 'x', media: refOf(hex('a'), hex('b')) }] }+ storage.setItem('gym_state_v1', JSON.stringify({ customEx: [{ id: 'y', media: refOf(hex('c')) }] })) // another tab's copy+ const store = appStore({ S, user: null, stashedMediaHashes: async () => new Set([hex('d')]) })+ await make(store).localMediaGc()+ expect((await media.list()).map(r => r.hash).sort()).toEqual([hex('a'), hex('b'), hex('c'), hex('d'), hex('9')].sort())+ })++ it('deletes nothing when the stash cannot be read', async () => {+ await put(hex('a'))+ clock += 2 * 3600000+ await make(appStore({ S: { customEx: [] }, stashedMediaHashes: async () => { throw new Error('io') } })).localMediaGc()+ expect(await media.has(hex('a'))).toBe(true)+ })++ it('signed in, evicts synced main files over the cap — never a pending one or a poster the state shows', async () => {+ const S = { customEx: [+ { id: 'x', media: refOf(hex('a'), hex('b'), { size: 200 * MB }) },+ { id: 'y', media: refOf(hex('c'), hex('d'), { size: 150 * MB }) }+ ] }+ await media.put(hex('a'), new Blob([new Uint8Array(200 * MB)]), { mime: 'image/webp', pending: false })+ await media.put(hex('b'), new Blob([new Uint8Array(1 * MB)]), { mime: 'image/webp', pending: false })+ await media.put(hex('c'), new Blob([new Uint8Array(150 * MB)]), { mime: 'image/webp', pending: true })+ await media.put(hex('d'), new Blob([new Uint8Array(1 * MB)]), { mime: 'image/webp', pending: false })+ clock += 2 * 3600000+ await make(appStore({ S })).localMediaGc()+ expect((await media.list()).map(r => r.hash).sort()).toEqual([hex('b'), hex('c'), hex('d')])+ })++ it('a guest never loses a referenced file to the cap', async () => {+ const S = { customEx: [{ id: 'x', media: refOf(hex('a'), null, { size: 400 * MB }) }] }+ await media.put(hex('a'), new Blob([new Uint8Array(400 * MB)]), { mime: 'image/webp', pending: false })+ clock += 2 * 3600000+ await make(appStore({ S, user: null })).localMediaGc()+ expect(await media.has(hex('a'))).toBe(true)+ })+})diff --git a/frontend/src/lib/mobile.js b/frontend/src/lib/mobile.jsindex 790709569..8e49164c0 100644--- a/frontend/src/lib/mobile.js+++ b/frontend/src/lib/mobile.js@@ -182,6 +182,24 @@ export async function shareExport(json, filename) {await Share.share({ title: filename, url: w.uri })}+// The same for a binary export — the backup with photos and videos (lib/backup-media.js). The+// file crosses the native bridge as base64 in 3 MB pieces, never as one string the size of the+// whole zip, which could be a few hundred MB.+export async function shareExportBlob(blob, filename) {+ const { Filesystem, Directory } = await import('@capacitor/filesystem')+ const { Share } = await import('@capacitor/share')+ const { toBase64 } = await import('./media-store-fs.js')+ const CHUNK = 3 * 1024 * 1024+ for (let o = 0; o < blob.size || o === 0; o += CHUNK) {+ const data = toBase64(new Uint8Array(await blob.slice(o, o + CHUNK).arrayBuffer()))+ if (o === 0) await Filesystem.writeFile({ path: filename, directory: Directory.Cache, data })+ else await Filesystem.appendFile({ path: filename, directory: Directory.Cache, data })+ if (!blob.size) break+ }+ const { uri } = await Filesystem.getUri({ path: filename, directory: Directory.Cache })+ await Share.share({ title: filename, url: uri })+}+// Hand a self-contained HTML document (lib/plan-share.js planPrintHTML) to the OS print flow.// Android routes it through the system PrintManager — "Save as PDF", "Save to Drive", a real// printer; iOS through the print sheet — "Save to Files" (as PDF), share, print. Either way thediff --git a/frontend/src/lib/sync-merge.js b/frontend/src/lib/sync-merge.jsindex 55196f2d5..33a13069d 100644--- a/frontend/src/lib/sync-merge.js+++ b/frontend/src/lib/sync-merge.js@@ -8,8 +8,10 @@** Rules, by field:* - scalars and settings, `week`, `dayPlan`, `wc`, `reminder`, …: from the copy with the newer `_ts`- * - customEx, equipProfiles, gymCards: union by id, the newer copy's version of an id that- * both have+ * - equipProfiles, gymCards: union by id, the newer copy's version of an id that both have+ * - customEx: union by id; of an id that both have, the version edited last by its own `_ts`+ * (stampCustomEx), the newer copy's on a tie — a photo or link added on one device must not+ * be lost to the other's copy just because that one logged a set since* - workouts: union by id; of an id that both have, the version edited last by its own `_ts`* (stampWorkout — a workout changed after it was logged: its sets edited, moved to another* day, its length or note corrected), the newer copy's on a tie; sorted by day and start@@ -181,6 +183,17 @@ export function mergeStates(a, b, { prefer } = {}) {return alt && (alt._ts || 0) > (r._ts || 0) ? clone(alt) : r})}+ // A custom exercise edited on both sides keeps the version edited last, the same rule and for+ // the same reason. The merge is whole-entry: a device that later renames an exercise whose+ // media it never saw change brings its old media back (the old file outlives the grace period+ // on the server, so nothing breaks, it is only the older picture).+ if (!prefer && out.customEx) {+ const other = new Map(list(o.customEx).filter(c => c?.id != null).map(c => [c.id, c]))+ out.customEx = out.customEx.map(c => {+ const alt = c?.id != null && other.get(c.id)+ return alt && (alt._ts || 0) > (c._ts || 0) ? clone(alt) : c+ })+ }out.bodyweight = mergeBodyweight(n.bodyweight, o.bodyweight).map(clone)if (list(n.favEx).length || list(o.favEx).length) out.favEx = [...new Set([...list(n.favEx), ...list(o.favEx)])]out.exWeights = clone(mergeExWeights(n.exWeights, o.exWeights))@@ -220,6 +233,24 @@ export function stampRoutines(prev = [], next = [], now = Date.now()) {return next}+const sameEntry = (a, b) => JSON.stringify({ ...a, _ts: 0 }) === JSON.stringify({ ...b, _ts: 0 })++/**+ * Stamps `_ts` on every custom exercise of `next` that is new or differs from its version in+ * `prev`, the way stampRoutines does for a plan — the edit time mergeStates needs to keep the+ * version edited last. The store runs it on every change (useStore update). Mutates and returns+ * `next`.+ */+export function stampCustomEx(prev = [], next = [], now = Date.now()) {+ const before = new Map(list(prev).filter(c => c?.id != null).map(c => [c.id, c]))+ for (const c of list(next)) {+ if (!c || typeof c !== 'object' || c.id == null) continue+ const old = before.get(c.id)+ if (!old || (old !== c && !sameEntry(old, c))) c._ts = now+ }+ return next+}+// What `local` holds that `server` does not: the workouts and weigh-ins a device logged while it// was signed out, and the custom exercises they use. Sign-in asks about these before the server's// profile replaces the local copy; zero of each means there is nothing to ask about.diff --git a/frontend/src/lib/sync-merge.test.js b/frontend/src/lib/sync-merge.test.jsindex 4fe671fe0..8385f6fd0 100644--- a/frontend/src/lib/sync-merge.test.js+++ b/frontend/src/lib/sync-merge.test.js@@ -1,5 +1,5 @@import { describe, expect, it } from 'vitest'-import { localExtras, mergeBodyweight, mergeStampedMap, mergeStates, newerOf, stampRoutines, stampWorkout, unionById } from './sync-merge.js'+import { localExtras, mergeBodyweight, mergeStampedMap, mergeStates, newerOf, stampCustomEx, stampRoutines, stampWorkout, unionById } from './sync-merge.js'import { retimeWorkout } from './workout-date.js'import { inventoryFor, loadKindFor, withLoadKind, withPlatePairs, withStandardPlates } from './plates.js'@@ -412,3 +412,46 @@ describe('plate loading keeps the choice made last', () => {expect(mergeStates(base({ _ts: 500 }), base({ _ts: 1 })).plates).toBeUndefined()})})++describe('custom exercises keep the version edited last', () => {+ const photo = hash => ({ kind: 'image', hash: hash.repeat(64), mime: 'image/webp', size: 10, width: 4, height: 3, at: 1 })+ const cx = (over = {}) => ({ id: 'c1', n: 'sandbag carry', bp: 'back', custom: true, ...over })++ it('a photo added on A survives a merge with B, which is newer as a whole', () => {+ const A = base({ _ts: 100, customEx: [cx({ media: photo('a'), _ts: 90 })] })+ const B = base({ _ts: 200, customEx: [cx({ _ts: 10 })], workouts: [workout('wB')] })+ const out = mergeStates(A, B)+ expect(out.customEx[0].media.hash).toBe('a'.repeat(64))+ expect(ids(out.workouts)).toEqual(['wB'])+ expect(mergeStates(B, A).customEx[0].media.hash).toBe('a'.repeat(64))+ })++ it('on a tie, or without stamps, the newer copy\'s version stays', () => {+ const A = base({ _ts: 100, customEx: [cx({ url: 'https://a.example/', _ts: 7 })] })+ const B = base({ _ts: 200, customEx: [cx({ url: 'https://b.example/', _ts: 7 })] })+ expect(mergeStates(A, B).customEx[0].url).toBe('https://b.example/')+ const C = base({ _ts: 100, customEx: [cx({ url: 'https://a.example/' })] })+ const D = base({ _ts: 200, customEx: [cx({ url: 'https://b.example/' })] })+ expect(mergeStates(C, D).customEx[0].url).toBe('https://b.example/')+ })++ it('sign-in (prefer) keeps the preferred side\'s version whatever the stamps say', () => {+ const server = base({ _ts: 100, customEx: [cx({ _ts: 1 })] })+ const device = base({ _ts: 50, customEx: [cx({ media: photo('d'), _ts: 99 })] })+ expect(mergeStates(server, device, { prefer: 'a' }).customEx[0].media).toBeUndefined()+ })++ it('stampCustomEx stamps a new or edited exercise and leaves the rest alone', () => {+ const prev = [cx({ _ts: 5 }), cx({ id: 'c2', n: 'b', _ts: 5 })]+ const next = JSON.parse(JSON.stringify(prev))+ next[0].media = photo('e')+ next.push(cx({ id: 'c3', n: 'new' }))+ stampCustomEx(prev, next, 1000)+ expect(next.map(c => c._ts)).toEqual([1000, 5, 1000])+ // Only the stamp differing is no edit.+ const again = JSON.parse(JSON.stringify(next))+ again[1]._ts = 6+ stampCustomEx(next, again, 2000)+ expect(again.map(c => c._ts)).toEqual([1000, 6, 1000])+ })+})diff --git a/frontend/src/main.jsx b/frontend/src/main.jsxindex f57590454..f345e54bc 100644--- a/frontend/src/main.jsx+++ b/frontend/src/main.jsx@@ -3,6 +3,7 @@ import { createRoot } from 'react-dom/client'import App from './App.jsx'import { MOBILE } from './lib/mobile.js'import { useStore } from './store/useStore.js'+import { startMediaSync } from './lib/media-sync.js'import './index.css'// App.jsx restores per-route scroll itself; the browser's own attempt races it.@@ -12,6 +13,10 @@ createRoot(document.getElementById('root')).render(<StrictMode><App /></StrictMode>)+// The photos and videos of custom exercises, in every build (the phone and the demo included):+// uploads of what the server lacks, the local clean-up, and the plan's files kept offline.+startMediaSync(useStore)+// Not in the mobile build: the native shell already serves everything from disk.if (!MOBILE && 'serviceWorker' in navigator && location.protocol === 'https:') {navigator.serviceWorker.register('sw.js').catch(() => {})diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.jsindex bee13a7a6..48668c174 100644--- a/frontend/src/store/useStore.js+++ b/frontend/src/store/useStore.js@@ -6,7 +6,10 @@ import { registerCustom } from '../lib/exercises.js'import { DEMO, DEMO_SEEDED } from '../lib/demo.js'import { guestAllowed } from '../lib/guest.js'import { MOBILE, initReminderSync, nativeLoad, nativeSave, onAppActive, readJsonFile, syncReminder, writeAutoBackup, writeJsonFile } from '../lib/mobile.js'-import { mergeStates, localExtras, stampRoutines } from '../lib/sync-merge.js'+import { mergeStates, localExtras, stampRoutines, stampCustomEx } from '../lib/sync-merge.js'+import { pendingRefCount, settleMedia, loadPending } from '../lib/media-owed.js'+import { referencedHashes } from '../lib/media-refs.js'+import { mediaStore } from '../lib/media-store.js'import { countChanges, syncFingerprint } from '../lib/sync-changes.js'import { saveWorkoutEdit } from '../lib/session-edit.js'import { appBase } from '../lib/app-base.js'@@ -545,11 +548,14 @@ export const useStore = create((set, get) => {})// Before a sign-out decides anything: the pull on its way has landed, and every push asked- // for — including one queued behind the push in flight — has run.+ // for — including one queued behind the push in flight — has run. Then the photos and videos+ // still waiting get one more try (lib/media-owed.js, at most a minute): they are owed exactly+ // like a change is.const settle = async () => {if (pulling) await pullingawait get().pushState() // never throws — an owed copy stays owed when it does not landwhile (pushing) await pushing+ await settleMedia(get().S)}// Forget where this device stood with the server — a different account, a sign-out. The copy@@ -563,6 +569,7 @@ export const useStore = create((set, get) => {// Everything a sign-out leaves behind on this device, whichever way it was triggered. The owner// goes last, after the wiped copy is written — the storage listener above relies on the order.const clearLocalSession = () => {+ const hadMedia = referencedHashes(get().S).size > 0get().setUser(null)localStorage.removeItem('gym_guest')forgetSync()@@ -574,7 +581,20 @@ export const useStore = create((set, get) => {// On a phone the file is wiped with it, now rather than after the usual wait: a start in// between would open in local mode on the signed-out account's data, since that boot takes// the file whenever storage holds nothing.- return MOBILE ? nativePersist(true) : null+ const wiped = MOBILE ? nativePersist(true) : null+ // The account's photos and videos go with its copy (a shared device keeps nothing of it),+ // except the ones a stash still refers to — whatever was pending is in a stash by now+ // (signOut refuses otherwise) and comes back with it. A file picked from here on is kept.+ // After the state file, which is the one write a sign-out must not be kept waiting for. A+ // copy that referred to no media leaves the store alone; files of exercises deleted earlier+ // are the local clean-up's (lib/media-sync.js localMediaGc).+ if (hadMedia) {+ const since = Date.now()+ Promise.resolve(wiped).then(() => readStashes())+ .then(all => mediaStore.retainOnly(new Set(Object.values(all).flatMap(e => [...referencedHashes(e?.state)])), { keepPutAfter: since }))+ .catch(() => {})+ }+ return wiped}/* The changes a forced sign-out or disconnect keeps, so that no change is ever lost silently.@@ -606,9 +626,11 @@ export const useStore = create((set, get) => {// way a forced sign-out keeps it, for when that account comes back. Written to localStorage// right here, since setUser wipes the copy straight after; on a phone the file follows, and// pairing waits for it (`keeping`) before it goes on.+ // Photos or videos that never reached that account's server count as owed on their own, even on+ // a copy with no workouts or routines yet — the custom exercise they belong to is all there is.const keepForPrevious = uid => {const S = get().S- if (!owes() || !hasData(S)) return+ if (!(owes() && hasData(S)) && !pendingRefCount(S)) returnconst server = serverBase()const key = stashKey(server, uid)let all = {}@@ -632,6 +654,9 @@ export const useStore = create((set, get) => {set({ keptRev: get().keptRev + 1 })if (MOBILE) keeping = readStashes().then(f => writeStashes({ ...f, [key]: entry })).catch(() => {})}+ // What a sign-out or disconnect answers when something is owed: how many changes, and how many+ // photos or videos, when there are any.+ const owedResult = (left, extra = {}) => ({ owed: true, count: left.count, ...(left.media ? { media: left.media } : {}), ...extra })const stashOwed = async () => {const user = get().userif (!user) return true@@ -692,6 +717,9 @@ export const useStore = create((set, get) => {const S0 = loadState()registerCustom(S0.customEx)+ // Which photos and videos are still waiting for the server, known before the first sign-out+ // check has to ask (lib/media-owed.js). A copy without any leaves the media store unopened.+ if (referencedHashes(S0).size) loadPending()meta.set(S0, { base: readStoredSync(), owed: storedOwed() })const user0 = (() => { try { return JSON.parse(localStorage.getItem('gym_user')) || null } catch { return null } })()const sync0 = { offline: false, pending: storedOwed(), auth: false, lastError: null, lastSynced: (() => { try { return +localStorage.getItem(SYNCED_AT_KEY) || 0 } catch { return 0 } })(), server: serverBase() }@@ -726,6 +754,7 @@ export const useStore = create((set, get) => {const S = clone(prev)mut(S)stampRoutines(prev.routines, S.routines)+ stampCustomEx(prev.customEx, S.customEx)persist(S, push)},// An edit of a saved workout (lib/session-edit.js) is saved or dropped like any other change:@@ -895,10 +924,16 @@ export const useStore = create((set, get) => {// differ from the last copy the two agreed on (plus one for settings and the plan) — null// when the device has no record of that copy (it last agreed with the server under an older// version), and then only `owed` is known.+ //+ // `media` counts the custom exercises whose photo or video the server has not confirmed+ // (lib/media-owed.js); present only when there are any, and they are owed on their own.unsyncedChanges() {- if (!get().user || !owes()) return { owed: false, count: 0 }+ if (!get().user) return { owed: false, count: 0 }+ const media = pendingRefCount(get().S)+ const withMedia = media ? { media } : {}+ if (!owes()) return { owed: media > 0, count: 0, ...withMedia }const count = countChanges(get().S, readFingerprint())- return { owed: count !== 0, count }+ return { owed: count !== 0 || media > 0, count, ...withMedia }},// The changes a forced sign-out or disconnect kept on this device, waiting for their server@@ -910,6 +945,12 @@ export const useStore = create((set, get) => {const all = await readStashes()return Object.values(all).map(({ server, uid, name, at }) => ({ server: server || null, uid, name, at }))},+ // Every photo and video file the stashes refer to: they are kept on this device for as long+ // as the stash is (lib/media-sync.js localMediaGc, Reset everything).+ async stashedMediaHashes() {+ const all = await readStashes()+ return new Set(Object.values(all).flatMap(e => [...referencedHashes(e?.state)]))+ },// Sign-in (and pairing a phone) takes the server's profile as this device's copy — the// profile is the truth for a signed-in user, whatever the timestamps say. The only thing@@ -1000,13 +1041,13 @@ export const useStore = create((set, get) => {async signOut({ force = false } = {}) {await settle()const left = get().unsyncedChanges()- if (left.owed && !force) return { owed: true, count: left.count }- if (left.owed && !(await stashOwed())) return { owed: true, count: left.count, stashed: false }+ if (left.owed && !force) return owedResult(left)+ if (left.owed && !(await stashOwed())) return owedResult(left, { stashed: false })// The device is signed out either way. A request that did not get through leaves the cookie// behind, still valid, so the logout is owed until the server answers it (see boot).try { await api('/api/logout', { method: 'POST', body: '{}' }) } catch (e) { if (!MOBILE) logoutOwed(true) }await clearLocalSession()- return left.owed ? { owed: true, count: left.count, stashed: true } : { owed: false }+ return left.owed ? owedResult(left, { stashed: true }) : { owed: false }},// Mobile-only ("connect to my server" onboarding, see App.jsx's needsMobileOnboarding).@@ -1055,11 +1096,11 @@ export const useStore = create((set, get) => {async signOutAll({ force = false } = {}) {await settle()const left = get().unsyncedChanges()- if (left.owed && !force) return { owed: true, count: left.count }+ if (left.owed && !force) return owedResult(left)await api('/api/logout/all', { method: 'POST', body: '{}' })- if (left.owed && !(await stashOwed())) return { owed: true, count: left.count, stashed: false }+ if (left.owed && !(await stashOwed())) return owedResult(left, { stashed: false })await clearLocalSession()- return left.owed ? { owed: true, count: left.count, stashed: true } : { owed: false }+ return left.owed ? owedResult(left, { stashed: true }) : { owed: false }},// Demo build only: drop the seeded example profile back in (Settings → "Reset demo data").@@ -1068,6 +1109,8 @@ export const useStore = create((set, get) => {const { buildDemoState } = await import('../lib/demoSeed.js')markOwed(false)persist(Object.assign(clone(DEF), buildDemoState()), false)+ // The demo's photos and videos were only ever in this browser, and the reset takes them too.+ await mediaStore.clearAll().catch(() => {})},// Boot: ask the server who we are, then pull.diff --git a/frontend/src/store/useStore.media.test.jsx b/frontend/src/store/useStore.media.test.jsxnew file mode 100644index 000000000..032e8253f--- /dev/null+++ b/frontend/src/store/useStore.media.test.jsx@@ -0,0 +1,123 @@+// @vitest-environment happy-dom++/* Photos and videos of custom exercises that have not reached the server are owed exactly like a+ change that has not: a sign-out refuses while any are waiting, going ahead anyway keeps them in+ the stash with the state (and their files on this device), a different account signing in+ keeps them aside for the previous one, and the attempt before a sign-out includes them. The+ local media store is the in-memory one; lib/media-sync.js is not started, so settle() only+ reads what is pending — except in the test that registers a runner of its own. */+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'++vi.mock('../lib/api.js', () => ({ api: vi.fn(), setRemoteAuth: vi.fn() }))+const { toast } = vi.hoisted(() => ({ toast: vi.fn() }))+vi.mock('./useUI.js', () => ({ useUI: { getState: () => ({ toast }) } }))++import { api } from '../lib/api.js'+import { createMediaStore, memoryBackend, _setMediaStore } from '../lib/media-store.js'+import { _resetMediaOwed, registerMediaRunner } from '../lib/media-owed.js'+import { DEF, useStore } from './useStore.js'++const clone = value => JSON.parse(JSON.stringify(value))+const USER = { id: 'user-1', name: 'One' }+const fresh = { offline: false, pending: false, auth: false, lastError: null, lastSynced: 0, server: null }+const A = 'a'.repeat(64)+const P = 'b'.repeat(64)+const ref = { kind: 'image', hash: A, mime: 'image/webp', size: 3, width: 8, height: 6, poster: { hash: P, mime: 'image/webp', size: 3, width: 8, height: 6 }, at: 1 }+const withPhoto = () => ({ ...clone(DEF), _ts: 100, customEx: [{ id: 'c1', n: 'sandbag carry', bp: 'back', custom: true, media: clone(ref) }] })+const tick = () => new Promise(r => setTimeout(r, 0))++let media+beforeEach(async () => {+ localStorage.clear()+ localStorage.setItem('gym_owner', USER.id)+ api.mockReset(); toast.mockReset()+ _resetMediaOwed()+ media = createMediaStore(memoryBackend())+ _setMediaStore(media)+ useStore.setState({ S: clone(DEF), user: null, ready: false, sync: { ...fresh }, config: null })+})+afterEach(() => {+ localStorage.clear()+ _setMediaStore(null)+ _resetMediaOwed()+ useStore.setState({ S: clone(DEF), user: null, ready: false, sync: { ...fresh }, config: null })+})++// Signed in, with a copy the server already has (the push lands, nothing else is owed).+async function signedInWithPendingPhoto() {+ await media.put(A, new Blob(['abc']), { mime: 'image/webp', pending: true })+ await media.put(P, new Blob(['abc']), { mime: 'image/webp', pending: false })+ useStore.setState({ S: withPhoto(), user: USER, ready: true, sync: { ...fresh } })+ localStorage.setItem('gym_sync', JSON.stringify({ rev: 1, ts: 100 }))+ api.mockImplementation(async (path, o) => (o?.method === 'PUT' ? { ok: true, rev: 2 } : { ok: true }))+}++describe('owed photos and videos', () => {+ it('a sign-out refuses while one is waiting, and says it is one', async () => {+ await signedInWithPendingPhoto()+ expect(useStore.getState().unsyncedChanges()).toMatchObject({ owed: true, media: 1 })+ const r = await useStore.getState().signOut()+ expect(r).toMatchObject({ owed: true, media: 1 })+ expect(r.stashed).toBeUndefined()+ expect(useStore.getState().user).toEqual(USER)+ expect(await media.has(A)).toBe(true)+ })++ it('nothing is owed once the server has it', async () => {+ await signedInWithPendingPhoto()+ await media.markSynced(A)+ expect(useStore.getState().unsyncedChanges()).toEqual({ owed: false, count: 0 })+ expect(await useStore.getState().signOut()).toEqual({ owed: false })+ })++ it('going ahead anyway keeps the copy in the stash, and its files survive the sign-out', async () => {+ await signedInWithPendingPhoto()+ await media.put('c'.repeat(64), new Blob(['x']), { mime: 'image/png', pending: false }) // nobody's+ const r = await useStore.getState().signOut({ force: true })+ expect(r).toMatchObject({ owed: true, media: 1, stashed: true })+ expect(useStore.getState().S.customEx).toEqual([])+ const stash = JSON.parse(localStorage.getItem('gym_stash'))+ expect(Object.values(stash)[0].state.customEx[0].media.hash).toBe(A)+ expect(await useStore.getState().stashedMediaHashes()).toEqual(new Set([A, P]))+ await new Promise(r => setTimeout(r, 20)) // the purge runs after the wiped copy is written+ expect(await media.has(A)).toBe(true)+ expect(await media.has(P)).toBe(true)+ expect(await media.has('c'.repeat(64))).toBe(false)+ })++ it('a different account signing in keeps the previous one\'s waiting photo aside, even with no workouts', async () => {+ await media.put(A, new Blob(['abc']), { mime: 'image/webp', pending: true })+ // The copy owes nothing else: no workouts, no routines — only the custom exercise and its photo.+ useStore.setState({ S: withPhoto(), user: null, ready: true, sync: { ...fresh } })+ localStorage.setItem('gym_owner_name', 'One')+ useStore.getState().setUser({ id: 'user-2', name: 'Two' })+ const stash = JSON.parse(localStorage.getItem('gym_stash'))+ expect(Object.values(stash).map(e => e.uid)).toEqual([USER.id])+ expect(Object.values(stash)[0].state.customEx[0].media.hash).toBe(A)+ expect(useStore.getState().S.customEx).toEqual([])+ })++ it('the attempt before a sign-out includes the photos and videos waiting', async () => {+ await signedInWithPendingPhoto()+ const settle = vi.fn(async () => { await media.markSynced(A) })+ registerMediaRunner({ settle })+ const r = await useStore.getState().signOut()+ expect(settle).toHaveBeenCalledTimes(1)+ expect(r).toEqual({ owed: false })+ })++ it('an edit stamps the custom exercise it changed, and only that one', async () => {+ useStore.setState({ S: { ...withPhoto(), customEx: [...withPhoto().customEx, { id: 'c2', n: 'other', bp: 'legs', custom: true, _ts: 5 }] }, user: null, ready: true })+ useStore.getState().update(s => { s.customEx[0].url = 'https://example.com/guide' })+ const [c1, c2] = useStore.getState().S.customEx+ expect(c1._ts).toBeGreaterThan(5)+ expect(c2._ts).toBe(5)+ })++ it('resetting the demo takes its photos and videos with it', async () => {+ await media.put(A, new Blob(['abc']), { mime: 'image/webp', pending: true })+ await useStore.getState().resetDemo()+ await tick()+ expect(await media.list()).toEqual([])+ })+})