Compare commits

..

No commits in common. "551e53ad6389c38f094fa4e6f8e2fc079056a11a" and "64a6eb20a0865411bc6abdd775a106130eececc0" have entirely different histories.

4 changed files with 70 additions and 137 deletions

View File

@ -5,10 +5,8 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
type ProgressInfo struct { type ProgressInfo struct {
@ -52,101 +50,53 @@ func handleRoot(w http.ResponseWriter, r *http.Request) {
} }
func handleUpload(w http.ResponseWriter, r *http.Request) { func handleUpload(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(32 << 20) file, header, err := r.FormFile("file")
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
defer file.Close()
files := r.MultipartForm.File["files"] tempFile, err := os.CreateTemp(uploadDir, header.Filename)
if len(files) == 0 { if err != nil {
http.Error(w, "No files uploaded", http.StatusBadRequest) http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer tempFile.Close()
_, err = io.Copy(tempFile, file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
uploadedFiles := []string{} tempFilename := filepath.Base(tempFile.Name())
for _, fileHeader := range files { _, err = parseInputFile(tempFile.Name())
file, err := fileHeader.Open() if err != nil {
if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
dst, err := os.Create(filepath.Join(uploadDir, fileHeader.Filename))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dst.Close()
_, err = io.Copy(dst, file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
uploadedFiles = append(uploadedFiles, fileHeader.Filename)
_, err = parseInputFile(dst.Name())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
validFiles := []string{}
for _, file := range uploadedFiles {
if file != "" {
validFiles = append(validFiles, file)
}
}
if len(validFiles) == 0 {
http.Error(w, "No valid files were uploaded", http.StatusBadRequest)
return return
} }
http.Redirect(w, r, "/select?files="+url.QueryEscape(strings.Join(validFiles, ",")), http.StatusSeeOther) http.Redirect(w, r, "/select?filename="+tempFilename, http.StatusSeeOther)
} }
func handleSelect(w http.ResponseWriter, r *http.Request) { func handleSelect(w http.ResponseWriter, r *http.Request) {
filesParam := r.URL.Query().Get("files") filename := r.URL.Query().Get("filename")
filenames := strings.Split(filesParam, ",") items, err := parseInputFile(filepath.Join(uploadDir, filename))
if err != nil {
allItems := make(map[string]map[string][]Item) http.Error(w, err.Error(), http.StatusInternalServerError)
for _, filename := range filenames {
if filename == "" {
continue
}
fullPath := filepath.Join(uploadDir, filename)
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
continue
}
items, err := parseInputFile(fullPath)
if err != nil {
continue
}
groupedItems := groupItemsBySeason(items)
allItems[filename] = groupedItems
}
if len(allItems) == 0 {
http.Error(w, "No valid files were processed", http.StatusBadRequest)
return return
} }
err := templates.ExecuteTemplate(w, "select", struct { groupedItems := groupItemsBySeason(items)
Filenames string
AllItems map[string]map[string][]Item err = templates.ExecuteTemplate(w, "select", struct {
Filename string
Items map[string][]Item
}{ }{
Filenames: filesParam, Filename: filename,
AllItems: allItems, Items: groupedItems,
}) })
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
@ -154,47 +104,36 @@ func handleSelect(w http.ResponseWriter, r *http.Request) {
} }
func handleProcess(w http.ResponseWriter, r *http.Request) { func handleProcess(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil { filename := r.FormValue("filename")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
selectedItems := r.Form["items"] selectedItems := r.Form["items"]
if len(selectedItems) == 0 {
http.Error(w, "No items selected", http.StatusBadRequest) items, err := parseInputFile(filepath.Join(uploadDir, filename))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
itemsByFile := make(map[string][]string) filteredItems := filterSelectedItems(items, selectedItems)
for _, item := range selectedItems {
parts := strings.SplitN(item, ":", 2)
if len(parts) != 2 {
continue
}
filename, itemName := parts[0], parts[1]
itemsByFile[filename] = append(itemsByFile[filename], itemName)
}
for filename, items := range itemsByFile { go func() {
fullPath := filepath.Join(uploadDir, filename) err := processItems(filename, filteredItems)
allItems, err := parseInputFile(fullPath)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) fmt.Printf("Error processing file: %v\n", err)
return
} }
selectedItems := filterSelectedItems(allItems, items) os.Remove(filepath.Join(uploadDir, filename))
go processItems(filename, selectedItems) }()
}
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/progress?filename="+filename, http.StatusSeeOther)
} }
func handleProgress(w http.ResponseWriter, r *http.Request) { func handleProgress(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Query().Get("filename") filename := r.URL.Query().Get("filename")
fmt.Printf("Handling progress request for filename: %s\n", filename)
if r.Header.Get("Accept") == "application/json" { if r.Header.Get("Accept") == "application/json" {
progressInfo := getProgress(filename) progressInfo := getProgress(filename)
fmt.Printf("Progress info for %s: %+v\n", filename, progressInfo)
if progressInfo == nil { if progressInfo == nil {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
@ -205,6 +144,7 @@ func handleProgress(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(progressInfo) err := json.NewEncoder(w).Encode(progressInfo)
if err != nil { if err != nil {
fmt.Printf("Error encoding progress info: %v\n", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
@ -266,6 +206,7 @@ func handleResume(w http.ResponseWriter, r *http.Request) {
jobInfo.Paused = false jobInfo.Paused = false
jobInfo.ResumeChan <- struct{}{} jobInfo.ResumeChan <- struct{}{}
// Update the progress information
progressMutex.Lock() progressMutex.Lock()
if progressInfo, ok := progress[filename]; ok { if progressInfo, ok := progress[filename]; ok {
progressInfo.Paused = false progressInfo.Paused = false
@ -350,4 +291,5 @@ func updateProgress(filename string, value float64, currentFile string) {
Paused: paused, Paused: paused,
} }
} }
fmt.Printf("Progress updated for %s: %.2f%%, Current file: %s, Paused: %v\n", filename, value, currentFile, paused)
} }

View File

@ -112,7 +112,7 @@
<body> <body>
<h1>Simple Downloader</h1> <h1>Simple Downloader</h1>
<form action="/upload" method="post" enctype="multipart/form-data"> <form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="files" accept=".drmd" multiple> <input type="file" name="file" accept=".drmd">
<input type="submit" value="Upload and Process"> <input type="submit" value="Upload and Process">
</form> </form>
<h2>Currently Running Jobs</h2> <h2>Currently Running Jobs</h2>

View File

@ -55,25 +55,22 @@
<body> <body>
<h1>Select Items to Download</h1> <h1>Select Items to Download</h1>
<form action="/process" method="post"> <form action="/process" method="post">
<input type="hidden" name="filenames" value="{{.Filenames}}"> <input type="hidden" name="filename" value="{{.Filename}}">
{{range $filename, $fileItems := .AllItems}} {{range $season, $items := .Items}}
<h2>{{$filename}}</h2> <div class="season">
{{range $season, $items := $fileItems}} <div class="season-title">
<div class="season"> <input type="checkbox" class="season-checkbox" id="season-{{$season}}" checked onchange="toggleSeason('{{$season}}')">
<div class="season-title"> <label for="season-{{$season}}">{{$season}}</label>
<input type="checkbox" class="season-checkbox" id="season-{{$filename}}-{{$season}}" checked onchange="toggleSeason('{{$filename}}-{{$season}}')">
<label for="season-{{$filename}}-{{$season}}">{{$season}}</label>
</div>
{{range $item := $items}}
<div class="item">
<label>
<input type="checkbox" name="items" value="{{$filename}}:{{$item.Filename}}" checked class="episode-{{$filename}}-{{$season}}">
{{$item.Filename}}
</label>
</div>
{{end}}
</div> </div>
{{end}} {{range $item := $items}}
<div class="item">
<label>
<input type="checkbox" name="items" value="{{$item.Filename}}" checked class="episode-{{$season}}">
{{$item.Filename}}
</label>
</div>
{{end}}
</div>
{{end}} {{end}}
<div> <div>
<button type="button" onclick="selectAll(true)">Select All</button> <button type="button" onclick="selectAll(true)">Select All</button>

View File

@ -138,24 +138,16 @@ func findOrCreateSegmentTimeline(adaptationSet *etree.Element) *etree.Element {
return segmentTemplate.CreateElement("SegmentTimeline") return segmentTemplate.CreateElement("SegmentTimeline")
} }
func parseInputFile(filename string) ([]Item, error) { func parseInputFile(inputFile string) ([]Item, error) {
fileInfo, err := os.Stat(filename) jsonFile, err := os.Open(inputFile)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("error opening file %s: %v", inputFile, err)
}
if fileInfo.IsDir() {
return nil, fmt.Errorf("%s is a directory", filename)
} }
defer jsonFile.Close()
file, err := os.Open(filename) byteValue, err := io.ReadAll(jsonFile)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("error reading file %s: %v", inputFile, err)
}
defer file.Close()
byteValue, err := io.ReadAll(file)
if err != nil {
return nil, err
} }
byteValue = removeBOM(byteValue) byteValue = removeBOM(byteValue)
@ -163,7 +155,7 @@ func parseInputFile(filename string) ([]Item, error) {
var items Items var items Items
err = json.Unmarshal(byteValue, &items) err = json.Unmarshal(byteValue, &items)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("error unmarshaling JSON: %v", err)
} }
return items.Items, nil return items.Items, nil
@ -225,6 +217,7 @@ func processItems(filename string, items []Item) error {
select { select {
case <-jobInfo.ResumeChan: case <-jobInfo.ResumeChan:
jobInfo.Paused = false jobInfo.Paused = false
fmt.Printf("Resuming download for %s\n", filename)
case <-jobInfo.AbortChan: case <-jobInfo.AbortChan:
updateProgress(filename, 100, "Aborted") updateProgress(filename, 100, "Aborted")
return fmt.Errorf("download aborted") return fmt.Errorf("download aborted")
@ -238,6 +231,7 @@ func processItems(filename string, items []Item) error {
i-- i--
continue continue
} }
fmt.Printf("Error downloading file: %v\n", err)
} }
} }
} }