ingest documents
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
package ingest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"mime"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"mediarr/content"
|
||||
"mediarr/document"
|
||||
)
|
||||
|
||||
const (
|
||||
KindVideo = document.KindVideo
|
||||
KindSubtitle = document.KindSubtitle
|
||||
KindImage = document.KindImage
|
||||
KindFile = document.KindFile
|
||||
)
|
||||
|
||||
type Probe struct {
|
||||
Mime string
|
||||
Metadata map[string]any
|
||||
}
|
||||
|
||||
type Prober interface {
|
||||
Probe(ctx context.Context, path string, info fs.FileInfo) (Probe, error)
|
||||
}
|
||||
|
||||
type BasicProber struct{}
|
||||
|
||||
func (BasicProber) Probe(_ context.Context, path string, _ fs.FileInfo) (Probe, error) {
|
||||
mimeType := mime.TypeByExtension(strings.ToLower(filepath.Ext(path)))
|
||||
if mimeType == "" {
|
||||
mimeType = "application/octet-stream"
|
||||
}
|
||||
|
||||
return Probe{Mime: mimeType}, nil
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
documents *document.Service
|
||||
contentService *content.Service
|
||||
prober Prober
|
||||
}
|
||||
|
||||
func NewService(documents *document.Service, contentService *content.Service, prober Prober) *Service {
|
||||
if prober == nil {
|
||||
prober = BasicProber{}
|
||||
}
|
||||
|
||||
return &Service{
|
||||
documents: documents,
|
||||
contentService: contentService,
|
||||
prober: prober,
|
||||
}
|
||||
}
|
||||
|
||||
type ScanResult struct {
|
||||
Files int
|
||||
Directories int
|
||||
Created int
|
||||
Updated int
|
||||
Unchanged int
|
||||
Errors []ScanError
|
||||
}
|
||||
|
||||
func (r ScanResult) HasErrors() bool {
|
||||
return len(r.Errors) > 0
|
||||
}
|
||||
|
||||
type ScanError struct {
|
||||
Path string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e ScanError) Error() string {
|
||||
return fmt.Sprintf("%s: %v", e.Path, e.Err)
|
||||
}
|
||||
|
||||
func (e ScanError) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
var (
|
||||
ErrServiceNotConfigured = errors.New("service d'ingestion non configuré")
|
||||
ErrInvalidRoot = errors.New("racine de scan invalide")
|
||||
ErrNotFolder = errors.New("document existant n'est pas un dossier")
|
||||
ErrNotFile = errors.New("document existant n'est pas un fichier")
|
||||
ErrNameConflict = errors.New("conflit de nom insensible à la casse")
|
||||
)
|
||||
|
||||
func KindForFile(name string) string {
|
||||
switch strings.ToLower(filepath.Ext(name)) {
|
||||
case ".mkv", ".mp4", ".m4v", ".avi", ".mov", ".webm", ".ts", ".m2ts", ".wmv", ".flv", ".mpg", ".mpeg":
|
||||
return KindVideo
|
||||
case ".srt", ".ass", ".ssa", ".vtt", ".sub":
|
||||
return KindSubtitle
|
||||
case ".jpg", ".jpeg", ".png", ".webp", ".gif", ".bmp", ".tif", ".tiff", ".heic", ".avif":
|
||||
return KindImage
|
||||
default:
|
||||
return KindFile
|
||||
}
|
||||
}
|
||||
|
||||
func metadataJSON(existing string, updates map[string]any) (string, error) {
|
||||
values := make(map[string]any)
|
||||
if existing != "" {
|
||||
if err := json.Unmarshal([]byte(existing), &values); err != nil {
|
||||
return "", fmt.Errorf("%w: %v", document.ErrInvalidMetadata, err)
|
||||
}
|
||||
if values == nil {
|
||||
values = make(map[string]any)
|
||||
}
|
||||
}
|
||||
|
||||
for key, value := range updates {
|
||||
values[key] = value
|
||||
}
|
||||
|
||||
encoded, err := json.Marshal(values)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(encoded), nil
|
||||
}
|
||||
|
||||
func fileMetadata(existing, relPath string, info fs.FileInfo, probe Probe) (string, error) {
|
||||
updates := map[string]any{
|
||||
"source_path": relPath,
|
||||
"size": info.Size(),
|
||||
"mtime": info.ModTime().UnixMilli(),
|
||||
}
|
||||
if probe.Mime != "" {
|
||||
updates["mime"] = probe.Mime
|
||||
}
|
||||
for key, value := range probe.Metadata {
|
||||
updates[key] = value
|
||||
}
|
||||
|
||||
return metadataJSON(existing, updates)
|
||||
}
|
||||
|
||||
func directoryMetadata(existing, relPath string) (string, error) {
|
||||
return metadataJSON(existing, map[string]any{"source_path": relPath})
|
||||
}
|
||||
Reference in New Issue
Block a user