From 4744ffac60b9bde27616f34a934f9404bc314efc Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Thu, 17 Sep 2026 00:32:04 +0200 Subject: gui: a rendered PDF page per file, and Settings in the top right --- gui/internal/model/preview.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'gui/internal/model/preview.go') diff --git a/gui/internal/model/preview.go b/gui/internal/model/preview.go index 6ca029e..d2a2830 100644 --- a/gui/internal/model/preview.go +++ b/gui/internal/model/preview.go @@ -30,6 +30,10 @@ type Preview struct { Image string // a file to show: the file itself, or a rendered page Text string Note string // what this is, or why there is nothing + // Dir is the directory a rendered page was written to, for the caller + // to remove once it is done with the image; "" when nothing was + // rendered and the file itself is being shown. + Dir string } // previewBytes is how much of a text file is read, and previewLines how @@ -72,17 +76,28 @@ func MakePreview(ctx context.Context, path, tmp string) Preview { // pdfPreview renders the first page if poppler can, and falls back to the // document's text - the same pdftotext krino's own (content ...) tests use. +// +// Each render goes in a directory of its own. pdftoppm names its output +// after the page number, so a shared directory would hold several files +// called page-1.png and the wrong one could be picked up - which is what +// happened: a preview showed the page of a PDF looked at earlier (his +// report, 2026-09-17). func pdfPreview(ctx context.Context, path, tmp string, sz int64) Preview { if _, err := exec.LookPath("pdftoppm"); err == nil { - out := filepath.Join(tmp, "page") + dir, err := os.MkdirTemp(tmp, "page-") + if err != nil { + return Preview{Note: err.Error()} + } + out := filepath.Join(dir, "page") cmd := exec.CommandContext(ctx, "pdftoppm", "-png", "-f", "1", "-l", "1", "-scale-to", "700", "--", path, out) if err := cmd.Run(); err == nil { if rendered := firstMatch(out + "*.png"); rendered != "" { - return Preview{Kind: PreviewImage, Image: rendered, + return Preview{Kind: PreviewImage, Image: rendered, Dir: dir, Note: fmt.Sprintf("page 1, %s", size(sz))} } } + os.RemoveAll(dir) } if _, err := exec.LookPath("pdftotext"); err != nil { return Preview{Note: fmt.Sprintf("PDF, %s - install poppler-utils to see it", size(sz))} -- cgit v1.3