some bug fixes
This commit is contained in:
parent
0e49d60994
commit
0e914c629e
16 changed files with 179 additions and 52 deletions
7
pkg/scan/args.go
Normal file
7
pkg/scan/args.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
// Copyright (C) 2019 Marius Schellenberger
|
||||
|
||||
// +build !openbsd
|
||||
|
||||
package scan
|
||||
|
||||
const tesseract_oem = "2"
|
||||
7
pkg/scan/args_openbsd.go
Normal file
7
pkg/scan/args_openbsd.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
// Copyright (C) 2019 Marius Schellenberger
|
||||
|
||||
// +build openbsd
|
||||
|
||||
package scan
|
||||
|
||||
const tesseract_oem = "1"
|
||||
|
|
@ -56,11 +56,11 @@ func New(base string, langs []string, timeout time.Duration) (s *Scanner, err er
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = godrop.Unveil(tcmd, "rx")
|
||||
pcmd, err = exec.LookPath("pdftotext")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
pcmd, err = exec.LookPath("pdftotext")
|
||||
err = godrop.Unveil(tcmd, "rx")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -73,11 +73,11 @@ func New(base string, langs []string, timeout time.Duration) (s *Scanner, err er
|
|||
}
|
||||
for i, l := range langs {
|
||||
lang += l
|
||||
if i < len(langs) {
|
||||
if i < len(langs)-1 {
|
||||
lang += "+"
|
||||
}
|
||||
}
|
||||
args := []string{"-l", lang, "--oem", "2"}
|
||||
args := []string{"-l", lang, "--oem", tesseract_oem}
|
||||
s = &Scanner{
|
||||
base,
|
||||
timeout,
|
||||
|
|
@ -98,14 +98,14 @@ func (s *Scanner) Scan(file string) (filename, text string, err error) {
|
|||
f *os.File
|
||||
)
|
||||
p, _ := filepath.Split(file)
|
||||
filename = filepath.Join(p, core.FileName()+pdfExt)
|
||||
pdfCmd := exec.Command(s.t.cmd, append(s.t.args, []string{scanfile, "stdout", "pdf"}...)...)
|
||||
pdfCmd.Env = s.t.env
|
||||
log.Debugf("%v %s %v", pdfCmd.Env, pdfCmd.Path, pdfCmd.Args)
|
||||
pdf, err = core.Timeout(pdfCmd, s.timeout)
|
||||
pdf, err = timeout(pdfCmd, s.timeout)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filename = filepath.Join(p, core.FileName()+pdfExt)
|
||||
pdffile = filepath.Join(s.base, filename)
|
||||
f, err = os.OpenFile(pdffile, os.O_RDWR|os.O_CREATE, fs.FileMode)
|
||||
if err != nil {
|
||||
|
|
@ -124,8 +124,9 @@ func (s *Scanner) Scan(file string) (filename, text string, err error) {
|
|||
}
|
||||
txtCmd := exec.Command(s.p.cmd, append(s.p.args, []string{pdffile, "-"}...)...)
|
||||
log.Debugf("%s %v", txtCmd.Path, txtCmd.Args)
|
||||
txt, err := core.Timeout(txtCmd, s.timeout)
|
||||
txt, err := timeout(txtCmd, s.timeout)
|
||||
if err != nil {
|
||||
log.Debugf("killed", err)
|
||||
return
|
||||
}
|
||||
if len(txt) <= 2 {
|
||||
|
|
|
|||
39
pkg/scan/timeout.go
Normal file
39
pkg/scan/timeout.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
// Copyright (C) 2019 Marius Schellenberger
|
||||
|
||||
package scan
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
func timeout(cmd *exec.Cmd, t time.Duration) (output []byte, err error) {
|
||||
done := make(chan error)
|
||||
out := make(chan []byte)
|
||||
go func() {
|
||||
o, e := cmd.Output()
|
||||
done <- e
|
||||
out <- o
|
||||
}()
|
||||
select {
|
||||
case <-time.After(t):
|
||||
e := cmd.Process.Kill()
|
||||
err = <-done
|
||||
<-out
|
||||
if e != nil {
|
||||
err = e
|
||||
} else {
|
||||
err = fmt.Errorf("command '%s' timed out: %s", cmd.Path, err)
|
||||
}
|
||||
case e := <-done:
|
||||
exerr, ok := e.(*exec.ExitError)
|
||||
if ok {
|
||||
err = fmt.Errorf("%s\n%s", e, string(exerr.Stderr))
|
||||
} else {
|
||||
err = e
|
||||
}
|
||||
output = <-out
|
||||
}
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue