vcom/internal/preview/image.go
vrubelroman 5d36b76bbe feat(preview): pure Build over vfs with limits, tools timeout and fast highlighting (5B)
Build(ctx, req) never opens FIFOs, sockets or devices (Kind=Special;
U-P1-10), reads at most MaxBytes, keeps at most DirLimit directory entries
and MaxLines text lines, and honours ctx in reads, tools and highlighting.
pdftotext/pdfinfo/ffprobe run in their own process group with a 3 s timeout
and are killed with their children. A chroma formatter writes foreground
SGR only (no background, no regexp post-processing); the first
HighlightLines (400) lines are highlighted, so 64 KiB of Go takes ~20 ms
(was 190). Remote images are downloaded to an LRU cache only up to
max_remote_image_bytes (N-P1-16); a 32-entry result cache avoids rebuilds.
theme.ChromaStyle maps every built-in theme (U-P2); KindLabel fixes the
inverted label (U-P2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 08:57:15 +03:00

30 lines
905 B
Go

package preview
import (
"context"
"image"
"vcom/internal/vfs"
)
// buildImage reports format and size; imgview decodes later. A remote image
// is downloaded to the cache only up to MaxRemoteImageBytes (N-P1-16).
func buildImage(ctx context.Context, req Request, res Result, fi vfs.FileInfo, format string, cfg image.Config) Result {
res.Kind = Image
res.Meta.ImageFormat, res.Meta.Width, res.Meta.Height = format, cfg.Width, cfg.Height
ref := &ImageRef{Loc: req.Loc, Size: fi.Size, ModTime: fi.ModTime, Format: format, Width: cfg.Width, Height: cfg.Height}
res.Image = ref
switch {
case req.Loc.FS.Kind() == vfs.KindLocal:
ref.Local = req.Loc.Path
case fi.Size > req.MaxRemoteImageBytes:
ref.TooLarge = true
case req.Images != nil:
p, err := req.Images.Fetch(ctx, req.Loc.FS, req.Loc.Path, fi.Size, fi.ModTime)
if err != nil {
res.Err = err
}
ref.Local = p
}
return res
}