Scanyonero/runner.go
2024-11-04 18:23:06 +01:00

59 lines
1.1 KiB
Go

package main
import (
"log"
"os"
"os/exec"
"path/filepath"
"time"
)
const OCRMyPDFExecutable = "ocrmypdf"
type Runner struct {
InputPatterns []string // The "Glob" patterns used for searching input files.
OutputPath string // The output directory passed to OCRmyPDF.
Interval time.Duration
}
func (r Runner) Run() {
if r.Interval == 0 {
r.Interval = 5 * time.Second
}
go func() {
ticker := time.NewTicker(r.Interval)
for range ticker.C {
for _, inputPattern := range r.InputPatterns {
filenames, err := filepath.Glob(inputPattern)
if err != nil {
log.Panicf("Failed to get input files: %v", err)
}
for _, filename := range filenames {
outputPath := filepath.Join(r.OutputPath, filepath.Base(filename))
args := []string{
"-l deu+eng",
"--rotate-pages",
"--optimize 1",
}
args = append(args, filename, outputPath)
cmd := exec.Command(OCRMyPDFExecutable, args...)
if err := cmd.Run(); err != nil {
log.Panicf("Failed to run OCRmyPDF: %v", err)
}
// Only delete the PDF if the previous steps did succeed.
os.Remove(filename)
}
}
}
}()
}