Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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/diff.go +60

@@ -8,6 +8,7 @@ package web
8 8
9 9 import (
10 10 "html/template"
11 "net/url"
11 12 "regexp"
12 13 "strconv"
13 14 "strings"
@@ -22,6 +23,14 @@ type diffFile struct {
22 23 Hunks []diffHunk
23 24 Added int
24 25 Removed int
26
27 // Repo, OldRef and NewRef locate each side of the file in the raw route,
28 // which is what lets an image be shown instead of a "binary file" note. A
29 // view that cannot name a commit for a side leaves that ref empty, and the
30 // side is not previewed; the URL builders report that with "".
31 Repo string
32 OldRef string
33 NewRef string
25 34 }
26 35
27 36 // Label is what to show as the file's name: the new path, or the old one for a
@@ -33,6 +42,57 @@ func (f diffFile) Label() string {
33 42 return f.OldPath
34 43 }
35 44
45 // diffMedia is one side of a changed file that can be shown rather than read: a
46 // picture, or a 3D model. Src is the raw URL and "" when that side does not
47 // exist or has nothing to preview; Model says which kind it is, and Format names
48 // the model loader ("stl", "obj", "gltf", "glb" or "3mf").
49 type diffMedia struct {
50 Src string
51 Format string
52 Model bool
53 }
54
55 // OldMedia and NewMedia locate the two sides in the raw route. The old side of a
56 // deletion and the new side of an addition are the only file that exists, so
57 // each is answered on its own.
58 func (f diffFile) OldMedia() diffMedia { return f.media(f.OldRef, f.OldPath) }
59 func (f diffFile) NewMedia() diffMedia { return f.media(f.NewRef, f.NewPath) }
60
61 func (f diffFile) media(ref, p string) diffMedia {
62 if f.Repo == "" || ref == "" || p == "" {
63 return diffMedia{}
64 }
65 src := "/" + url.PathEscape(f.Repo) + "/raw/" + url.PathEscape(ref) + "/" + pathURL(p)
66 switch {
67 case isImage(p):
68 return diffMedia{Src: src}
69 case isModel(p):
70 return diffMedia{Src: src, Format: modelFormat(p), Model: true}
71 }
72 return diffMedia{}
73 }
74
75 // withDiffMedia records where each changed file's two sides can be read. The
76 // refs are commits, not ranges, because the raw route resolves one revision.
77 func withDiffMedia(repo, oldRef, newRef string, files []diffFile) {
78 for i := range files {
79 files[i].Repo = repo
80 files[i].OldRef = oldRef
81 files[i].NewRef = newRef
82 }
83 }
84
85 // diffHasMedia reports whether any file in the diff would be shown rather than
86 // read. A request view resolves the commits behind its range only when one is.
87 func diffHasMedia(files []diffFile) bool {
88 for _, f := range files {
89 if isImage(f.OldPath) || isImage(f.NewPath) || isModel(f.OldPath) || isModel(f.NewPath) {
90 return true
91 }
92 }
93 return false
94 }
95
36 96 // pathspec is what git has to be told to print this file's patch. A rename needs
37 97 // both of its paths: filtered to the destination alone, git reports what is left
38 98 // as a new file and the rename is lost.