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: a file over the server's cap for its kind is refused before it is sent, and a declared-too-large upload is drained far enough for its 413 to arrive
Duarte Santos679fd8f9ad75
diff --git a/api/media.js b/api/media.jsindex 870a00218..100ecb286 100644--- a/api/media.js+++ b/api/media.js@@ -594,7 +594,13 @@ export function createMediaStore({ dir, limits, now = Date.now, readState = () =if (!declared) throw new MediaError(415, 'media-type');cap = capOf(declared.kind);const len = contentLength(req);- if (len != null && len > cap) throw new MediaError(413, 'media-too-large', { maxMB: capMB(declared.kind) });+ if (len != null && len > cap) {+ // Drained like a refusal before receive() (discard): up to twice the LARGEST cap. Twice+ // this kind's cap would cut a photo of 5 MB off at 4 MB with a reset, and a client that+ // hits the reset before the answer reads a network error and tries again forever.+ cap = maxCap;+ throw new MediaError(413, 'media-too-large', { maxMB: capMB(declared.kind) });+ }const have = e.hashes.get(hash);if (have) { drain(req, 2 * cap); touch(e, hash); return existed(hash, have); }diff --git a/api/test/server-media.test.js b/api/test/server-media.test.jsindex 674b6ee7a..f5f58733f 100644--- a/api/test/server-media.test.js+++ b/api/test/server-media.test.js@@ -170,6 +170,16 @@ test('a declared length over the cap is answered before the body is sent', asyncassert.deepEqual(h.tmp(U1), []);});+test('a body more than twice its kind\'s cap still gets its 413, not a reset', async t => {+ // A 2000-byte image cap and a 5000-byte photo: a phone whose own limits were bigger than this+ // server's. Draining only twice the image cap reset the connection before the answer.+ const h = await start(t, { MEDIA_IMAGE_MAX_MB: String(2000 / 1048576) });+ const r = await h.put(M.jpeg(5000));+ assert.equal(r.status, 413);+ assert.equal((await r.json()).code, 'media-too-large');+ assert.deepEqual(h.tmp(U1), []);+});+test('the cap of what the bytes are applies, and the quota says how full it is', async t => {// 10 KB images, 1 MB GIFs, a 25 KB quota.const h = await start(t, { MEDIA_IMAGE_MAX_MB: '0.009765625', MEDIA_GIF_MAX_MB: '1', MEDIA_QUOTA_MB: '0.0244140625' });diff --git a/frontend/src/lib/media-sync.js b/frontend/src/lib/media-sync.jsindex a23014f6b..cc20e8724 100644--- a/frontend/src/lib/media-sync.js+++ b/frontend/src/lib/media-sync.js@@ -24,7 +24,7 @@ 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 { MB, BG_UPLOAD_METERED_MAX_MB, LOCAL_CACHE_MAX_MB, LOCAL_GC_GRACE_MS, limitsFrom } from './media-limits.js'import { MOBILE, nativeLoad } from './mobile.js'import { registerMediaRunner, publishMediaStatus, pendingRefCount, loadPending } from './media-owed.js'import { t } from './i18n-core.js'@@ -130,10 +130,25 @@ export function createMediaSync(deps = {}) {}let deferred = 0, unavailable = 0, rejected = 0, skipped = 0, stopped = falselet added = 0+ const lim = limitsFrom(cfg)+ const capOf = mime => {+ const kind = MEDIA_MIMES[mime]?.kind+ return Math.round((kind === 'video' ? lim.videoMB : kind === 'gif' ? lim.gifMB : lim.imageMB) * MB)+ }+ const refuse = async h => {+ await d.media.markRejected(h)+ rejected+++ if (!rejectTold.has(h)) { rejectTold.add(h); d.toast(t('The server refused the file as too large.')) }+ }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 itif (rec.rejected && !retryRejected) { rejected++; continue }+ // Over this server's cap for its kind (a phone picked it under the defaults, or the caps+ // came down since): refused here, as the server would. Sent, a body more than twice the cap+ // can end in a reset rather than the 413, which reads as no network — backed off and+ // tried again for ever, and a forced run would stop at it every time.+ if (rec.size > capOf(rec.mime)) { await refuse(h); continue }if (!force && rec.size > BG_UPLOAD_METERED_MAX_MB * MB && !d.allowed()) { deferred++; continue } // big files wait for Wi-Fiif (!force && active(upBackoff, h)) { skipped++; continue }try {diff --git a/frontend/src/lib/media-sync.test.js b/frontend/src/lib/media-sync.test.jsindex 72b6cf2a2..5c39ed536 100644--- a/frontend/src/lib/media-sync.test.js+++ b/frontend/src/lib/media-sync.test.js@@ -150,6 +150,25 @@ describe('syncMedia', () => {expect((await media.get(hex('a')))).toMatchObject({ pending: false, rejected: false })})+ it('a file over this server\'s cap for its kind is refused here, never sent, and does not stop the files after it', async () => {+ // Picked on a phone under the default caps, then paired to a server that allows 1 MB photos.+ const S = { customEx: [{ id: 'a', media: refOf(hex('a'), null, { size: 3 * MB }) }, { id: 'b', media: refOf(hex('b')) }] }+ await put(hex('a'), 3 * MB); await put(hex('b'))+ const store = appStore({ S, config: { media: { imageMB: 1, gifMB: 8, videoMB: 40, videoSec: 60, quotaMB: 200 } } })+ const sync = make(store)+ await sync.syncMedia({ force: true })+ expect(apiUpload.mock.calls.map(c => c[0])).toEqual(['/api/media/' + hex('b')])+ expect(await media.get(hex('a'))).toMatchObject({ pending: true, rejected: true })+ expect(toast).toHaveBeenCalledWith('The server refused the file as too large.')+ expect(getMediaStatus().rejected).toBe(1)+ // A video under the video cap is not held to the photo's.+ const V = { customEx: [{ id: 'v', media: refOf(hex('c'), null, { kind: 'video', mime: 'video/mp4', size: 3 * MB }) }] }+ await put(hex('c'), 3 * MB, 'video/mp4')+ store.setState({ S: V })+ await sync.syncMedia({ force: true })+ expect(apiUpload.mock.calls.at(-1)[0]).toBe('/api/media/' + hex('c'))+ })+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')