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>
This commit is contained in:
2026-10-18 21:44:43 +02:00
parent e696d83283
commit 8c7300fa51
18 changed files with 726 additions and 44 deletions

View File

@@ -3,6 +3,8 @@
* own, and open anything you hold a grant for.
*/
import { ALBUM_MIME, albumMeta, encodeAlbum, parseAlbum } from './album.js';
import type { Album } from './album.js';
import { randomBytes, sha256, toB64u, toHex } from './bytes.js';
import { MediaFormatError } from './errors.js';
import { mediaGrantFrom, mediaKeysOf } from './grant.js';
@@ -79,6 +81,12 @@ export class MediaClient {
return this.putSealed(result, bytes, opts.meta !== undefined ? { ...opts, meta: opts.meta } : opts);
}
/** Seal and upload an album; the grant opens every item in it. */
async uploadAlbum(album: Album, opts: Omit<PutOptions, 'meta'> = {}): Promise<MediaGrant> {
const meta = albumMeta(album.title);
return this.uploadBytes(encodeAlbum(album), { ...opts, meta });
}
async delete(mediaId: Uint8Array): Promise<boolean> {
const id = toHex(mediaId);
const res = await this.fetchFn(mediaUrl(this.server, mediaId), {
@@ -112,3 +120,21 @@ export function openGrant(
});
return MediaReader.open(source, mediaKeysOf(grant), opts);
}
/** An album holds at most 10 000 grants of well under 1 KiB each. */
const MAX_ALBUM_BYTES = 16 * 1024 * 1024;
/** Open a granted album and read its items. */
export async function openAlbum(
grant: MediaGrant,
opts: ReaderOptions & { fetch?: typeof fetch; url?: string } = {},
): Promise<Album> {
return readAlbum(await openGrant(grant, opts));
}
/** The items of an album already opened as a reader. */
export async function readAlbum(reader: MediaReader): Promise<Album> {
if (reader.meta.mime !== ALBUM_MIME) throw new MediaFormatError(`not an album (${reader.meta.mime ?? 'no type'})`);
if (reader.size > MAX_ALBUM_BYTES) throw new MediaFormatError('album larger than 16 MiB');
return parseAlbum(await reader.read(0, reader.size));
}