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.
Serve LFS objects in the raw and blob views
54bbbe45eadc398d4c1039a3b4b41d257e97083e · Keenan Nemetz · 13d ago · parent 3b2bcc30
Serve LFS objects in the raw and blob views The code browser read the committed blob, so an LFS-tracked texture was a 130-byte pointer: raw returned it with an image content type, and the blob page showed pointer source instead of the picture. Game teams browse assets in the forge, so this was the most visible part of the engine gap. openBlobSource reads at most one pointer's worth of the blob. A pointer is resolved through the repository's LFS namespace, which keeps private and cross-repository reads at the 404 they already were. Raw serves the object with ServeContent, so Range, HEAD, ETag and conditional requests work on the asset; blob reads the object into the existing preview logic, and the object size, not the pointer size, decides the preview cap. A pointer whose object is missing says so: raw answers 404, and the blob page shows the pointer with a note rather than pretending it is the file. Files larger than a pointer that merely begin with the version line pass through the normal path. Stacked on render-media-previews: the raw type and preview logic this builds on are not on main yet.
server/web/codebrowser.go +136−16
| @@ -8,17 +8,21 @@ package web | |||
| 8 | 8 | // this is the page that reads it. | |
| 9 | 9 | ||
| 10 | 10 | import ( | |
| 11 | "bytes" | ||
| 11 | 12 | "fmt" | |
| 12 | 13 | "html/template" | |
| 13 | 14 | "io" | |
| 14 | 15 | "net" | |
| 15 | 16 | "net/http" | |
| 17 | "os" | ||
| 16 | 18 | "sort" | |
| 17 | 19 | "strconv" | |
| 18 | 20 | "strings" | |
| 21 | "time" | ||
| 19 | 22 | "unicode/utf8" | |
| 20 | 23 | ||
| 21 | 24 | "rodent.software/vole/core/git" | |
| 25 | "rodent.software/vole/core/lfs" | ||
| 22 | 26 | "rodent.software/vole/forge/config" | |
| 23 | 27 | "rodent.software/vole/forge/repo" | |
| 24 | 28 | ) | |
| @@ -59,6 +63,7 @@ type codeView struct { | |||
| 59 | 63 | Lines []string | |
| 60 | 64 | Language string | |
| 61 | 65 | Size int64 | |
| 66 | LFSMissing bool | ||
| 62 | 67 | IsMarkdown bool | |
| 63 | 68 | IsImage bool | |
| 64 | 69 | ImagePreview bool | |
| @@ -228,7 +233,40 @@ func (w *Web) blob(rw http.ResponseWriter, r *http.Request, raw bool) { | |||
| 228 | 233 | return | |
| 229 | 234 | } | |
| 230 | 235 | defer stream.Close() | |
| 236 | |||
| 237 | // A file tracked by Git LFS is a pointer in the tree. Resolving it here is | ||
| 238 | // what makes both the download and the preview show the asset instead of the | ||
| 239 | // stub that stands in for it. | ||
| 240 | src, err := w.openBlobSource(m, stream) | ||
| 241 | if err != nil { | ||
| 242 | w.serverError(rw, r, "cannot read "+p+": "+err.Error()) | ||
| 243 | return | ||
| 244 | } | ||
| 245 | if src.object != nil { | ||
| 246 | defer src.object.Close() | ||
| 247 | } | ||
| 248 | |||
| 231 | 249 | if raw { | |
| 250 | if src.object != nil { | ||
| 251 | // ServeContent gives a large asset Range, HEAD and conditional | ||
| 252 | // requests without this path buffering it. The object id is its | ||
| 253 | // content, so it is the right validator and cannot go stale. | ||
| 254 | h := rw.Header() | ||
| 255 | h.Set("Content-Type", rawContentType(p)) | ||
| 256 | h.Set("X-Content-Type-Options", "nosniff") | ||
| 257 | if isSVG(p) { | ||
| 258 | h.Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'") | ||
| 259 | } | ||
| 260 | h.Set("Cache-Control", w.downloadCacheControl(m)+", no-transform") | ||
| 261 | h.Set("ETag", `"`+src.oid+`"`) | ||
| 262 | http.ServeContent(rw, r, p, time.Time{}, src.object) | ||
| 263 | return | ||
| 264 | } | ||
| 265 | if src.missing { | ||
| 266 | http.Error(rw, "vole: the LFS object this file names is not stored on this instance", | ||
| 267 | http.StatusNotFound) | ||
| 268 | return | ||
| 269 | } | ||
| 232 | 270 | // The download is the one place that should carry every byte, so it is | |
| 233 | 271 | // streamed rather than read and written: a 100 MiB asset costs one copy | |
| 234 | 272 | // buffer per request, not 100 MiB of heap. A byte range skips the bytes | |
| @@ -250,36 +288,40 @@ func (w *Web) blob(rw http.ResponseWriter, r *http.Request, raw bool) { | |||
| 250 | 288 | // re-encoded on the way out; a compressed body has no such length. | |
| 251 | 289 | h.Set("Cache-Control", w.downloadCacheControl(m)+", no-transform") | |
| 252 | 290 | h.Set("Accept-Ranges", "bytes") | |
| 253 | br, partial, unsatisfiable := parseByteRange(r.Header.Get("Range"), stream.Size) | ||
| 291 | // openBlobSource consumed the head to look for a pointer; put it back in | ||
| 292 | // front of the rest of the git stream. | ||
| 293 | body := io.MultiReader(bytes.NewReader(src.head), stream) | ||
| 294 | size := stream.Size | ||
| 295 | br, partial, unsatisfiable := parseByteRange(r.Header.Get("Range"), size) | ||
| 254 | 296 | if unsatisfiable { | |
| 255 | h.Set("Content-Range", fmt.Sprintf("bytes */%d", stream.Size)) | ||
| 297 | h.Set("Content-Range", fmt.Sprintf("bytes */%d", size)) | ||
| 256 | 298 | rw.WriteHeader(http.StatusRequestedRangeNotSatisfiable) | |
| 257 | 299 | return | |
| 258 | 300 | } | |
| 259 | 301 | if partial { | |
| 260 | h.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", br.start, br.start+br.length-1, stream.Size)) | ||
| 302 | h.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", br.start, br.start+br.length-1, size)) | ||
| 261 | 303 | h.Set("Content-Length", strconv.FormatInt(br.length, 10)) | |
| 262 | 304 | rw.WriteHeader(http.StatusPartialContent) | |
| 263 | 305 | if r.Method == http.MethodHead { | |
| 264 | 306 | return | |
| 265 | 307 | } | |
| 266 | 308 | if br.start > 0 { | |
| 267 | if _, err := io.CopyN(io.Discard, stream, br.start); err != nil { | ||
| 309 | if _, err := io.CopyN(io.Discard, body, br.start); err != nil { | ||
| 268 | 310 | w.Logger("raw %s %s: skip: %v", m.Name, p, err) | |
| 269 | 311 | return | |
| 270 | 312 | } | |
| 271 | 313 | } | |
| 272 | if _, err := io.CopyN(rw, stream, br.length); err != nil { | ||
| 314 | if _, err := io.CopyN(rw, body, br.length); err != nil { | ||
| 273 | 315 | // The client hung up mid-download; headers are already out. | |
| 274 | 316 | w.Logger("raw %s %s: %v", m.Name, p, err) | |
| 275 | 317 | } | |
| 276 | 318 | return | |
| 277 | 319 | } | |
| 278 | h.Set("Content-Length", strconv.FormatInt(stream.Size, 10)) | ||
| 320 | h.Set("Content-Length", strconv.FormatInt(size, 10)) | ||
| 279 | 321 | if r.Method == http.MethodHead { | |
| 280 | 322 | return | |
| 281 | 323 | } | |
| 282 | if _, err := io.Copy(rw, stream); err != nil { | ||
| 324 | if _, err := io.Copy(rw, body); err != nil { | ||
| 283 | 325 | // The client hung up mid-download; headers are already out. | |
| 284 | 326 | w.Logger("raw %s %s: %v", m.Name, p, err) | |
| 285 | 327 | } | |
| @@ -287,10 +329,36 @@ func (w *Web) blob(rw http.ResponseWriter, r *http.Request, raw bool) { | |||
| 287 | 329 | } | |
| 288 | 330 | // Everything the page shows is a preview: the tree and the line numbers are | |
| 289 | 331 | // bounded, so there is no reason to hold the whole file. | |
| 290 | data, truncated, err := stream.ReadPrefix(maxInlineBytes) | ||
| 291 | if err != nil { | ||
| 292 | w.serverError(rw, r, "cannot read "+p+": "+err.Error()) | ||
| 293 | return | ||
| 332 | var ( | ||
| 333 | data []byte | ||
| 334 | truncated bool | ||
| 335 | size int64 | ||
| 336 | ) | ||
| 337 | switch { | ||
| 338 | case src.object != nil: | ||
| 339 | size = src.size | ||
| 340 | n := size | ||
| 341 | if n > maxInlineBytes { | ||
| 342 | n = maxInlineBytes | ||
| 343 | truncated = true | ||
| 344 | } | ||
| 345 | data = make([]byte, n) | ||
| 346 | if _, err := io.ReadFull(src.object, data); err != nil { | ||
| 347 | w.serverError(rw, r, "cannot read "+p+": "+err.Error()) | ||
| 348 | return | ||
| 349 | } | ||
| 350 | case src.missing: | ||
| 351 | // Show what is committed, but say plainly that it is not the file. | ||
| 352 | data, size = src.head, stream.Size | ||
| 353 | default: | ||
| 354 | rest, restTruncated, err := stream.ReadPrefix(int64(maxInlineBytes - len(src.head))) | ||
| 355 | if err != nil { | ||
| 356 | w.serverError(rw, r, "cannot read "+p+": "+err.Error()) | ||
| 357 | return | ||
| 358 | } | ||
| 359 | data = append(src.head, rest...) | ||
| 360 | truncated = restTruncated | ||
| 361 | size = stream.Size | ||
| 294 | 362 | } | |
| 295 | 363 | content := string(data) | |
| 296 | 364 | image := isImage(p) | |
| @@ -329,13 +397,13 @@ func (w *Web) blob(rw http.ResponseWriter, r *http.Request, raw bool) { | |||
| 329 | 397 | case markdown: | |
| 330 | 398 | preview = !truncated && r.URL.Query().Get("preview") == "1" | |
| 331 | 399 | case model && binary: | |
| 332 | preview = stream.Size > 0 && stream.Size <= maxModelPreviewBytes | ||
| 400 | preview = size > 0 && size <= maxModelPreviewBytes | ||
| 333 | 401 | case model: | |
| 334 | preview = stream.Size > 0 && !truncated && r.URL.Query().Get("preview") == "1" | ||
| 402 | preview = size > 0 && !truncated && r.URL.Query().Get("preview") == "1" | ||
| 335 | 403 | case image && binary: | |
| 336 | preview = stream.Size > 0 && stream.Size <= maxMediaPreviewBytes | ||
| 404 | preview = size > 0 && size <= maxMediaPreviewBytes | ||
| 337 | 405 | case image: | |
| 338 | preview = stream.Size > 0 && !truncated && r.URL.Query().Get("preview") == "1" | ||
| 406 | preview = size > 0 && !truncated && r.URL.Query().Get("preview") == "1" | ||
| 339 | 407 | } | |
| 340 | 408 | var rendered template.HTML | |
| 341 | 409 | if preview && markdown { | |
| @@ -356,7 +424,8 @@ func (w *Web) blob(rw http.ResponseWriter, r *http.Request, raw bool) { | |||
| 356 | 424 | view.LineCut = lineCut | |
| 357 | 425 | view.Lines = lines | |
| 358 | 426 | view.Language = language | |
| 359 | view.Size = stream.Size | ||
| 427 | view.Size = size | ||
| 428 | view.LFSMissing = src.missing | ||
| 360 | 429 | view.IsMarkdown = markdown | |
| 361 | 430 | view.IsImage = image | |
| 362 | 431 | view.ImagePreview = image && preview | |
| @@ -374,6 +443,57 @@ func (w *Web) blob(rw http.ResponseWriter, r *http.Request, raw bool) { | |||
| 374 | 443 | w.render(rw, "code", view, 0) | |
| 375 | 444 | } | |
| 376 | 445 | ||
| 446 | // blobSource is what a blob turned out to be: a pointer whose object this | ||
| 447 | // repository may serve, a pointer whose object is missing, or an ordinary file | ||
| 448 | // whose head has been read and must be put back in front of the stream. | ||
| 449 | type blobSource struct { | ||
| 450 | head []byte | ||
| 451 | object *os.File | ||
| 452 | size int64 | ||
| 453 | oid string | ||
| 454 | missing bool | ||
| 455 | } | ||
| 456 | |||
| 457 | // openBlobSource reads at most one pointer's worth of the blob. A regular file | ||
| 458 | // is left for the caller to continue reading; a pointer is resolved through the | ||
| 459 | // repository's LFS namespace, so a private repository and another repository's | ||
| 460 | // object answer the same 404 they would anywhere else. | ||
| 461 | func (w *Web) openBlobSource(m *repo.Meta, stream *git.BlobStream) (blobSource, error) { | ||
| 462 | var src blobSource | ||
| 463 | head, truncated, err := stream.ReadPrefix(lfs.MaxPointerBytes) | ||
| 464 | if err != nil { | ||
| 465 | return src, err | ||
| 466 | } | ||
| 467 | src.head = head | ||
| 468 | // A pointer is small by definition; a larger blob that begins with the same | ||
| 469 | // version line is a real file. | ||
| 470 | if truncated || stream.Size > lfs.MaxPointerBytes { | ||
| 471 | return src, nil | ||
| 472 | } | ||
| 473 | ptr, ok := lfs.ParsePointer(head) | ||
| 474 | if !ok { | ||
| 475 | return src, nil | ||
| 476 | } | ||
| 477 | if w.LFS == nil { | ||
| 478 | src.missing = true | ||
| 479 | return src, nil | ||
| 480 | } | ||
| 481 | f, n, err := w.LFS.Open(m.Name, ptr.OID) | ||
| 482 | if err != nil { | ||
| 483 | src.missing = true | ||
| 484 | w.Logger("lfs open %s %s: %v", m.Name, ptr.OID, err) | ||
| 485 | return src, nil | ||
| 486 | } | ||
| 487 | if n != ptr.Size { | ||
| 488 | f.Close() | ||
| 489 | src.missing = true | ||
| 490 | w.Logger("lfs size %s %s: stored %d, pointer says %d", m.Name, ptr.OID, n, ptr.Size) | ||
| 491 | return src, nil | ||
| 492 | } | ||
| 493 | src.object, src.size, src.oid = f, n, ptr.OID | ||
| 494 | return src, nil | ||
| 495 | } | ||
| 496 | |||
| 377 | 497 | func (w *Web) Commits(rw http.ResponseWriter, r *http.Request) { | |
| 378 | 498 | m, ok := w.open(rw, r) | |
| 379 | 499 | if !ok { | |