DRMDTool/main.go

138 lines
2.7 KiB
Go
Raw Normal View History

2024-09-03 23:06:15 +02:00
package main
import (
"flag"
"fmt"
"html/template"
"net/http"
"os"
"strings"
"sync"
"embed"
2024-09-03 23:06:15 +02:00
)
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
2024-09-06 11:29:19 +02:00
//go:embed templates
var templateFS embed.FS
2024-09-03 23:06:15 +02:00
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/*"))
2024-09-03 23:06:15 +02:00
}
func main() {
loadConfig()
2024-09-03 23:06:15 +02:00
inputFile := flag.String("f", "", "Path to the input JSON file")
flag.Parse()
if *inputFile == "" {
startWebServer()
} else {
2024-09-13 22:00:44 +02:00
items, err := parseInputFile(*inputFile)
if err != nil {
fmt.Printf("Error parsing input file: %v\n", err)
return
}
processItems(*inputFile, items)
2024-09-03 23:06:15 +02:00
}
}
func startWebServer() {
http.HandleFunc("/", handleRoot)
http.HandleFunc("/upload", handleUpload)
2024-09-13 22:00:44 +02:00
http.HandleFunc("/select", handleSelect)
http.HandleFunc("/process", handleProcess)
2024-09-03 23:06:15 +02:00
http.HandleFunc("/progress", handleProgress)
2024-09-13 22:15:20 +02:00
http.HandleFunc("/abort", handleAbort)
2024-09-03 23:06:15 +02:00
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]),
}
}
2024-09-13 22:15:20 +02:00
func handleAbort(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Query().Get("filename")
if filename == "" {
http.Error(w, "Filename is required", http.StatusBadRequest)
return
}
jobsMutex.Lock()
abortChan, exists := jobs[filename]
jobsMutex.Unlock()
if !exists {
http.Error(w, "Job not found", http.StatusNotFound)
return
}
close(abortChan)
fmt.Fprintf(w, "Abort signal sent for %s", filename)
}