feat(media): media put resumes across runs, and a file already up gives its link again

A run already survives a dropped connection by retrying. This covers what one
run cannot survive: Ctrl-C, a laptop that sleeps, a relay that is gone for
longer than the retries wait.

Each file being put has a job under $XDG_CONFIG_HOME/shade/media-pending/,
keyed by server, path, size, mtime and options. The job holds:
- the sealed copy;
- job.json (0600: the seal result with the file key, the read token, and the
  meta including subtitle grants);
- a lock with the pid of the run working on it. A lock left by a dead run is
  taken over.

Running the same command again resumes with the same media id, so the relay
already has most of the file. Once the relay has stored it, the copy goes and
the grant stays. Putting the file again gives the same link while the relay
still serves it, so a season's put run again after it stopped re-sends only
what is missing. Jobs untouched for 30 days are removed.

Verified with real processes, a 300 MiB file and a file-store relay:
- Run 1 was SIGKILLed after 8 of 19 pieces.
- Run 2 resumed at 42% and finished.
- Run 3 printed "already on the relay, same link".
- `media get` returned the file byte-identical, and the library has one item.

tests/put-jobs.test.ts (4). media 118, cli 15. All 27 packages type-check.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 23:45:56 +02:00
parent 0dc89e6cba
commit 20c5ec07ab
5 changed files with 382 additions and 17 deletions

View File

@@ -73,7 +73,10 @@ New package **`@shade/media`** — see `docs/media.md`.
pieces in flight: the client asks how far the upload got and sends the rest.
Uploads under way survive restarts (`uploads/` on disk, `shade_media_uploads`
beside the S3 multipart upload), count against quota and are swept after
24 h without a piece. `media put` shows progress. Above all, a relay's
24 h without a piece. `media put` shows progress, and resumes across runs:
after Ctrl-C or a long outage the same command goes on with the same
media id (sealed copy and job under `$XDG_CONFIG_HOME/shade/media-pending/`),
and a file already up gives its link again instead of a second copy. Above all, a relay's
memory no longer grows with the film (see Fixed).
- **`shade live video --input`** a film, `/dev/video0` or `rtsp://…`: ffmpeg is
run for you, H.264/AAC copied (a camera's own encoder) and anything else