Files
Kazier/backend/handlers/ocr.go
T
m d5886c678b feat(api): OCR réel — POST /ocr/jobs + GET /ocr/jobs/:id (tesseract syscall, jobs asynchrones)
- ocr/tesseract.go : Engine → Tesseract subprocess (OCR_LANG fra+eng) ; PDF → calque texte via ledongthuc/pdf (go.mod : nouveau dep)
- repository/ocr_jobs.go : queued→processing→done/failed, scoping device, text/error NULLIFés
- service/ocr.go : Create valide le fichier (GetFile), queue + goroutine de traitement ; physique résolu par glob UPLOAD_DIR/<device>/<id>.* ; fail propre (fichier illisible, erreur moteur)
- handlers/ocr.go : réels (validate fileId, NOT_FOUND si job d'un autre device), fin 501 OCR
- tests end-to-end : cycle queued→done (stub moteur), NOT_FOUND fichier inconnu, scoping device
- smoke réel : PNG 'VAULTDROP' → job done text='VAULTDROP' (tesseract installé)
- docs/AGENTS : §5 + état des routes (tout V1 réel)
2026-09-10 19:54:16 +02:00

54 lines
1.1 KiB
Go

package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/vaultdrop/backend/pkg/api"
"github.com/vaultdrop/backend/repository"
)
type ocrJobRequest struct {
FileID string `json:"fileId"`
}
func OcrJobsCreate(c *gin.Context) {
if Store == nil || Ocr == nil {
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
return
}
deviceID := c.GetString(DeviceIDKey)
var req ocrJobRequest
if err := c.ShouldBindJSON(&req); err != nil {
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid request")
return
}
job, err := Ocr.Create(deviceID, req.FileID)
if err != nil {
writeError(c, err)
return
}
api.OK(c, job)
}
func OcrJobsGet(c *gin.Context) {
if Ocr == nil {
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
return
}
deviceID := c.GetString(DeviceIDKey)
job, err := Ocr.Get(deviceID, c.Param("id"))
if err != nil {
if err == repository.ErrJobNotFound {
api.Error(c, http.StatusNotFound, "NOT_FOUND", "ocr job not found")
return
}
writeError(c, err)
return
}
api.OK(c, job)
}