Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([issue-4188]): address codex review — playable video keys, poster fallback, one upload cap

[._.]/ Adam Eivy5856eee31d33
diff --git a/client/src/pages/MoodBoardDetail.jsx b/client/src/pages/MoodBoardDetail.jsx
index e6e18eea22..e8a10e489f 100644
--- a/client/src/pages/MoodBoardDetail.jsx
+++ b/client/src/pages/MoodBoardDetail.jsx
@@ -506,7 +506,24 @@ export default function MoodBoardDetail() {
className="relative w-full aspect-square bg-port-bg text-gray-600 group"
>
{src ? (
- <img src={src} alt={item.caption || ''} loading="lazy" className="w-full h-full object-cover" />
+ <img
+ src={src}
+ alt={item.caption || ''}
+ loading="lazy"
+ className="w-full h-full object-cover"
+ onError={(e) => {
+ // A synced board can carry a poster URL whose file
+ // only exists on the sending machine (a downloaded
+ // video's thumbnail is named `<id>.jpg`, not
+ // `<filename-stem>.jpg`). The receiver regenerates
+ // the stem-named poster when it pulls the video, so
+ // fall back to that derived name on a 404.
+ const fallback = moodBoardItemSrc({ ...item, imageUrl: null });
+ if (fallback && e.currentTarget.getAttribute('src') !== fallback) {
+ e.currentTarget.src = fallback;
+ }
+ }}
+ />
) : (
<span className="w-full h-full flex items-center justify-center">
<Film className="w-8 h-8" aria-hidden="true" />
diff --git a/client/src/pages/MoodBoardDetail.test.jsx b/client/src/pages/MoodBoardDetail.test.jsx
index cd1890634f..168d8945ef 100644
--- a/client/src/pages/MoodBoardDetail.test.jsx
+++ b/client/src/pages/MoodBoardDetail.test.jsx
@@ -147,4 +147,28 @@ describe('MoodBoardDetail video items (#4188)', () => {
expect(video.getAttribute('src')).toBe('/data/videos/upload-ab12cd34.mp4');
});
});
+
+ it('falls back to the derived stem poster when the stored thumbnail 404s (synced download pin)', async () => {
+ // A downloaded video's sender-side thumbnail is `<id>.jpg`, but a peer
+ // regenerates `<filename-stem>.jpg` on pull — the stored URL 404s there.
+ mockGetMoodBoard.mockResolvedValueOnce({
+ id: 'a',
+ name: 'Board A',
+ items: [{
+ id: 'mbi-1',
+ type: 'video',
+ mediaKey: 'video:downloaded-abc123.mp4',
+ imageUrl: '/data/video-thumbnails/abc123.jpg',
+ caption: null,
+ source: null,
+ }],
+ });
+ renderPage();
+ await waitFor(() => expect(boardNameValue()).toBe('Board A'));
+
+ const poster = screen.getByRole('button', { name: 'Play video' }).querySelector('img');
+ expect(poster.getAttribute('src')).toBe('/data/video-thumbnails/abc123.jpg');
+ fireEvent.error(poster);
+ expect(poster.getAttribute('src')).toBe('/data/video-thumbnails/downloaded-abc123.jpg');
+ });
});
diff --git a/server/lib/moodBoardValidation.js b/server/lib/moodBoardValidation.js
index 1c1467ea28..7f0191db5d 100644
--- a/server/lib/moodBoardValidation.js
+++ b/server/lib/moodBoardValidation.js
@@ -16,6 +16,24 @@ export const MOOD_BOARD_ITEM_TYPES = Object.freeze(['image', 'text', 'video']);
// so a board can't pin a key the rest of PortOS would reject.
const mediaKeySchema = z.string().trim().refine(isValidKey, 'mediaKey must be a valid `<kind>:<ref>` media key');
+// A `type:'video'` board item's mediaKey ref is the on-disk FILENAME
+// (extension included), not a bare history id: playback resolves it as
+// `/data/videos/<ref>` and the peer-sync asset manifest passes an extensioned
+// ref through untouched, so `video:job-123` would 404 locally and mis-guess
+// `.mp4` on the wire. parseKey already bounds the ref and rejects `:`; this
+// adds the path-traversal guard (mirroring sanitizeAssetFilename) and the
+// extension requirement. Shared with the item-update invariant in
+// moodBoard/logic.js so a PATCH can't swap a video item onto a non-video or
+// extension-less key.
+export function isVideoItemMediaKey(key) {
+ const parsed = parseKey(key || '');
+ if (parsed?.kind !== 'video') return false;
+ const { ref } = parsed;
+ if (ref.includes('/') || ref.includes('\\')) return false;
+ if (ref === '.' || ref === '..') return false;
+ return /\.[a-z0-9]{2,6}$/i.test(ref);
+}
+
// External/pinned image URL. http(s) or a same-origin app path (e.g. a served
// `/data/images/...` URL). Bounded; the UI renders it in an <img>, so no exotic
// schemes. A protocol-relative `//host/...` is rejected: it starts with `/` but
@@ -74,10 +92,10 @@ export const moodBoardItemCreateSchema = z.object({
});
}
} else if (val.type === 'video') {
- if (parseKey(val.mediaKey || '')?.kind !== 'video') {
+ if (!isVideoItemMediaKey(val.mediaKey)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
- message: 'a video item requires a `video:<filename>` mediaKey',
+ message: 'a video item requires a `video:<filename>` mediaKey (filename with extension)',
path: ['mediaKey'],
});
}
diff --git a/server/lib/moodBoardValidation.test.js b/server/lib/moodBoardValidation.test.js
index 6c80db61ff..2feb859982 100644
--- a/server/lib/moodBoardValidation.test.js
+++ b/server/lib/moodBoardValidation.test.js
@@ -10,6 +10,7 @@ import {
moodBoardUpdateSchema,
moodBoardItemCreateSchema,
moodBoardItemUpdateSchema,
+ isVideoItemMediaKey,
} from './moodBoardValidation.js';
describe('moodBoardCreateSchema', () => {
@@ -73,6 +74,25 @@ describe('moodBoardItemCreateSchema', () => {
it('rejects a video item whose mediaKey is not kind video', () => {
expect(moodBoardItemCreateSchema.safeParse({ type: 'video', mediaKey: 'image:a.png' }).success).toBe(false);
});
+ it('rejects a video item whose ref is a bare id (no extension) — it would 404 as a filename', () => {
+ expect(moodBoardItemCreateSchema.safeParse({ type: 'video', mediaKey: 'video:job-123' }).success).toBe(false);
+ });
+});
+
+describe('isVideoItemMediaKey', () => {
+ it('accepts a filename-with-extension video key', () => {
+ expect(isVideoItemMediaKey('video:upload-ab12cd34.mp4')).toBe(true);
+ expect(isVideoItemMediaKey('video:clip.webm')).toBe(true);
+ });
+ it('rejects non-video kinds, extension-less refs, and traversal shapes', () => {
+ expect(isVideoItemMediaKey('image:a.png')).toBe(false);
+ expect(isVideoItemMediaKey('video:job-123')).toBe(false);
+ expect(isVideoItemMediaKey('video:..')).toBe(false);
+ expect(isVideoItemMediaKey('video:a/b.mp4')).toBe(false);
+ expect(isVideoItemMediaKey('video:a\\b.mp4')).toBe(false);
+ expect(isVideoItemMediaKey(null)).toBe(false);
+ expect(isVideoItemMediaKey('')).toBe(false);
+ });
});
describe('moodBoardItemUpdateSchema', () => {
diff --git a/server/routes/videoGen.js b/server/routes/videoGen.js
index aa9b32afca..963ba8de8f 100644
--- a/server/routes/videoGen.js
+++ b/server/routes/videoGen.js
@@ -62,6 +62,7 @@ import {
} from '../lib/hfCache.js';
import { startHfDownloadStream, openSseStream } from '../lib/sseDownload.js';
import { saveUploadedGalleryVideo } from '../services/videoUpload.js';
+import { JSON_BODY_LIMIT_BYTES } from '../lib/uploadLimits.js';
import { createInstallLogger } from '../lib/installLogger.js';
const router = Router();
@@ -1221,10 +1222,12 @@ router.get('/history', asyncHandler(async (_req, res) => {
// POST /api/image-gen/upload. Lands the bytes under PATHS.videos with a
// `source: 'upload'` history entry so the file federates via the peer-sync
// asset manifest (unlike POST /api/uploads → data/uploads/, which does not).
-// Base64 max = 56MB ≈ the 41MB binary cap (MAX_GALLERY_VIDEO_UPLOAD_BYTES)
-// after base64 expansion, mirroring the image route's transport bound.
+// The schema's string cap is the JSON body-parser limit itself — anything
+// longer 413s at the parser before this route runs — and the reachable cap
+// is the binary MAX_GALLERY_VIDEO_UPLOAD_BYTES check in the saver, both
+// derived from server/lib/uploadLimits.js so there is one source of truth.
const uploadVideoSchema = z.object({
- data: z.string().min(1).max(56 * 1024 * 1024),
+ data: z.string().min(1).max(JSON_BODY_LIMIT_BYTES),
filename: z.string().max(255).optional(),
});
diff --git a/server/services/moodBoard/logic.js b/server/services/moodBoard/logic.js
index f19a8066b3..2cc17a6c43 100644
--- a/server/services/moodBoard/logic.js
+++ b/server/services/moodBoard/logic.js
@@ -16,6 +16,7 @@ import { ServerError } from '../../lib/errorHandler.js';
import { compareNewerWins } from '../../lib/lwwTimestamp.js';
import { sanitizeSoftDeleteFields } from '../../lib/syncWire.js';
import { localImageFilename, assetBasename } from '../../lib/localImageFilename.js';
+import { isVideoItemMediaKey } from '../../lib/moodBoardValidation.js';
const isStr = (v) => typeof v === 'string';
@@ -205,8 +206,8 @@ export function updateItem(board, itemId, patch) {
if (updated.type === 'image' && !updated.mediaKey && !updated.imageUrl) {
throw new ServerError('An image item must keep a mediaKey or imageUrl', { status: 400, code: 'INVALID_ITEM' });
}
- if (updated.type === 'video' && !updated.mediaKey) {
- throw new ServerError('A video item must keep its mediaKey', { status: 400, code: 'INVALID_ITEM' });
+ if (updated.type === 'video' && !isVideoItemMediaKey(updated.mediaKey)) {
+ throw new ServerError('A video item must keep a video:<filename> mediaKey', { status: 400, code: 'INVALID_ITEM' });
}
if (updated.type === 'text' && (typeof updated.text !== 'string' || !updated.text.trim())) {
throw new ServerError('A text item must keep non-empty text', { status: 400, code: 'INVALID_ITEM' });
diff --git a/server/services/moodBoard/logic.test.js b/server/services/moodBoard/logic.test.js
index bd21753f30..ee57db1be2 100644
--- a/server/services/moodBoard/logic.test.js
+++ b/server/services/moodBoard/logic.test.js
@@ -139,6 +139,10 @@ describe('updateItem', () => {
expect(item.caption).toBe('clip');
expect(item.text).toBeNull();
expect(() => updateItem(board, vidId, { mediaKey: null })).toThrow(/mediaKey/i);
+ // A PATCH can't swap a video item onto a non-video or extension-less key
+ // either — the merged key must stay a playable video:<filename>.
+ expect(() => updateItem(board, vidId, { mediaKey: 'image:foo.png' })).toThrow(/mediaKey/i);
+ expect(() => updateItem(board, vidId, { mediaKey: 'video:job-123' })).toThrow(/mediaKey/i);
});
});
diff --git a/server/services/videoUpload.js b/server/services/videoUpload.js
index f9df90e6c1..7acf39e6bb 100644
--- a/server/services/videoUpload.js
+++ b/server/services/videoUpload.js
@@ -24,13 +24,15 @@ import { randomUUID } from 'crypto';
import { writeFile, unlink } from 'fs/promises';
import { ServerError } from '../lib/errorHandler.js';
import { PATHS, ensureDir } from '../lib/fileUtils.js';
+import { MAX_BASE64_UPLOAD_BYTES } from '../lib/uploadLimits.js';
import { generateThumbnail, probeVideoDuration } from '../lib/ffmpeg.js';
import { mutateVideoHistory } from './videoGen/history.js';
import { videoGenEvents } from './videoGen/events.js';
-// Matches the client-side JSON transport cap (JSON_UPLOAD_MAX_FILE_SIZE) so
-// the server never accepts what the picker wouldn't send.
-export const MAX_GALLERY_VIDEO_UPLOAD_BYTES = 41 * 1024 * 1024;
+// The largest raw file that fits the JSON body-parser limit once
+// base64-encoded — the single source of truth in uploadLimits.js, mirrored
+// client-side as JSON_UPLOAD_MAX_FILE_SIZE (the cap the picker enforces).
+export const MAX_GALLERY_VIDEO_UPLOAD_BYTES = MAX_BASE64_UPLOAD_BYTES;
// Container sniff — extension comes from the BYTES, not the client filename.
// ISO-BMFF (`ftyp` box at offset 4) covers mp4/m4v/mov; the `qt` major brand