CI / lint-backend (push) Failing after 1m39s
CI / lint-mobile (push) Canceled after 11m8s
CI / test-backend (push) Canceled after 0s
CI / test-mobile (push) Canceled after 0s
CI / build-backend (push) Canceled after 0s
CI / docker (push) Canceled after 0s
145 lines
4.3 KiB
Go
145 lines
4.3 KiB
Go
package ocr
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/ledongthuc/pdf"
|
|
)
|
|
|
|
const (
|
|
// tesseractTimeout : budget max par job (A13) — Tesseract ne doit jamais
|
|
// bloquer un worker indéfiniment (image pathologique).
|
|
tesseractTimeout = 120 * time.Second
|
|
// outputCap : plafond stdout/stderr capturés (A13, 2 Mo) — un document
|
|
// hostile ne doit pas faire exploser la RAM du worker.
|
|
outputCap = 2 << 20
|
|
// maxPDFPages : plafond de pages lues (A13) — au-delà, erreur explicite
|
|
// (pas de troncature silencieuse).
|
|
maxPDFPages = 50
|
|
)
|
|
|
|
// validTesseractLang : whitelist `^[a-z+]{2,20}$` (A12, défense en profondeur
|
|
// — le point d'entrée normal est service.normalizeOcrLang). Évite toute
|
|
// injection via `tesseract -l` (espaces, `;`, options `--...`).
|
|
func validTesseractLang(lang string) bool {
|
|
if len(lang) < 2 || len(lang) > 20 {
|
|
return false
|
|
}
|
|
for _, r := range lang {
|
|
if (r < 'a' || r > 'z') && r != '+' {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// TesseractEngine runs the `tesseract` binary in a subprocess.
|
|
// Images are OCR'd directly; PDFs have their text layer extracted first
|
|
// (scanned PDFs → empty text, no rendering pipeline in V1).
|
|
type TesseractEngine struct{}
|
|
|
|
func NewTesseract() *TesseractEngine { return &TesseractEngine{} }
|
|
|
|
// ExtractText implements Engine. Chaque job est borné par tesseractTimeout
|
|
// (contexte dérivé, annulé en fin d'appel).
|
|
func (t *TesseractEngine) ExtractText(ctx context.Context, filePath, lang string) (string, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, tesseractTimeout)
|
|
defer cancel()
|
|
if strings.ToLower(filepath.Ext(filePath)) == ".pdf" {
|
|
return extractPDFText(ctx, filePath)
|
|
}
|
|
return runTesseract(ctx, filePath, lang)
|
|
}
|
|
|
|
// cappedBuffer borne un flux capturé à `limit` octets et signale la
|
|
// troncature (erreur explicite côté appelant, pas de coupe silencieuse).
|
|
type cappedBuffer struct {
|
|
buf bytes.Buffer
|
|
limit int
|
|
truncated bool
|
|
}
|
|
|
|
func (c *cappedBuffer) Write(p []byte) (int, error) {
|
|
remaining := c.limit - c.buf.Len()
|
|
if remaining <= 0 {
|
|
c.truncated = true
|
|
return len(p), nil // déverse : le process ne bloque pas sur un pipe plein
|
|
}
|
|
if len(p) > remaining {
|
|
c.truncated = true
|
|
p = p[:remaining]
|
|
}
|
|
return c.buf.Write(p)
|
|
}
|
|
|
|
func (c *cappedBuffer) String() string { return c.buf.String() }
|
|
|
|
func runTesseract(ctx context.Context, filePath, lang string) (string, error) {
|
|
// Défense en profondeur (A12) : même si l'appelant ne passe pas par
|
|
// service.normalizeOcrLang, `-l` ne reçoit jamais de métacaractères.
|
|
if !validTesseractLang(lang) {
|
|
return "", fmt.Errorf("tesseract: langue %q invalide", lang)
|
|
}
|
|
cmd := exec.CommandContext(ctx, "tesseract", filePath, "stdout", "-l", lang)
|
|
stdout := &cappedBuffer{limit: outputCap}
|
|
stderr := &cappedBuffer{limit: outputCap}
|
|
cmd.Stdout = stdout
|
|
cmd.Stderr = stderr
|
|
if err := cmd.Run(); err != nil {
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
|
return "", fmt.Errorf("tesseract: délai dépassé (%s): %w", tesseractTimeout, ctx.Err())
|
|
}
|
|
if errors.Is(ctx.Err(), context.Canceled) {
|
|
return "", ctx.Err()
|
|
}
|
|
msg := strings.TrimSpace(stderr.String())
|
|
if msg == "" {
|
|
msg = err.Error()
|
|
}
|
|
return "", errors.New("tesseract: " + msg)
|
|
}
|
|
if stdout.truncated {
|
|
return "", fmt.Errorf("tesseract: sortie tronquée (> %d octets)", outputCap)
|
|
}
|
|
// stderr tronqué avec un run OK : le texte est complet, mais le diagnostic
|
|
// est perdu — on l'accepte sans échouer le job.
|
|
return strings.TrimSpace(stdout.String()), nil
|
|
}
|
|
|
|
func extractPDFText(ctx context.Context, filePath string) (string, error) {
|
|
f, r, err := pdf.Open(filePath)
|
|
if err != nil {
|
|
return "", errors.New("pdf: " + err.Error())
|
|
}
|
|
defer f.Close()
|
|
if n := r.NumPage(); n > maxPDFPages {
|
|
return "", fmt.Errorf("pdf: %d pages > plafond %d (tronquer = perdre du texte)", n, maxPDFPages)
|
|
}
|
|
var builder strings.Builder
|
|
for i := 1; i <= r.NumPage(); i++ {
|
|
select {
|
|
case <-ctx.Done():
|
|
return "", fmt.Errorf("pdf: annulé page %d: %w", i, ctx.Err())
|
|
default:
|
|
}
|
|
p := r.Page(i)
|
|
if p.V.IsNull() {
|
|
return "", fmt.Errorf("pdf: page %d nulle", i)
|
|
}
|
|
plain, err := p.GetPlainText(nil)
|
|
if err != nil {
|
|
return "", fmt.Errorf("pdf: page %d illisible: %w", i, err)
|
|
}
|
|
builder.WriteString(plain)
|
|
builder.WriteString("\n")
|
|
}
|
|
return strings.TrimSpace(builder.String()), nil
|
|
}
|