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>
150 lines
3.9 KiB
Go
150 lines
3.9 KiB
Go
package preview
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"vcom/internal/vfs"
|
|
)
|
|
|
|
// localPath returns the path tools can read, or "" for a non-local FS.
|
|
func localPath(req Request) string {
|
|
if req.Loc.FS.Kind() == vfs.KindLocal {
|
|
return req.Loc.Path
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func message(res Result, k Kind, lines ...string) Result {
|
|
res.Kind, res.Lines, res.Plain = k, lines, lines
|
|
return res
|
|
}
|
|
|
|
// buildPDF: the text of the first 3 pages via pdftotext, page count via
|
|
// pdfinfo.
|
|
func buildPDF(ctx context.Context, req Request, res Result) Result {
|
|
lp := localPath(req)
|
|
switch {
|
|
case lp == "":
|
|
return message(res, PDF, "PDF on a remote file system: text preview is not available.")
|
|
case req.Tools == nil || req.Tools.PdfToText == "":
|
|
return message(res, PDF, "PDF file. Install poppler-utils (pdftotext) for a text preview.")
|
|
}
|
|
out, err := req.Tools.run(ctx, req.MaxBytes, req.Tools.PdfToText, "-l", "3", "-layout", "-nopgbrk", lp, "-")
|
|
if err != nil {
|
|
res.Kind, res.Err = Error, err
|
|
return res
|
|
}
|
|
if req.Tools.PdfInfo != "" {
|
|
if info, err := req.Tools.run(ctx, 64<<10, req.Tools.PdfInfo, lp); err == nil {
|
|
for _, line := range strings.Split(string(info), "\n") {
|
|
if v, ok := strings.CutPrefix(line, "Pages:"); ok {
|
|
res.Meta.PageCount = strings.TrimSpace(v)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
res.Kind = PDF
|
|
res.Plain, res.Truncated = textLines(string(out), req.MaxLines)
|
|
res.Lines = res.Plain
|
|
return res
|
|
}
|
|
|
|
// buildMedia: duration, bitrate, codecs via ffprobe.
|
|
func buildMedia(ctx context.Context, req Request, res Result, k Kind) Result {
|
|
lp := localPath(req)
|
|
switch {
|
|
case lp == "":
|
|
return message(res, k, "Media file on a remote file system: metadata is not available.")
|
|
case req.Tools == nil || req.Tools.FFProbe == "":
|
|
return message(res, k, "Media file. Install ffmpeg (ffprobe) for metadata.")
|
|
}
|
|
out, err := req.Tools.run(ctx, 1<<20, req.Tools.FFProbe, "-v", "quiet", "-print_format", "json",
|
|
"-show_format", "-show_streams", lp)
|
|
if err != nil {
|
|
res.Kind, res.Err = Error, err
|
|
return res
|
|
}
|
|
var info struct {
|
|
Format struct {
|
|
Duration string `json:"duration"`
|
|
Bitrate string `json:"bit_rate"`
|
|
} `json:"format"`
|
|
Streams []struct {
|
|
CodecType string `json:"codec_type"`
|
|
CodecName string `json:"codec_name"`
|
|
SampleRate string `json:"sample_rate"`
|
|
Channels int `json:"channels"`
|
|
Width int `json:"width"`
|
|
Height int `json:"height"`
|
|
} `json:"streams"`
|
|
}
|
|
if err := json.Unmarshal(out, &info); err != nil {
|
|
res.Kind, res.Err = Error, fmt.Errorf("ffprobe output: %w", err)
|
|
return res
|
|
}
|
|
m := &res.Meta
|
|
if secs, err := strconv.ParseFloat(info.Format.Duration, 64); err == nil {
|
|
s := int(secs)
|
|
if s >= 3600 {
|
|
m.Duration = fmt.Sprintf("%d:%02d:%02d", s/3600, s%3600/60, s%60)
|
|
} else {
|
|
m.Duration = fmt.Sprintf("%d:%02d", s/60, s%60)
|
|
}
|
|
}
|
|
if bps, err := strconv.ParseInt(info.Format.Bitrate, 10, 64); err == nil {
|
|
m.Bitrate = fmt.Sprintf("%d kbps", bps/1000)
|
|
}
|
|
for _, st := range info.Streams {
|
|
switch st.CodecType {
|
|
case "video":
|
|
if m.VideoCodec == "" {
|
|
m.VideoCodec = st.CodecName
|
|
if st.Width > 0 {
|
|
m.Dimensions = fmt.Sprintf("%dx%d", st.Width, st.Height)
|
|
}
|
|
}
|
|
case "audio":
|
|
if m.AudioCodec == "" {
|
|
m.AudioCodec = st.CodecName
|
|
if st.SampleRate != "" {
|
|
m.SampleRate = st.SampleRate + " Hz"
|
|
}
|
|
m.Channels = channels(st.Channels)
|
|
}
|
|
}
|
|
}
|
|
var lines []string
|
|
add := func(label, v string) {
|
|
if v != "" {
|
|
lines = append(lines, fmt.Sprintf("%-11s %s", label+":", v))
|
|
}
|
|
}
|
|
add("Duration", m.Duration)
|
|
add("Bitrate", m.Bitrate)
|
|
add("Video", m.VideoCodec)
|
|
add("Resolution", m.Dimensions)
|
|
add("Audio", m.AudioCodec)
|
|
add("Rate", m.SampleRate)
|
|
add("Channels", m.Channels)
|
|
return message(res, k, lines...)
|
|
}
|
|
|
|
func channels(n int) string {
|
|
switch n {
|
|
case 0:
|
|
return ""
|
|
case 1:
|
|
return "mono"
|
|
case 2:
|
|
return "stereo"
|
|
case 6:
|
|
return "5.1"
|
|
case 8:
|
|
return "7.1"
|
|
}
|
|
return fmt.Sprintf("%d ch", n)
|
|
}
|