Files in the browser: the media route and the kind registry

The foundation for rendering files on the blob page (#7329, #7330).

The media route

GET /:owner/:name/media/:ref/*path serves a file's bytes under a real media type, for <img>, <video>, <audio>, <object> and fetch().

The kind registry

fileKind(path, firstBytes) answers image | svg | pdf | audio | video | csv | model3d | config | markdown | patch | text | binary. RENDER_CAP is the largest file each kind draws inline; over it, the page offers Download.

kinds.ts is plain data with no web dependency: the editor and the terminal ask it whether a path is Markdown, as the blob page does.

renderBlob asks rendererFor in render/renderers.ts. A kind in RENDERERS is drawn by its module; a kind with none yet keeps the source or binary view it always had. A file too large for the page to read is drawn only by a media kind (MEDIA_KINDS), whose renderer never reads the body (the browser fetches the media route), and only up to that kind's cap. A page with no media route (a live browse of another forge, /origins/…) draws no media kind. Where a renderer exists the header offers Rendered | Source; every file's header has the Raw and Download buttons.

Audio and video (#7333)

An audio file is <audio controls preload="metadata"> and a video <video controls playsinline preload="metadata">, each with a <source> on the media route and the registry's type (render/audio.ts, render/video.ts, render/player.ts). Range on the media route is what seeks. A .mov also offers itself as video/mp4, which is what most QuickTime files are inside, for browsers that refuse video/quicktime.

Adding a renderer

Write src/web/render/<kind>.ts exporting a Renderer (plain data in: path, size, body, and media, the media route's address; HTML out), and name it once in RENDERERS (render/renderers.ts); rendererFor picks it. Point any element that needs the bytes at input.media, never at an inline copy, so repository content only ever runs under the media route's sandbox. render/markdown.ts is the example.

3D models

An .stl (ASCII or binary), .obj, .3mf, .glb or .gltf file's page shows the part, ready to turn (src/web/render/model3d.ts, #7335).

Images and SVG (#7331)

render/image.ts draws the image and svg kinds as an <img> at the media route; an SVG is never inlined. The picture fits the pane; a click shows it at 100% and the pane scrolls or drags to pan. A checkerboard sits behind it for transparency. The header line carries the pixel size (read in the browser from naturalWidth/naturalHeight by a few lines in the shell script, so no new request), the file size and the format. Animated GIF and WebP play, since the browser draws the original bytes. Over RENDER_CAP the page shows the "too large" notice and Download.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs