Files
Shade/packages/shade-media/src/album.ts
Sterister 8c7300fa51 feat(media): albums — one link for a season or a photo album; fix path traversal in media get
An album is a sealed file (shade.album/v1) listing grants, so the relay
cannot tell it from a film. `shade media put dir/` uploads every file and
the album (subdirectories nest, natural order); `media get` brings an
album back as a directory; /watch/ shows a grid with decrypted previews
and Back returns to the album without putting grants in history. Kotlin
reads albums (Albums.read) against a new shared vector.

Fixed: `media get` used the sealer-chosen meta.name as the output path.
Fixed a relay test race (flush() is sent, not received); the relay now
reports framesIn.

Verified: 64 TS tests x3, Kotlin 81/0, a 6-file album with a nested album
uploaded, browsed, played and downloaded back bit-identical.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-18 21:44:43 +02:00

92 lines
3.6 KiB
TypeScript

/**
* Albums: one link for many files.
*
* A season of a series, a photo album, a playlist, a wall of cameras. An
* album is itself a sealed file (SMF1) whose plaintext is a list of grants,
* with the MIME type `application/vnd.shade.album+json`. So:
*
* - one capability link opens every item, and nothing else;
* - the relay stores it like any other object: it cannot tell an album
* from a film, let alone count or name what is in it;
* - an album may hold live grants (a camera wall) and other albums.
*
* Holding an album means holding every item's keys. Deleting the album
* object does not revoke them from anyone who already opened it — delete
* the items to do that.
*/
import { MediaFormatError } from './errors.js';
import { encodeGrant, parseGrant } from './grant.js';
import type { Grant, MediaGrant } from './grant.js';
import type { MediaMeta } from './sealed.js';
export const ALBUM_KIND = 'shade.album/v1';
export const ALBUM_MIME = 'application/vnd.shade.album+json';
/** Enough for any season or camera roll, small enough to parse without thought. */
export const MAX_ALBUM_ITEMS = 10_000;
const MAX_TITLE = 512;
export interface Album {
title: string;
items: Grant[];
}
const enc = new TextEncoder();
const dec = new TextDecoder('utf-8', { fatal: true });
export function encodeAlbum(album: Album): Uint8Array {
checkTitle(album.title);
if (album.items.length > MAX_ALBUM_ITEMS) throw new MediaFormatError(`album holds more than ${MAX_ALBUM_ITEMS} items`);
// Grants are embedded as objects, each in its own wire form.
const items = album.items.map((g) => JSON.parse(encodeGrant(g)) as unknown);
return enc.encode(JSON.stringify({ kind: ALBUM_KIND, title: album.title, items }));
}
/** Parse an album's plaintext, strictly: every item must be a valid grant. */
export function parseAlbum(bytes: Uint8Array): Album {
let o: unknown;
try {
o = JSON.parse(dec.decode(bytes));
} catch {
throw new MediaFormatError('album is not UTF-8 JSON');
}
if (typeof o !== 'object' || o === null || Array.isArray(o)) throw new MediaFormatError('album must be an object');
const r = o as Record<string, unknown>;
if (r.kind !== ALBUM_KIND) throw new MediaFormatError(`not an album (kind ${String(r.kind)})`);
if (typeof r.title !== 'string') throw new MediaFormatError('album.title must be a string');
checkTitle(r.title);
if (!Array.isArray(r.items)) throw new MediaFormatError('album.items must be an array');
if (r.items.length > MAX_ALBUM_ITEMS) throw new MediaFormatError(`album holds more than ${MAX_ALBUM_ITEMS} items`);
const items = r.items.map((it, i) => {
if (typeof it !== 'object' || it === null || Array.isArray(it)) throw new MediaFormatError(`album item ${i} is not a grant`);
try {
return parseGrant(JSON.stringify(it));
} catch (e) {
throw new MediaFormatError(`album item ${i}: ${(e as Error).message}`);
}
});
return { title: r.title, items };
}
/** Does this grant (by its meta preview) or this sealed meta name an album? */
export function isAlbum(x: Grant | MediaMeta | undefined): boolean {
if (x === undefined) return false;
const meta = 'kind' in x ? x.meta : x;
return meta?.mime === ALBUM_MIME;
}
/** The meta an album's sealed file carries. */
export function albumMeta(title: string): MediaMeta {
return { mime: ALBUM_MIME, name: title };
}
/** Narrow for callers that only take sealed files. */
export function isMediaGrant(g: Grant): g is MediaGrant {
return g.kind === 'shade.media-grant/v1';
}
function checkTitle(t: string): void {
if (t.length > MAX_TITLE) throw new MediaFormatError(`album title longer than ${MAX_TITLE} characters`);
}