aboutsummaryrefslogtreecommitdiff
path: root/gui/internal/model/preview.go
diff options
context:
space:
mode:
Diffstat (limited to 'gui/internal/model/preview.go')
-rw-r--r--gui/internal/model/preview.go19
1 files changed, 17 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))}