170 lines
3.8 KiB
Go
170 lines
3.8 KiB
Go
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) {
|
|
return Probe{Mime: mimeForPath(path)}, nil
|
|
}
|
|
|
|
func mimeForPath(path string) string {
|
|
mimeType := mime.TypeByExtension(strings.ToLower(filepath.Ext(path)))
|
|
if mimeType == "" {
|
|
mimeType = "application/octet-stream"
|
|
}
|
|
return mimeType
|
|
}
|
|
|
|
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
|
|
Pruned 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 hasSourcePath(metadata, root string) bool {
|
|
var values map[string]any
|
|
if err := json.Unmarshal([]byte(metadata), &values); err != nil {
|
|
return false
|
|
}
|
|
|
|
sourceRoot, _ := values["source_root"].(string)
|
|
sourcePath, ok := values["source_path"].(string)
|
|
return ok && sourcePath != "" && sourceRoot == root
|
|
}
|
|
|
|
func fileMetadata(existing, root, relPath string, info fs.FileInfo, probe Probe) (string, error) {
|
|
updates := map[string]any{
|
|
"source_root": root,
|
|
"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, root, relPath string) (string, error) {
|
|
return metadataJSON(existing, map[string]any{
|
|
"source_root": root,
|
|
"source_path": relPath,
|
|
})
|
|
}
|