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

perf: serve thumbnail covers on the Media Collections grid (#13179)

Adam Eivy3904a55a2a6b
diff --git a/server/services/mediaAssetIndex/gallery.js b/server/services/mediaAssetIndex/gallery.js
index 518ab38a0f..696b5bce83 100644
--- a/server/services/mediaAssetIndex/gallery.js
+++ b/server/services/mediaAssetIndex/gallery.js
@@ -121,8 +121,39 @@ export async function listGalleryFacets(diskReader) {
categories: [...categories], kinds: [...kinds], collections: populated.filter(Boolean) };
}
+const FLAT_PNG_COVER = /^\/data\/images\/([A-Za-z0-9][A-Za-z0-9._-]*)\.png$/i;
+const VIDEO_THUMB_COVER = /^\/data\/video-thumbnails\/([A-Za-z0-9][A-Za-z0-9._-]*)\.[A-Za-z0-9]+$/;
+
+/**
+ * Card-sized cover URL: a flat gallery PNG maps to its lazily built 512px WebP
+ * (`/data/image-thumbnails`) instead of the multi-MB original, and a cover whose
+ * asset is neither local nor peer-hosted becomes null so the card never issues
+ * a request that is certain to fail. Other shapes pass through unchanged.
+ */
+async function projectCovers(summaries) {
+ const { resolveMediaLocations } = await import('../peerHostedMedia.js');
+ const probes = summaries.map(({ cover }) => {
+ const png = FLAT_PNG_COVER.exec(cover || '');
+ if (png) return { kind: 'image', ref: `${png[1]}.png`, url: `/data/image-thumbnails/${png[1]}.webp` };
+ const thumb = VIDEO_THUMB_COVER.exec(cover || '');
+ if (thumb) return { kind: 'video', ref: thumb[1], url: cover };
+ return null;
+ });
+ const located = await resolveMediaLocations(probes.filter(Boolean));
+ let next = 0;
+ return summaries.map((summary, i) => {
+ const probe = probes[i];
+ if (!probe) return summary;
+ return { ...summary, cover: located[next++].location === 'missing' ? null : probe.url };
+ });
+}
+
/** Collection grid covers and counts, without downloading all image sidecars. */
export async function listGalleryCollectionSummaries(diskReader) {
+ return projectCovers(await collectionSummaryRows(diskReader));
+}
+
+async function collectionSummaryRows(diskReader) {
const collections = await (await import('../mediaCollections.js')).listCollections();
const videoSnapshot = await (await import('../videoGen/history.js')).loadCompactHistory();
if (!escapeHatch()) {
diff --git a/server/services/mediaAssetIndex/gallery.test.js b/server/services/mediaAssetIndex/gallery.test.js
index b9ed946677..9f85db26f6 100644
--- a/server/services/mediaAssetIndex/gallery.test.js
+++ b/server/services/mediaAssetIndex/gallery.test.js
@@ -7,6 +7,8 @@ const { listAnnotations, loadHistory, listCollections, getCollection } = vi.hois
vi.mock('../videoGen/history.js', () => ({ loadHistory, loadCompactHistory: loadHistory }));
vi.mock('../mediaCollections.js', () => ({ listCollections, getCollection }));
vi.mock('../mediaAnnotations.js', () => ({ listAnnotations }));
+const { resolveMediaLocations } = vi.hoisted(() => ({ resolveMediaLocations: vi.fn() }));
+vi.mock('../peerHostedMedia.js', () => ({ resolveMediaLocations }));
vi.mock('../../lib/db.js', () => ({ query: vi.fn() }));
afterEach(() => { vi.unstubAllEnvs(); vi.resetAllMocks(); });
@@ -156,12 +158,31 @@ describe('scoped browsing compatibility', () => {
expect((await listGalleryPage({ kind: 'all', collectionId: 'col', limit: 1 }, disk)).items).toEqual([{ kind: 'video', data: video }]);
expect((await listGalleryPage({ kind: 'all', collectionId: 'unsorted', limit: 1 }, disk)).items).toEqual([{ kind: 'image', data: images[1] }]);
expect(await listGalleryFacets(disk)).toEqual({ universes: [{ id: 'u', name: 'Example' }], categories: ['places'], kinds: ['canon'], collections: [{ id: 'col', name: 'Example collection' }] });
+ resolveMediaLocations.mockImplementation(async items => items.map(() => ({ location: 'local' })));
const summaries = await listGalleryCollectionSummaries(disk);
- expect(summaries.find(s => s.id === 'col')).toMatchObject({ total: 2, cover: '/data/images/a.png' });
+ expect(summaries.find(s => s.id === 'col')).toMatchObject({ total: 2, cover: '/data/image-thumbnails/a.webp' });
expect(summaries.find(s => s.id === 'unsorted')).toMatchObject({ total: 1, counts: { image: 1, video: 0, all: 1 } });
});
});
+describe('collection cover projection', () => {
+ it('serves thumbnails and nulls covers whose asset is gone locally and on every peer', async () => {
+ vi.stubEnv('MEMORY_BACKEND', 'file');
+ loadHistory.mockResolvedValue([]);
+ const collections = [
+ { id: 'ok', items: [{ kind: 'image', ref: 'a.png', addedAt: '2026-01-01' }] },
+ { id: 'gone', items: [{ kind: 'image', ref: 'b.png', addedAt: '2026-01-01' }] },
+ ];
+ listCollections.mockResolvedValue(collections);
+ getCollection.mockImplementation(async id => collections.find(c => c.id === id));
+ resolveMediaLocations.mockImplementation(async items => items.map(i => ({ location: i.ref === 'b.png' ? 'missing' : 'local' })));
+ const disk = async () => [{ filename: 'a.png', createdAt: '2026-01-01' }, { filename: 'b.png', createdAt: '2026-01-01' }];
+ const byId = Object.fromEntries((await listGalleryCollectionSummaries(disk)).map(s => [s.id, s.cover]));
+ expect(byId.ok).toBe('/data/image-thumbnails/a.webp');
+ expect(byId.gone).toBeNull();
+ });
+});
+
// The route test (routes/imageGen.clean.test.js) owns the both-directions
// contract end to end against a real clean. What only lives here: the SQL the
// indexed path emits, and the disk path the route test cannot reach because