From dc7b313c504d68165f1d1b085a6ce94eb6e8b55f Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 10 Jan 2022 15:10:23 +0000 Subject: rescribe: handle PDF errors much more gracefully --- cmd/rescribe/gui.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'cmd/rescribe/gui.go') diff --git a/cmd/rescribe/gui.go b/cmd/rescribe/gui.go index fb0450a..7ae5465 100644 --- a/cmd/rescribe/gui.go +++ b/cmd/rescribe/gui.go @@ -298,7 +298,38 @@ func startGui(log log.Logger, cmd string, training string, tessdir string) error } }() - err = startProcess(log, cmd, dir.Text, filepath.Base(dir.Text), trainingOpts.Selected, dir.Text, tessdir) + bookdir := dir.Text + savedir := dir.Text + bookname := filepath.Base(dir.Text) + + f, err := os.Stat(bookdir) + if err != nil { + // TODO: surface error and cancel process better + fmt.Fprintf(os.Stderr, "Error opening file as PDF: %v\n", err) + return + } + + if strings.HasSuffix(dir.Text, ".pdf") && !f.IsDir() { + bookdir, err = extractPdfImgs(bookdir) + if err != nil { + // TODO: surface error and cancel process better + fmt.Fprintf(os.Stderr, "Error opening file as PDF: %v\n", err) + return + } + + // happens if extractPdfImgs recovers from a PDF panic, + // which will occur if we encounter an image we can't decode + if bookdir == "" { + // TODO: surface error and cancel process better + fmt.Fprintf(os.Stderr, "Error opening PDF\nThe format of this PDF is not supported, extract the images manually into a folder first.\n") + return + } + + savedir = strings.TrimSuffix(savedir, ".pdf") + bookname = strings.TrimSuffix(bookname, ".pdf") + } + + err = startProcess(log, cmd, bookdir, bookname, trainingOpts.Selected, savedir, tessdir) if err != nil { // add a newline before this printing as another message from stdout // or stderr may well be half way through printing -- cgit v1.2.1-24-ge1ad