vodular/scanner/scanner.go

170 lines
3.8 KiB
Go
Raw Normal View History

2026-01-28 10:48:14 +00:00
package scanner
import (
"encoding/json"
"fmt"
2026-01-28 10:48:14 +00:00
"os"
2026-01-28 12:50:11 +00:00
"path"
"strconv"
2026-01-28 10:48:14 +00:00
"strings"
"time"
"github.com/pelletier/go-toml/v2"
ffmpeg_go "github.com/u2takey/ffmpeg-go"
2026-01-28 10:48:14 +00:00
)
type (
Category struct {
Name string `toml:"name"`
Type string `toml:"type" comment:"Valid types: gaming, other (default: other)"`
Url string `toml:"url"`
2026-01-28 10:48:14 +00:00
}
Metadata struct {
Title string `toml:"title"`
Part int `toml:"part"`
Date toml.LocalDate `toml:"date"`
Tags []string `toml:"tags"`
FootageDir string `toml:"footage_dir"`
Uploaded bool `toml:"uploaded"`
Category *Category `toml:"category" comment:"(Optional) Category details, for additional credits."`
2026-01-28 10:48:14 +00:00
}
FFprobeFormat struct {
Duration float64 `json:"duration"`
Size int64 `json:"size"`
}
FFprobeOutput struct {
Format FFprobeFormat `json:"format"`
}
2026-01-28 10:48:14 +00:00
)
2026-01-28 12:50:11 +00:00
const METADATA_FILENAME = "metadata.toml"
2026-06-28 13:40:08 +01:00
func InitialiseDirectory(directory string) error {
dirInfo, err := os.Stat(directory)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("no such directory: %s", directory)
}
return fmt.Errorf("failed to open directory: %v", err)
}
if !dirInfo.IsDir() {
return fmt.Errorf("not a directory: %s", directory)
}
_, err = os.Stat(path.Join(directory, METADATA_FILENAME))
if err == nil {
return fmt.Errorf("directory already initialised: %s", directory)
}
err = WriteMetadata(directory, DefaultMetadata())
return err
}
2026-01-28 12:50:11 +00:00
func ScanSegments(directory string, extension string) ([]string, error) {
2026-01-28 10:48:14 +00:00
entries, err := os.ReadDir(directory)
if err != nil {
return nil, err
}
files := []string{}
for _, item := range entries {
if item.IsDir() { continue }
if strings.HasPrefix(item.Name(), ".") { continue }
2026-01-28 12:50:11 +00:00
if !strings.HasSuffix(item.Name(), "." + extension) { continue }
if strings.HasSuffix(item.Name(), "-fullvod." + extension) { continue }
2026-01-28 10:48:14 +00:00
files = append(files, item.Name())
}
return files, nil
}
func ScanThumbnail(directory string) (string, int64, error) {
thumbnailPath := path.Join(directory, "thumbnail.png")
stat, err := os.Stat(thumbnailPath)
if err != nil {
2026-06-23 19:07:51 +01:00
if os.IsNotExist(err) {
return "", 0, err
}
return "", 0, err
}
if stat.IsDir() {
return "", 0, fmt.Errorf("thumbnail.png is a directory")
}
return thumbnailPath, stat.Size(), nil
}
func ProbeSegment(filename string) (*FFprobeOutput, error) {
out, err := ffmpeg_go.Probe(filename)
if err != nil { return nil, err }
type (
RawFFprobeFormat struct {
// these being strings upsets me immensely
Duration string `json:"duration"`
Size string `json:"size"`
}
RawFFprobeOutput struct {
Format RawFFprobeFormat `json:"format"`
}
)
probe := RawFFprobeOutput{}
err = json.Unmarshal([]byte(out), &probe)
if err != nil { return nil, err }
duration, err := strconv.ParseFloat(probe.Format.Duration, 64)
if err != nil { return nil, err }
size, err := strconv.ParseInt(probe.Format.Size, 10, 0)
if err != nil { return nil, err }
return &FFprobeOutput{
Format: FFprobeFormat{
Duration: duration,
Size: size,
},
}, nil
}
2026-01-28 12:50:11 +00:00
func ReadMetadata(directory string) (*Metadata, error) {
metadata := &Metadata{}
file, err := os.OpenFile(
path.Join(directory, METADATA_FILENAME),
os.O_RDONLY, os.ModePerm,
)
if err != nil { return nil, err }
2026-01-28 10:48:14 +00:00
2026-01-28 12:50:11 +00:00
err = toml.NewDecoder(file).Decode(metadata)
if err != nil { return nil, err }
2026-01-28 10:48:14 +00:00
2026-01-28 12:50:11 +00:00
return metadata, nil
2026-01-28 10:48:14 +00:00
}
2026-01-28 12:50:11 +00:00
func WriteMetadata(directory string, metadata *Metadata) (error) {
data, err := toml.Marshal(metadata)
2026-01-28 12:50:11 +00:00
if err != nil { return err }
err = os.WriteFile(path.Join(directory, METADATA_FILENAME), data, 0644)
2026-01-28 12:50:11 +00:00
return err
2026-01-28 10:48:14 +00:00
}
2026-01-28 12:50:11 +00:00
func DefaultMetadata() *Metadata {
return &Metadata{
2026-01-28 10:48:14 +00:00
Title: "Untitled Stream",
Date: toml.LocalDate{
Year: time.Now().Year(),
Month: int(time.Now().Month()),
Day: time.Now().Day(),
},
2026-01-28 10:48:14 +00:00
Part: 0,
Category: &Category{
Name: "Something",
Type: "",
Url: "",
},
}
}