package main
import (
"flag"
"fmt"
"html/template"
"net/http"
"os"
"strings"
"sync"
"embed"
)
type Item struct {
MPD string
Keys string
Filename string
Description string
Subtitles string
Poster string
Metadata string
}
type Items struct {
Items []Item
}
type Metadata struct {
Title string
Type string
Season string
}
var progressMutex sync.Mutex
var progress = make(map[string]*ProgressInfo)
const uploadDir = "uploads"
type ProgressInfo struct {
Percentage float64
CurrentFile string
}
var templates *template.Template
//go:embed templates
var templateFS embed.FS
func init() {
if err := os.MkdirAll(uploadDir, 0755); err != nil {
fmt.Printf("Error creating upload directory: %v\n", err)
}
templates = template.Must(template.ParseFS(templateFS, "templates/*"))
}
func main() {
loadConfig()
inputFile := flag.String("f", "", "Path to the input JSON file")
flag.Parse()
if *inputFile == "" {
startWebServer()
} else {
items, err := parseInputFile(*inputFile)
if err != nil {
fmt.Printf("Error parsing input file: %v\n", err)
return
}
processItems(*inputFile, items)
}
}
func startWebServer() {
http.HandleFunc("/", handleRoot)
http.HandleFunc("/upload", handleUpload)
http.HandleFunc("/select", handleSelect)
http.HandleFunc("/process", handleProcess)
http.HandleFunc("/progress", handleProgress)
http.HandleFunc("/abort", handleAbort)
http.HandleFunc("/pause", handlePause)
http.HandleFunc("/resume", handleResume)
fmt.Println("Starting web server on http://0.0.0.0:8080")
http.ListenAndServe(":8080", nil)
}
func updateProgress(filename string, value float64, currentFile string) {
progressMutex.Lock()
defer progressMutex.Unlock()
progress[filename] = &ProgressInfo{
Percentage: value,
CurrentFile: currentFile,
}
fmt.Printf("Progress updated for %s: %.2f%%, Current file: %s\n", filename, value, currentFile)
}
func getProgress(filename string) *ProgressInfo {
progressMutex.Lock()
defer progressMutex.Unlock()
return progress[filename]
}
func getKeys(keys string) []string {
return strings.Split(keys, ",")
}
func parseMetadata(metadata string) Metadata {
parts := strings.Split(metadata, ";")
if len(parts) != 3 {
return Metadata{}
}
return Metadata{
Title: strings.TrimSpace(parts[0]),
Type: strings.TrimSpace(parts[1]),
Season: "S" + strings.TrimSpace(parts[2]),
}
}