Files
query-orchestration/api/docInitRunner/runner.go
T
Jay Brown c45e1dd427 Merged in feature/textExtractionsPart1 (pull request #192)
all schema and rest apis for text extraction support

* in progress

* stage 8 complete

* phase 9 completed

* phase 9 complete

* ongoing - s3 path fix

* working

* optimize ci build

* e2e tests

* missing test
2025-11-26 19:23:42 +00:00

79 lines
1.8 KiB
Go

package docinitrunner
import (
"context"
"database/sql"
"errors"
"log/slog"
"queryorchestration/internal/database/repository"
documentinit "queryorchestration/internal/document/init"
"queryorchestration/internal/serviceconfig/objectstore"
"github.com/google/uuid"
)
const Name = "docInitRunner"
type Services struct {
Document *documentinit.Service
Queries *repository.Queries
}
type Runner struct {
svc *Services
}
func New(svc *Services) Runner {
return Runner{
svc: svc,
}
}
type Body struct {
Bucket string `json:"bucket" validate:"required"`
Key string `json:"key" validate:"required"`
Hash string `json:"hash" validate:"required"`
}
func (s Runner) Process(ctx context.Context, body Body) bool {
key, err := objectstore.ParseBucketKey(body.Key)
if err != nil {
slog.Error("unable to parse key", "key", body.Key, "error", err)
return false
}
// Get filename and folder_id from documentUploads table
var filename *string
var folderID *uuid.UUID
upload, err := s.svc.Queries.GetDocumentUploadByKey(ctx, &repository.GetDocumentUploadByKeyParams{
Bucket: body.Bucket,
Key: body.Key,
})
if err != nil && !errors.Is(err, sql.ErrNoRows) {
slog.Error("unable to get document upload", "bucket", body.Bucket, "key", body.Key, "error", err)
return false
}
if err == nil {
if upload.Filename != nil {
filename = upload.Filename
}
folderID = upload.FolderID
}
id, err := s.svc.Document.Create(ctx, &documentinit.Create{
Key: key,
Bucket: body.Bucket,
Hash: body.Hash,
Filename: filename,
OriginalPath: filename, // For batch uploads, filename contains the full original path
FolderID: folderID,
})
if err != nil {
return false
}
slog.Debug("created document", "id", id, "filename", filename, "folder_id", folderID)
return true
}