vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Render images and 3D models in the browser views
3b2bcc30534f24b4628b6365e69dcb9065115065 · Keenan Nemetz · 13d ago · parent 87bab8e1
Render images and 3D models in the browser views A blob whose name is an image is served as that image, so the <img> a README already emitted works and the source view can show it. An SVG keeps its source view and previews on request. The instance introduction resolves assets/, so a landing-page picture is no longer a broken link. A changed image or model shows both sides in the commit, compare, and change request diffs, where git only says "binary". The 3D viewer is a vendored three.js bundle, loaded only when a page has a model, with a download fallback for a reader without JavaScript or WebGL. closes: 01M7XQ9AKKKFNEX0F679JRRYQD
server/web/codebrowser.go +59−15
| @@ -51,16 +51,21 @@ type codeView struct { | |||
| 51 | 51 | Readme template.HTML | |
| 52 | 52 | ReadmeName string | |
| 53 | 53 | ||
| 54 | Content string | ||
| 55 | Markdown template.HTML | ||
| 56 | Binary bool | ||
| 57 | Truncated bool | ||
| 58 | LineCut bool | ||
| 59 | Lines []string | ||
| 60 | Language string | ||
| 61 | Size int64 | ||
| 62 | IsMarkdown bool | ||
| 63 | Preview bool | ||
| 54 | Content string | ||
| 55 | Markdown template.HTML | ||
| 56 | Binary bool | ||
| 57 | Truncated bool | ||
| 58 | LineCut bool | ||
| 59 | Lines []string | ||
| 60 | Language string | ||
| 61 | Size int64 | ||
| 62 | IsMarkdown bool | ||
| 63 | IsImage bool | ||
| 64 | ImagePreview bool | ||
| 65 | IsModel bool | ||
| 66 | ModelFormat string | ||
| 67 | ModelPreview bool | ||
| 68 | Preview bool | ||
| 64 | 69 | } | |
| 65 | 70 | ||
| 66 | 71 | // Code is the file browser: a persistent tree beside the file it has open. The | |
| @@ -230,7 +235,17 @@ func (w *Web) blob(rw http.ResponseWriter, r *http.Request, raw bool) { | |||
| 230 | 235 | // before it in the stream (git cannot seek) and copies only the requested | |
| 231 | 236 | // part, so the win is what does not cross the network. | |
| 232 | 237 | h := rw.Header() | |
| 233 | h.Set("Content-Type", "text/plain; charset=utf-8") | ||
| 238 | // An image is served as its type so an <img> in a README and the source | ||
| 239 | // view's preview both work. Everything else stays text/plain, and nosniff | ||
| 240 | // keeps a blob that contains HTML from being treated as a document on this | ||
| 241 | // origin. | ||
| 242 | h.Set("Content-Type", rawContentType(p)) | ||
| 243 | h.Set("X-Content-Type-Options", "nosniff") | ||
| 244 | if isSVG(p) { | ||
| 245 | // An SVG opened directly is a document, and a document can carry | ||
| 246 | // script. The policy makes it inert; as an <img> it renders normally. | ||
| 247 | h.Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'") | ||
| 248 | } | ||
| 234 | 249 | // The announced length is part of the contract, so a text blob must not be | |
| 235 | 250 | // re-encoded on the way out; a compressed body has no such length. | |
| 236 | 251 | h.Set("Cache-Control", w.downloadCacheControl(m)+", no-transform") | |
| @@ -278,7 +293,12 @@ func (w *Web) blob(rw http.ResponseWriter, r *http.Request, raw bool) { | |||
| 278 | 293 | return | |
| 279 | 294 | } | |
| 280 | 295 | content := string(data) | |
| 281 | binary := !utf8.Valid(data) | ||
| 296 | image := isImage(p) | ||
| 297 | // A raster format can start with bytes that happen to be valid UTF-8 (a GIF | ||
| 298 | // begins "GIF89a"), so validity is not what decides whether a picture has a | ||
| 299 | // source to read. An image that is not an SVG is binary by definition. | ||
| 300 | raster := image && !isSVG(p) | ||
| 301 | binary := !utf8.Valid(data) || raster | ||
| 282 | 302 | // A table row per line is pleasant to read and cheap for a source file, but a | |
| 283 | 303 | // million blank lines would be neither. Past the cap the page falls back to a | |
| 284 | 304 | // preformatted block. | |
| @@ -297,11 +317,28 @@ func (w *Web) blob(rw http.ResponseWriter, r *http.Request, raw bool) { | |||
| 297 | 317 | language = prismLanguage(p) | |
| 298 | 318 | } | |
| 299 | 319 | markdown := !binary && isMarkdown(p) | |
| 320 | model := isModel(p) | ||
| 300 | 321 | // A markdown file opens as source like any other file. Rendering it is an | |
| 301 | // explicit choice, so the code browser never hides the bytes by default. | ||
| 302 | preview := markdown && !truncated && r.URL.Query().Get("preview") == "1" | ||
| 322 | // explicit choice, so the code browser never hides the bytes by default. A | ||
| 323 | // raster image or a binary model has no source worth reading, so the picture | ||
| 324 | // or the mesh is the file; an SVG, an OBJ, a glTF or an ASCII STL is text and | ||
| 325 | // keeps its source view until preview is asked for. A file past the preview | ||
| 326 | // cap stays a download. | ||
| 327 | preview := false | ||
| 328 | switch { | ||
| 329 | case markdown: | ||
| 330 | preview = !truncated && r.URL.Query().Get("preview") == "1" | ||
| 331 | case model && binary: | ||
| 332 | preview = stream.Size > 0 && stream.Size <= maxModelPreviewBytes | ||
| 333 | case model: | ||
| 334 | preview = stream.Size > 0 && !truncated && r.URL.Query().Get("preview") == "1" | ||
| 335 | case image && binary: | ||
| 336 | preview = stream.Size > 0 && stream.Size <= maxMediaPreviewBytes | ||
| 337 | case image: | ||
| 338 | preview = stream.Size > 0 && !truncated && r.URL.Query().Get("preview") == "1" | ||
| 339 | } | ||
| 303 | 340 | var rendered template.HTML | |
| 304 | if preview { | ||
| 341 | if preview && markdown { | ||
| 305 | 342 | // The document knows where it lives, so a relative link in it can be | |
| 306 | 343 | // resolved against its directory instead of the URL it is being read at. | |
| 307 | 344 | rendered = w.renderTreeMarkdown(treeDoc{Repo: m.Name, Ref: ref, Path: p}, content) | |
| @@ -321,6 +358,13 @@ func (w *Web) blob(rw http.ResponseWriter, r *http.Request, raw bool) { | |||
| 321 | 358 | view.Language = language | |
| 322 | 359 | view.Size = stream.Size | |
| 323 | 360 | view.IsMarkdown = markdown | |
| 361 | view.IsImage = image | ||
| 362 | view.ImagePreview = image && preview | ||
| 363 | view.IsModel = model | ||
| 364 | if model { | ||
| 365 | view.ModelFormat = modelFormat(p) | ||
| 366 | } | ||
| 367 | view.ModelPreview = model && preview | ||
| 324 | 368 | view.Preview = preview | |
| 325 | 369 | if !full { | |
| 326 | 370 | w.render(rw, "code_main", view, 0) | |