aboutsummaryrefslogtreecommitdiff
path: root/gui/internal/model
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-09-17 00:32:04 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-09-17 00:32:04 +0200
commit4744ffac60b9bde27616f34a934f9404bc314efc (patch)
tree0366f6abb1062fc23ab72f8ab47fb7114bed5944 /gui/internal/model
parent04b4243ad31d144c4caf9be4c5096a0f27a2648e (diff)
downloadkrino-4744ffac60b9bde27616f34a934f9404bc314efc.tar.gz
krino-4744ffac60b9bde27616f34a934f9404bc314efc.zip
gui: a rendered PDF page per file, and Settings in the top right
Diffstat (limited to 'gui/internal/model')
-rw-r--r--gui/internal/model/preview.go19
-rw-r--r--gui/internal/model/preview_test.go56
2 files changed, 73 insertions, 2 deletions
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))}
diff --git a/gui/internal/model/preview_test.go b/gui/internal/model/preview_test.go
index 8c19922..b620a92 100644
--- a/gui/internal/model/preview_test.go
+++ b/gui/internal/model/preview_test.go
@@ -5,6 +5,7 @@ package model
import (
"context"
"os"
+ "os/exec"
"path/filepath"
"strings"
"testing"
@@ -96,3 +97,58 @@ func TestPreviewShowsOnlyTheHead(t *testing.T) {
t.Error("a cut file does not say it was cut")
}
}
+
+// TestPdfPreviewsDoNotMixUp: two PDFs looked at one after the other each
+// get their own rendered page. pdftoppm names its file after the page
+// number, so previews sharing a directory would collide - one of his ended
+// up showing another document's cover.
+func TestPdfPreviewsDoNotMixUp(t *testing.T) {
+ if _, err := exec.LookPath("pdftoppm"); err != nil {
+ t.Skip("pdftoppm is not installed")
+ }
+ if _, err := exec.LookPath("groff"); err != nil {
+ t.Skip("groff is not installed, so there is nothing to make a PDF with")
+ }
+ dir := t.TempDir()
+ pdf := func(name, text string) string {
+ p := filepath.Join(dir, name)
+ f, err := os.Create(p)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer f.Close()
+ cmd := exec.Command("groff", "-Tpdf", "-ms")
+ cmd.Stdin = strings.NewReader(".TL\n" + text + "\n")
+ cmd.Stdout = f
+ if err := cmd.Run(); err != nil {
+ t.Skipf("groff cannot write a PDF here: %v", err)
+ }
+ return p
+ }
+ first := pdf("first.pdf", "The first document")
+ second := pdf("second.pdf", "The second document")
+
+ tmp := t.TempDir()
+ a := MakePreview(context.Background(), first, tmp)
+ b := MakePreview(context.Background(), second, tmp)
+ if a.Kind != PreviewImage || b.Kind != PreviewImage {
+ t.Fatalf("previews = %+v, %+v", a, b)
+ }
+ if a.Image == b.Image {
+ t.Fatalf("both previews point at %s", a.Image)
+ }
+ sameBytes := func(x, y string) bool {
+ bx, err1 := os.ReadFile(x)
+ by, err2 := os.ReadFile(y)
+ return err1 == nil && err2 == nil && string(bx) == string(by)
+ }
+ if sameBytes(a.Image, b.Image) {
+ t.Error("the second preview is a copy of the first page rendered")
+ }
+ // The caller is told where to clean up.
+ for _, p := range []Preview{a, b} {
+ if p.Dir == "" {
+ t.Error("a rendered page comes with no directory to remove")
+ }
+ }
+}