1
0
Fork 0

Add document tests, refactor a bit further

This commit is contained in:
Sebastian Schulze 2022-03-03 23:16:38 +01:00
parent c2a8976645
commit 6eaef572d5
Signed by: bascht
GPG Key ID: 5BCB1D3B4D38A35A
4 changed files with 34 additions and 11 deletions

View File

@ -4,3 +4,4 @@ type Command struct {
Name string
Args []string
}

View File

@ -1,11 +1,9 @@
package scan
import (
"fmt"
"time"
)
type Document struct {
Id string
Name string
@ -20,10 +18,8 @@ func (document Document) Args() []string {
if document.Duplex {
cmd = append(cmd, " -d")
}
cmd = append(cmd, document.FullName())
fmt.Println("cmd: ", cmd)
return cmd
}

29
scan/document_test.go Normal file
View File

@ -0,0 +1,29 @@
package scan
import (
"reflect"
"testing"
"time"
)
func TestDocumentFullName(t *testing.T) {
document := Document{
Id: "test-uuid",
Name: "Here-Be-A-Test-Name",
Date: time.Date(2020, 10, 12, 20, 15, 00, 0, time.UTC),
Duplex: true,
Events: make(chan string),
}
if document.FullName() != "2020-10-12-201500-Here-Be-A-Test-Name" {
t.Error("Full name incorrect" + document.FullName())
}
if document.FullNameWithExtension() != "2020-10-12-201500-Here-Be-A-Test-Name.pdf" {
t.Error("Full name with extension incorrect" + document.FullName())
}
if reflect.DeepEqual(document.Args(), []string{"-d", "2020-10-12-201500-Here-Be-A-Test-Name.pdf"}) {
t.Error("Incorrect arguments")
}
}

View File

@ -25,40 +25,37 @@ func Process(basedir string, document *Document) string {
var commands []Command
// tmpfn := filepath.Join(dir, "tmpfile")
commands = append(commands, Command{Name: "scanimage", Args: []string{"--device-name=epjitsu:libusb:001:003", "--format=tiff", "--batch=" + filepath.Join(basedir, document.FullName()+"-%d.tif"), "--source=" + feed, "--mode=Gray", "--resolution=300"}})
commands = append(commands, Command{Name: "convert", Args: []string{filepath.Join(basedir, document.FullName()) + "*tif", filepath.Join(basedir, document.FullName()+"_input.pdf")}})
commands = append(commands, Command{Name: "convert", Args: []string{filepath.Join(basedir, document.FullName()) + "-1.tif", filepath.Join(basedir, document.FullName()+".thumbnail.jpg")}})
commands = append(commands, Command{Name: "ocrmypdf", Args: []string{"--language=deu", filepath.Join(basedir, document.FullName()+"_input.pdf"), filepath.Join(basedir, document.FullName()+".pdf")}})
for _, command := range commands {
fmt.Println("Will call "+command.Name+" with %v", command.Args)
cmd := exec.Command(command.Name, command.Args...)
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatalf("could not get stderr pipe: %v", err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatalf("could not get stdout pipe: %v", err)
}
// cmdReader, _ := cmd.StdoutPipe()
// scanner := bufio.NewScanner(cmdReader)
done := make(chan bool)
go func() {
document.Events <- command.Name + " Startet"
merged := io.MultiReader(stderr, stdout)
scanner := bufio.NewScanner(merged)
for scanner.Scan() {
fmt.Printf("\t > %s\n", scanner.Text())
document.Events <- scanner.Text()
}
document.Events <- command.Name + " Ist fast fertig"
done <- true
}()
cmd.Start()
document.Events <- command.Name + " Bin fast fertig"
document.Events <- command.Name + " Ist gestartet"
cmd.Wait()
<-done
}