package main import ( "flag" "fmt" "html/template" "net/http" "os" "strings" "sync" "embed" ) var logger *Logger 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" var templates *template.Template //go:embed templates var templateFS embed.FS var globalSpeedLimit string 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/*")) logger = NewLogger("") } func main() { loadConfig() inputFile := flag.String("f", "", "Path to the input JSON file") flag.Parse() if *inputFile == "" { go watchFolder() startWebServer() } else { items, err := parseInputFile(*inputFile) if err != nil { logger.LogError("Main", fmt.Sprintf("Error parsing input file: %v", err)) return } processItems(*inputFile, items) } http.HandleFunc("/set-speed-limit", handleSetSpeedLimit) } 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) http.HandleFunc("/clear-completed", handleClearCompleted) http.HandleFunc("/ws", handleWebSocket) http.HandleFunc("/set-speed-limit", handleSetSpeedLimit) logger.LogInfo("Main", "Starting web server on http://0.0.0.0:8080") http.ListenAndServe(":8080", nil) } 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]), } }