Compare commits
	
		
			15 Commits
		
	
	
		
			v1.0.4
			...
			7159bae9f7
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						7159bae9f7
	
				 | 
					
					
						|||
| 5b6e1e6b01 | |||
| 
						
						
							
						
						4b03c7c59b
	
				 | 
					
					
						|||
| 
						
						
							
						
						1dd8aa594d
	
				 | 
					
					
						|||
| 
						
						
							
						
						72889d3083
	
				 | 
					
					
						|||
| 
						
						
							
						
						bd87baa40a
	
				 | 
					
					
						|||
| 
						
						
							
						
						2f738413f3
	
				 | 
					
					
						|||
| 
						
						
							
						
						f6a447d7f4
	
				 | 
					
					
						|||
| 551e53ad63 | |||
| 
						
						
							
						
						707de8fcf1
	
				 | 
					
					
						|||
| 64a6eb20a0 | |||
| 
						
						
							
						
						67b17c1df7
	
				 | 
					
					
						|||
| 
						
						
							
						
						142c09e624
	
				 | 
					
					
						|||
| 0b3797dc19 | |||
| d4dae21d8f | 
							
								
								
									
										40
									
								
								Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								Makefile
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,40 @@
 | 
			
		||||
GOCMD=go
 | 
			
		||||
GOBUILD=$(GOCMD) build
 | 
			
		||||
GOCLEAN=$(GOCMD) clean
 | 
			
		||||
GOTEST=$(GOCMD) test
 | 
			
		||||
GOGET=$(GOCMD) get
 | 
			
		||||
BINARY_NAME=drmdtool
 | 
			
		||||
SRC_DIR=src
 | 
			
		||||
 | 
			
		||||
all: test build
 | 
			
		||||
 | 
			
		||||
build:
 | 
			
		||||
	cd $(SRC_DIR) && $(GOBUILD) -o ../$(BINARY_NAME) -v
 | 
			
		||||
 | 
			
		||||
test:
 | 
			
		||||
	cd $(SRC_DIR) && $(GOTEST) -v ./...
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
	$(GOCLEAN)
 | 
			
		||||
	rm -f $(BINARY_NAME)
 | 
			
		||||
 | 
			
		||||
run:
 | 
			
		||||
	cd $(SRC_DIR) && $(GOBUILD) -o ../$(BINARY_NAME) -v
 | 
			
		||||
	./$(BINARY_NAME)
 | 
			
		||||
 | 
			
		||||
deps:
 | 
			
		||||
	$(GOGET) github.com/BurntSushi/toml
 | 
			
		||||
	$(GOGET) github.com/beevik/etree
 | 
			
		||||
	$(GOGET) github.com/asticode/go-astisub
 | 
			
		||||
 | 
			
		||||
# Cross compilation
 | 
			
		||||
build-linux:
 | 
			
		||||
	cd $(SRC_DIR) && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o ../$(BINARY_NAME)_linux -v
 | 
			
		||||
 | 
			
		||||
build-windows:
 | 
			
		||||
	cd $(SRC_DIR) && CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o ../$(BINARY_NAME).exe -v
 | 
			
		||||
 | 
			
		||||
build-mac:
 | 
			
		||||
	cd $(SRC_DIR) && CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -o ../$(BINARY_NAME)_mac -v
 | 
			
		||||
 | 
			
		||||
.PHONY: all build test clean run deps build-linux build-windows build-mac
 | 
			
		||||
							
								
								
									
										20
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								README.md
									
									
									
									
									
								
							@@ -9,6 +9,7 @@ Create a `config.toml` file in the same directory as the drmdtool executable:
 | 
			
		||||
```toml
 | 
			
		||||
BaseDir = "/path/to/save/downloads"
 | 
			
		||||
Format = "mkv"
 | 
			
		||||
TempBaseDir = "/tmp/nre"
 | 
			
		||||
 | 
			
		||||
[N_m3u8DL-RE]
 | 
			
		||||
Path = "/path/to/N_m3u8DL-RE"
 | 
			
		||||
@@ -38,8 +39,17 @@ To process a file directly from the command line:
 | 
			
		||||
 | 
			
		||||
This will download the file and save it in the base directory specified in the config.
 | 
			
		||||
 | 
			
		||||
## TODO
 | 
			
		||||
- ~~Filename Sanitation (Makes new directory on /... oops)~~
 | 
			
		||||
- ~~GoPlay Fix~~
 | 
			
		||||
- Windows?
 | 
			
		||||
- Proper UI?
 | 
			
		||||
 | 
			
		||||
# Previews
 | 
			
		||||
 | 
			
		||||
## Index Page
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
## Select Page
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
## Progress Page
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										234
									
								
								handlers.go
									
									
									
									
									
								
							
							
						
						
									
										234
									
								
								handlers.go
									
									
									
									
									
								
							@@ -1,234 +0,0 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func handleRoot(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	if r.URL.Path != "/" {
 | 
			
		||||
		http.NotFound(w, r)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	progressMutex.Lock()
 | 
			
		||||
	jobs := make(map[string]*ProgressInfo)
 | 
			
		||||
	for k, v := range progress {
 | 
			
		||||
		jobs[k] = v
 | 
			
		||||
	}
 | 
			
		||||
	progressMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	err := templates.ExecuteTemplate(w, "index", struct{ Jobs map[string]*ProgressInfo }{jobs})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleUpload(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	file, header, err := r.FormFile("file")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusBadRequest)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	defer file.Close()
 | 
			
		||||
 | 
			
		||||
	tempFile, err := os.CreateTemp(uploadDir, header.Filename)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		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
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tempFilename := filepath.Base(tempFile.Name())
 | 
			
		||||
 | 
			
		||||
	_, err = parseInputFile(tempFile.Name())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	http.Redirect(w, r, "/select?filename="+tempFilename, http.StatusSeeOther)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleSelect(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	filename := r.URL.Query().Get("filename")
 | 
			
		||||
	items, err := parseInputFile(filepath.Join(uploadDir, filename))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	groupedItems := groupItemsBySeason(items)
 | 
			
		||||
 | 
			
		||||
	err = templates.ExecuteTemplate(w, "select", struct {
 | 
			
		||||
		Filename string
 | 
			
		||||
		Items    map[string][]Item
 | 
			
		||||
	}{
 | 
			
		||||
		Filename: filename,
 | 
			
		||||
		Items:    groupedItems,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleProcess(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	filename := r.FormValue("filename")
 | 
			
		||||
	selectedItems := r.Form["items"]
 | 
			
		||||
 | 
			
		||||
	items, err := parseInputFile(filepath.Join(uploadDir, filename))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	filteredItems := filterSelectedItems(items, selectedItems)
 | 
			
		||||
 | 
			
		||||
	go func() {
 | 
			
		||||
		err := processItems(filename, filteredItems)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Printf("Error processing file: %v\n", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		os.Remove(filepath.Join(uploadDir, filename))
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	http.Redirect(w, r, "/progress?filename="+filename, http.StatusSeeOther)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleProgress(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	filename := r.URL.Query().Get("filename")
 | 
			
		||||
	fmt.Printf("Handling progress request for filename: %s\n", filename)
 | 
			
		||||
 | 
			
		||||
	if r.Header.Get("Accept") == "application/json" {
 | 
			
		||||
		progressInfo := getProgress(filename)
 | 
			
		||||
		fmt.Printf("Progress info for %s: %+v\n", filename, progressInfo)
 | 
			
		||||
 | 
			
		||||
		if progressInfo == nil {
 | 
			
		||||
			w.WriteHeader(http.StatusNotFound)
 | 
			
		||||
			json.NewEncoder(w).Encode(map[string]string{"error": "No progress information found"})
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
		err := json.NewEncoder(w).Encode(progressInfo)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Printf("Error encoding progress info: %v\n", err)
 | 
			
		||||
			http.Error(w, "Internal Server Error", http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err := templates.ExecuteTemplate(w, "progress", struct{ Filename string }{filename})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handlePause(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()
 | 
			
		||||
	jobInfo, exists := jobs[filename]
 | 
			
		||||
	jobsMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	if !exists {
 | 
			
		||||
		http.Error(w, "Job not found", http.StatusNotFound)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	jobInfo.Paused = true
 | 
			
		||||
	if jobInfo.Cmd != nil && jobInfo.Cmd.Process != nil {
 | 
			
		||||
		jobInfo.Cmd.Process.Kill()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Fprintf(w, "Pause signal sent for %s", filename)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleResume(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()
 | 
			
		||||
	jobInfo, exists := jobs[filename]
 | 
			
		||||
	jobsMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	if !exists {
 | 
			
		||||
		http.Error(w, "Job not found", http.StatusNotFound)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	jobInfo.Paused = false
 | 
			
		||||
	jobInfo.ResumeChan <- struct{}{}
 | 
			
		||||
 | 
			
		||||
	fmt.Fprintf(w, "Resume signal sent for %s", filename)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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()
 | 
			
		||||
	jobInfo, exists := jobs[filename]
 | 
			
		||||
	jobsMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	if !exists {
 | 
			
		||||
		http.Error(w, "Job not found", http.StatusNotFound)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	close(jobInfo.AbortChan)
 | 
			
		||||
	if jobInfo.Cmd != nil && jobInfo.Cmd.Process != nil {
 | 
			
		||||
		jobInfo.Cmd.Process.Kill()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if jobInfo.TempDir != "" {
 | 
			
		||||
		os.RemoveAll(jobInfo.TempDir)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Fprintf(w, "Abort signal sent for %s", filename)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleClearCompleted(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	if r.Method != http.MethodPost {
 | 
			
		||||
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	clearCompletedJobs()
 | 
			
		||||
 | 
			
		||||
	w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
	json.NewEncoder(w).Encode(map[string]bool{"success": true})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func clearCompletedJobs() {
 | 
			
		||||
	progressMutex.Lock()
 | 
			
		||||
	defer progressMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	for filename, info := range progress {
 | 
			
		||||
		if info.Percentage >= 100 {
 | 
			
		||||
			delete(progress, filename)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								images/index.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								images/index.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 33 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								images/progress.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								images/progress.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 29 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								images/select.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								images/select.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 41 KiB  | 
@@ -133,7 +133,6 @@ func downloadFile(item Item, jobInfo *JobInfo) error {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Download completed successfully")
 | 
			
		||||
	os.RemoveAll(tempDir)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										356
									
								
								src/handlers.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										356
									
								
								src/handlers.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,356 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ProgressInfo struct {
 | 
			
		||||
	Percentage  float64
 | 
			
		||||
	CurrentFile string
 | 
			
		||||
	Paused      bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleRoot(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	progressMutex.Lock()
 | 
			
		||||
	defer progressMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	jobsInfo := make(map[string]struct {
 | 
			
		||||
		Percentage  float64
 | 
			
		||||
		CurrentFile string
 | 
			
		||||
		Paused      bool
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	for filename, info := range progress {
 | 
			
		||||
		jobsInfo[filename] = struct {
 | 
			
		||||
			Percentage  float64
 | 
			
		||||
			CurrentFile string
 | 
			
		||||
			Paused      bool
 | 
			
		||||
		}{
 | 
			
		||||
			Percentage:  info.Percentage,
 | 
			
		||||
			CurrentFile: info.CurrentFile,
 | 
			
		||||
			Paused:      info.Paused,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err := templates.ExecuteTemplate(w, "index", struct {
 | 
			
		||||
		Jobs map[string]struct {
 | 
			
		||||
			Percentage  float64
 | 
			
		||||
			CurrentFile string
 | 
			
		||||
			Paused      bool
 | 
			
		||||
		}
 | 
			
		||||
	}{jobsInfo})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleUpload(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	err := r.ParseMultipartForm(32 << 20)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusBadRequest)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	files := r.MultipartForm.File["files"]
 | 
			
		||||
	if len(files) == 0 {
 | 
			
		||||
		http.Error(w, "No files uploaded", http.StatusBadRequest)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	uploadedFiles := []string{}
 | 
			
		||||
 | 
			
		||||
	for _, fileHeader := range files {
 | 
			
		||||
		file, err := fileHeader.Open()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		defer file.Close()
 | 
			
		||||
 | 
			
		||||
		tempFile, err := os.CreateTemp(uploadDir, fileHeader.Filename)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			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
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		uploadedFiles = append(uploadedFiles, filepath.Base(tempFile.Name()))
 | 
			
		||||
 | 
			
		||||
		_, err = parseInputFile(tempFile.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
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	http.Redirect(w, r, "/select?files="+url.QueryEscape(strings.Join(validFiles, ",")), http.StatusSeeOther)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleSelect(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	filesParam := r.URL.Query().Get("files")
 | 
			
		||||
	filenames := strings.Split(filesParam, ",")
 | 
			
		||||
 | 
			
		||||
	allItems := make(map[string]map[string][]Item)
 | 
			
		||||
 | 
			
		||||
	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
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		sortItems(items)
 | 
			
		||||
 | 
			
		||||
		groupedItems := groupItemsBySeason(items)
 | 
			
		||||
		allItems[filename] = groupedItems
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(allItems) == 0 {
 | 
			
		||||
		http.Error(w, "No valid files were processed", http.StatusBadRequest)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err := templates.ExecuteTemplate(w, "select", struct {
 | 
			
		||||
		Filenames string
 | 
			
		||||
		AllItems  map[string]map[string][]Item
 | 
			
		||||
	}{
 | 
			
		||||
		Filenames: filesParam,
 | 
			
		||||
		AllItems:  allItems,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleProcess(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	if err := r.ParseForm(); err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusBadRequest)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	selectedItems := r.Form["items"]
 | 
			
		||||
	if len(selectedItems) == 0 {
 | 
			
		||||
		http.Error(w, "No items selected", http.StatusBadRequest)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	itemsByFile := make(map[string][]string)
 | 
			
		||||
	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 {
 | 
			
		||||
		fullPath := filepath.Join(uploadDir, filename)
 | 
			
		||||
		allItems, err := parseInputFile(fullPath)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		selectedItems := filterSelectedItems(allItems, items)
 | 
			
		||||
		sortItems(selectedItems)
 | 
			
		||||
		go processItems(filename, selectedItems)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	http.Redirect(w, r, "/", http.StatusSeeOther)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleProgress(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	filename := r.URL.Query().Get("filename")
 | 
			
		||||
 | 
			
		||||
	if r.Header.Get("Accept") == "application/json" {
 | 
			
		||||
		progressInfo := getProgress(filename)
 | 
			
		||||
 | 
			
		||||
		if progressInfo == nil {
 | 
			
		||||
			w.WriteHeader(http.StatusNotFound)
 | 
			
		||||
			json.NewEncoder(w).Encode(map[string]string{"error": "No progress information found"})
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
		err := json.NewEncoder(w).Encode(progressInfo)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			http.Error(w, "Internal Server Error", http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err := templates.ExecuteTemplate(w, "progress", struct{ Filename string }{filename})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handlePause(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()
 | 
			
		||||
	jobInfo, exists := jobs[filename]
 | 
			
		||||
	jobsMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	if !exists {
 | 
			
		||||
		http.Error(w, "Job not found", http.StatusNotFound)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	jobInfo.Paused = true
 | 
			
		||||
	if jobInfo.Cmd != nil && jobInfo.Cmd.Process != nil {
 | 
			
		||||
		jobInfo.Cmd.Process.Kill()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	progressMutex.Lock()
 | 
			
		||||
	if progressInfo, ok := progress[filename]; ok {
 | 
			
		||||
		progressInfo.Paused = true
 | 
			
		||||
	}
 | 
			
		||||
	progressMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	fmt.Fprintf(w, "Pause signal sent for %s", filename)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleResume(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()
 | 
			
		||||
	jobInfo, exists := jobs[filename]
 | 
			
		||||
	jobsMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	if !exists {
 | 
			
		||||
		http.Error(w, "Job not found", http.StatusNotFound)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	jobInfo.Paused = false
 | 
			
		||||
	jobInfo.ResumeChan <- struct{}{}
 | 
			
		||||
 | 
			
		||||
	progressMutex.Lock()
 | 
			
		||||
	if progressInfo, ok := progress[filename]; ok {
 | 
			
		||||
		progressInfo.Paused = false
 | 
			
		||||
	}
 | 
			
		||||
	progressMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	fmt.Fprintf(w, "Resume signal sent for %s", filename)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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()
 | 
			
		||||
	jobInfo, exists := jobs[filename]
 | 
			
		||||
	jobsMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	if !exists {
 | 
			
		||||
		http.Error(w, "Job not found", http.StatusNotFound)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	close(jobInfo.AbortChan)
 | 
			
		||||
	if jobInfo.Cmd != nil && jobInfo.Cmd.Process != nil {
 | 
			
		||||
		jobInfo.Cmd.Process.Kill()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if jobInfo.TempDir != "" {
 | 
			
		||||
		os.RemoveAll(jobInfo.TempDir)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Fprintf(w, "Abort signal sent for %s", filename)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleClearCompleted(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	if r.Method != http.MethodPost {
 | 
			
		||||
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	clearCompletedJobs()
 | 
			
		||||
 | 
			
		||||
	w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
	json.NewEncoder(w).Encode(map[string]bool{"success": true})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func clearCompletedJobs() {
 | 
			
		||||
	progressMutex.Lock()
 | 
			
		||||
	defer progressMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	for filename, info := range progress {
 | 
			
		||||
		if info.Percentage >= 100 {
 | 
			
		||||
			delete(progress, filename)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func updateProgress(filename string, value float64, currentFile string) {
 | 
			
		||||
	progressMutex.Lock()
 | 
			
		||||
	defer progressMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	jobsMutex.Lock()
 | 
			
		||||
	jobInfo, exists := jobs[filename]
 | 
			
		||||
	jobsMutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	paused := false
 | 
			
		||||
	if exists {
 | 
			
		||||
		paused = jobInfo.Paused
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if existingProgress, ok := progress[filename]; ok {
 | 
			
		||||
		existingProgress.Percentage = value
 | 
			
		||||
		existingProgress.CurrentFile = currentFile
 | 
			
		||||
		existingProgress.Paused = paused
 | 
			
		||||
	} else {
 | 
			
		||||
		progress[filename] = &ProgressInfo{
 | 
			
		||||
			Percentage:  value,
 | 
			
		||||
			CurrentFile: currentFile,
 | 
			
		||||
			Paused:      paused,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -37,11 +37,6 @@ var progress = make(map[string]*ProgressInfo)
 | 
			
		||||
 | 
			
		||||
const uploadDir = "uploads"
 | 
			
		||||
 | 
			
		||||
type ProgressInfo struct {
 | 
			
		||||
	Percentage  float64
 | 
			
		||||
	CurrentFile string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var templates *template.Template
 | 
			
		||||
 | 
			
		||||
//go:embed templates
 | 
			
		||||
@@ -87,16 +82,6 @@ func startWebServer() {
 | 
			
		||||
	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()
 | 
			
		||||
							
								
								
									
										125
									
								
								src/main_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										125
									
								
								src/main_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,125 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"os"
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestSanitizeFilename(t *testing.T) {
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		input    string
 | 
			
		||||
		expected string
 | 
			
		||||
	}{
 | 
			
		||||
		{"file:name.mp4", "file_name.mp4"},
 | 
			
		||||
		{"file/name.mp4", "file_name.mp4"},
 | 
			
		||||
		{"file\\name.mp4", "file_name.mp4"},
 | 
			
		||||
		{"file?name.mp4", "file_name.mp4"},
 | 
			
		||||
		{"file*name.mp4", "file_name.mp4"},
 | 
			
		||||
		{"file<name>.mp4", "file_name_.mp4"},
 | 
			
		||||
		{".hidden", "hidden"},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, test := range tests {
 | 
			
		||||
		result := sanitizeFilename(test.input)
 | 
			
		||||
		if result != test.expected {
 | 
			
		||||
			t.Errorf("sanitizeFilename(%q) = %q, want %q", test.input, result, test.expected)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestIsValidURL(t *testing.T) {
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		input    string
 | 
			
		||||
		expected bool
 | 
			
		||||
	}{
 | 
			
		||||
		{"https://example.com", true},
 | 
			
		||||
		{"http://example.com", true},
 | 
			
		||||
		{"ftp://example.com", true},
 | 
			
		||||
		{"not a url", false},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, test := range tests {
 | 
			
		||||
		result := isValidURL(test.input)
 | 
			
		||||
		if result != test.expected {
 | 
			
		||||
			t.Errorf("isValidURL(%q) = %v, want %v", test.input, result, test.expected)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestParseMetadata(t *testing.T) {
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		input    string
 | 
			
		||||
		expected Metadata
 | 
			
		||||
	}{
 | 
			
		||||
		{"Show Title; serie; 01", Metadata{Title: "Show Title", Type: "serie", Season: "S01"}},
 | 
			
		||||
		{"Movie Title; movie; ", Metadata{Title: "Movie Title", Type: "movie", Season: "S"}},
 | 
			
		||||
		{"Invalid Metadata", Metadata{}},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, test := range tests {
 | 
			
		||||
		result := parseMetadata(test.input)
 | 
			
		||||
		if !reflect.DeepEqual(result, test.expected) {
 | 
			
		||||
			t.Errorf("parseMetadata(%q) = %v, want %v", test.input, result, test.expected)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestParseInputFile(t *testing.T) {
 | 
			
		||||
	tempFile, err := os.CreateTemp("", "test_input_*.json")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatalf("Failed to create temp file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer os.Remove(tempFile.Name())
 | 
			
		||||
 | 
			
		||||
	testData := Items{
 | 
			
		||||
		Items: []Item{
 | 
			
		||||
			{MPD: "http://example.com/video1.mpd", Filename: "video1.mp4"},
 | 
			
		||||
			{MPD: "http://example.com/video2.mpd", Filename: "video2.mp4"},
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	jsonData, _ := json.Marshal(testData)
 | 
			
		||||
	if _, err := tempFile.Write(jsonData); err != nil {
 | 
			
		||||
		t.Fatalf("Failed to write to temp file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	tempFile.Close()
 | 
			
		||||
 | 
			
		||||
	items, err := parseInputFile(tempFile.Name())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatalf("parseInputFile() returned an error: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(items) != len(testData.Items) {
 | 
			
		||||
		t.Errorf("parseInputFile() returned %d items, want %d", len(items), len(testData.Items))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for i, item := range items {
 | 
			
		||||
		if !reflect.DeepEqual(item, testData.Items[i]) {
 | 
			
		||||
			t.Errorf("parseInputFile() item %d = %v, want %v", i, item, testData.Items[i])
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestGroupItemsBySeason(t *testing.T) {
 | 
			
		||||
	items := []Item{
 | 
			
		||||
		{Filename: "show1_s01e01.mp4", Metadata: "Show 1; serie; 01"},
 | 
			
		||||
		{Filename: "show1_s01e02.mp4", Metadata: "Show 1; serie; 01"},
 | 
			
		||||
		{Filename: "show2_s01e01.mp4", Metadata: "Show 2; serie; 01"},
 | 
			
		||||
		{Filename: "movie1.mp4", Metadata: "Movie 1; movie; "},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	grouped := groupItemsBySeason(items)
 | 
			
		||||
 | 
			
		||||
	expectedGroups := map[string]int{
 | 
			
		||||
		"Show 1 - S01": 2,
 | 
			
		||||
		"Show 2 - S01": 1,
 | 
			
		||||
		"Movies":       1,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for group, count := range expectedGroups {
 | 
			
		||||
		if len(grouped[group]) != count {
 | 
			
		||||
			t.Errorf("groupItemsBySeason() group %q has %d items, want %d", group, len(grouped[group]), count)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -74,6 +74,9 @@
 | 
			
		||||
            display: inline-block;
 | 
			
		||||
            width: 5em;
 | 
			
		||||
        }
 | 
			
		||||
        .paused {
 | 
			
		||||
            color: #ffa500;
 | 
			
		||||
        }
 | 
			
		||||
        @media (max-width: 600px) {
 | 
			
		||||
            body {
 | 
			
		||||
                padding: 10px;
 | 
			
		||||
@@ -109,7 +112,7 @@
 | 
			
		||||
<body>
 | 
			
		||||
    <h1>Simple Downloader</h1>
 | 
			
		||||
    <form action="/upload" method="post" enctype="multipart/form-data">
 | 
			
		||||
        <input type="file" name="file" accept=".drmd">
 | 
			
		||||
        <input type="file" name="files" accept=".drmd" multiple>
 | 
			
		||||
        <input type="submit" value="Upload and Process">
 | 
			
		||||
    </form>
 | 
			
		||||
    <h2>Currently Running Jobs</h2>
 | 
			
		||||
@@ -120,7 +123,11 @@
 | 
			
		||||
                    <a href="/progress?filename={{$filename}}">{{$filename}}</a>
 | 
			
		||||
                </div>
 | 
			
		||||
                <div class="job-info">
 | 
			
		||||
                    Progress: <span class="progress-text">{{printf "%5.1f%%" $info.Percentage}}</span> Current file: {{$info.CurrentFile}}
 | 
			
		||||
                    Progress: <span class="progress-text">{{printf "%5.1f%%" $info.Percentage}}</span>
 | 
			
		||||
                    Current file: {{$info.CurrentFile}}
 | 
			
		||||
                    {{if $info.Paused}}
 | 
			
		||||
                        <span class="paused">(Paused)</span>
 | 
			
		||||
                    {{end}}
 | 
			
		||||
                </div>
 | 
			
		||||
            </li>
 | 
			
		||||
        {{else}}
 | 
			
		||||
@@ -127,10 +127,12 @@
 | 
			
		||||
    <div>
 | 
			
		||||
        <button id="abort-button" onclick="abortDownload()">Abort Download</button>
 | 
			
		||||
        <button id="pause-button" onclick="pauseDownload()">Pause Download</button>
 | 
			
		||||
        <button id="resume-button" onclick="resumeDownload()">Resume Download</button>
 | 
			
		||||
        <button id="resume-button" onclick="resumeDownload()" style="display: none;">Resume Download</button>
 | 
			
		||||
        <button id="back-button" onclick="window.location.href='/'">Back to Index</button>
 | 
			
		||||
    </div>
 | 
			
		||||
    <script>
 | 
			
		||||
        let isPaused = false;
 | 
			
		||||
 | 
			
		||||
        function updateProgress() {
 | 
			
		||||
            fetch('/progress?filename={{.Filename}}', {
 | 
			
		||||
                headers: {
 | 
			
		||||
@@ -143,12 +145,25 @@
 | 
			
		||||
                    document.getElementById('progress-bar').style.width = progress + '%';
 | 
			
		||||
                    document.getElementById('progress-text').innerText = progress + '%';
 | 
			
		||||
                    document.getElementById('currentFile').innerText = 'Current file: ' + (data.CurrentFile || 'None');
 | 
			
		||||
                    if (progress < 100) {
 | 
			
		||||
                    
 | 
			
		||||
                    isPaused = data.Paused;
 | 
			
		||||
                    updatePauseResumeButtons();
 | 
			
		||||
                    
 | 
			
		||||
                    if (progress < 100 && !isPaused) {
 | 
			
		||||
                        setTimeout(updateProgress, 1000);
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
        }
 | 
			
		||||
        updateProgress();
 | 
			
		||||
 | 
			
		||||
        function updatePauseResumeButtons() {
 | 
			
		||||
            if (isPaused) {
 | 
			
		||||
                document.getElementById('pause-button').style.display = 'none';
 | 
			
		||||
                document.getElementById('resume-button').style.display = 'inline-block';
 | 
			
		||||
            } else {
 | 
			
		||||
                document.getElementById('pause-button').style.display = 'inline-block';
 | 
			
		||||
                document.getElementById('resume-button').style.display = 'none';
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        function abortDownload() {
 | 
			
		||||
            fetch('/abort?filename={{.Filename}}', { method: 'POST' })
 | 
			
		||||
@@ -166,8 +181,8 @@
 | 
			
		||||
                .then(response => {
 | 
			
		||||
                    if (response.ok) {
 | 
			
		||||
                        console.log('Pause signal sent. The download will pause soon.');
 | 
			
		||||
                        document.getElementById('pause-button').style.display = 'none';
 | 
			
		||||
                        document.getElementById('resume-button').style.display = 'inline-block';
 | 
			
		||||
                        isPaused = true;
 | 
			
		||||
                        updatePauseResumeButtons();
 | 
			
		||||
                    } else {
 | 
			
		||||
                        alert('Failed to pause the download.');
 | 
			
		||||
                    }
 | 
			
		||||
@@ -179,13 +194,16 @@
 | 
			
		||||
                .then(response => {
 | 
			
		||||
                    if (response.ok) {
 | 
			
		||||
                        console.log('Resume signal sent. The download will resume soon.');
 | 
			
		||||
                        document.getElementById('resume-button').style.display = 'none';
 | 
			
		||||
                        document.getElementById('pause-button').style.display = 'inline-block';
 | 
			
		||||
                        isPaused = false;
 | 
			
		||||
                        updatePauseResumeButtons();
 | 
			
		||||
                        updateProgress();
 | 
			
		||||
                    } else {
 | 
			
		||||
                        alert('Failed to resume the download.');
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        updateProgress();
 | 
			
		||||
    </script>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
@@ -50,27 +50,44 @@
 | 
			
		||||
        button:hover, input[type="submit"]:hover {
 | 
			
		||||
            background-color: #45a049;
 | 
			
		||||
        }
 | 
			
		||||
        #fix-order-button {
 | 
			
		||||
            background-color: #2196F3;
 | 
			
		||||
            color: white;
 | 
			
		||||
            border: none;
 | 
			
		||||
            padding: 10px 15px;
 | 
			
		||||
            margin: 5px;
 | 
			
		||||
            border-radius: 4px;
 | 
			
		||||
            cursor: pointer;
 | 
			
		||||
        }
 | 
			
		||||
        #fix-order-button:hover {
 | 
			
		||||
            background-color: #1976D2;
 | 
			
		||||
        }
 | 
			
		||||
    </style>
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
    <h1>Select Items to Download</h1>
 | 
			
		||||
    <form action="/process" method="post">
 | 
			
		||||
        <input type="hidden" name="filename" value="{{.Filename}}">
 | 
			
		||||
        {{range $season, $items := .Items}}
 | 
			
		||||
            <div class="season">
 | 
			
		||||
                <div class="season-title">
 | 
			
		||||
                    <input type="checkbox" class="season-checkbox" id="season-{{$season}}" checked onchange="toggleSeason('{{$season}}')">
 | 
			
		||||
                    <label for="season-{{$season}}">{{$season}}</label>
 | 
			
		||||
                </div>
 | 
			
		||||
                {{range $item := $items}}
 | 
			
		||||
                    <div class="item">
 | 
			
		||||
                        <label>
 | 
			
		||||
                            <input type="checkbox" name="items" value="{{$item.Filename}}" checked class="episode-{{$season}}">
 | 
			
		||||
                            {{$item.Filename}}
 | 
			
		||||
                        </label>
 | 
			
		||||
        <input type="hidden" name="filenames" value="{{.Filenames}}">
 | 
			
		||||
        {{range $filename, $fileItems := .AllItems}}
 | 
			
		||||
            <h2>{{$filename}}</h2>
 | 
			
		||||
            {{range $season, $items := $fileItems}}
 | 
			
		||||
                <div class="season" id="season-{{$filename}}-{{$season}}">
 | 
			
		||||
                    <div class="season-title">
 | 
			
		||||
                        <input type="checkbox" class="season-checkbox" id="season-checkbox-{{$filename}}-{{$season}}" checked onchange="toggleSeason('{{$filename}}-{{$season}}')">
 | 
			
		||||
                        <label for="season-checkbox-{{$filename}}-{{$season}}">{{$season}}</label>
 | 
			
		||||
                    </div>
 | 
			
		||||
                {{end}}
 | 
			
		||||
            </div>
 | 
			
		||||
                    <div class="season-items">
 | 
			
		||||
                    {{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}}
 | 
			
		||||
        {{end}}
 | 
			
		||||
        <div>
 | 
			
		||||
            <button type="button" onclick="selectAll(true)">Select All</button>
 | 
			
		||||
@@ -91,7 +108,7 @@
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        function toggleSeason(season) {
 | 
			
		||||
            var seasonCheckbox = document.getElementById('season-' + season);
 | 
			
		||||
            var seasonCheckbox = document.getElementById('season-checkbox-' + season);
 | 
			
		||||
            var episodeCheckboxes = document.getElementsByClassName('episode-' + season);
 | 
			
		||||
            for (var i = 0; i < episodeCheckboxes.length; i++) {
 | 
			
		||||
                episodeCheckboxes[i].checked = seasonCheckbox.checked;
 | 
			
		||||
@@ -9,6 +9,7 @@ import (
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"sort"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
@@ -138,16 +139,24 @@ func findOrCreateSegmentTimeline(adaptationSet *etree.Element) *etree.Element {
 | 
			
		||||
	return segmentTemplate.CreateElement("SegmentTimeline")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func parseInputFile(inputFile string) ([]Item, error) {
 | 
			
		||||
	jsonFile, err := os.Open(inputFile)
 | 
			
		||||
func parseInputFile(filename string) ([]Item, error) {
 | 
			
		||||
	fileInfo, err := os.Stat(filename)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("error opening file %s: %v", inputFile, err)
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if fileInfo.IsDir() {
 | 
			
		||||
		return nil, fmt.Errorf("%s is a directory", filename)
 | 
			
		||||
	}
 | 
			
		||||
	defer jsonFile.Close()
 | 
			
		||||
 | 
			
		||||
	byteValue, err := io.ReadAll(jsonFile)
 | 
			
		||||
	file, err := os.Open(filename)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("error reading file %s: %v", inputFile, err)
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	defer file.Close()
 | 
			
		||||
 | 
			
		||||
	byteValue, err := io.ReadAll(file)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	byteValue = removeBOM(byteValue)
 | 
			
		||||
@@ -155,7 +164,7 @@ func parseInputFile(inputFile string) ([]Item, error) {
 | 
			
		||||
	var items Items
 | 
			
		||||
	err = json.Unmarshal(byteValue, &items)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("error unmarshaling JSON: %v", err)
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return items.Items, nil
 | 
			
		||||
@@ -188,6 +197,43 @@ func filterSelectedItems(items []Item, selectedItems []string) []Item {
 | 
			
		||||
	return filtered
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func sortItems(items []Item) {
 | 
			
		||||
	sort.Slice(items, func(i, j int) bool {
 | 
			
		||||
		iMeta := parseMetadata(items[i].Metadata)
 | 
			
		||||
		jMeta := parseMetadata(items[j].Metadata)
 | 
			
		||||
 | 
			
		||||
		if iMeta.Title != jMeta.Title {
 | 
			
		||||
			return iMeta.Title < jMeta.Title
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		iSeason := extractNumber(iMeta.Season)
 | 
			
		||||
		jSeason := extractNumber(jMeta.Season)
 | 
			
		||||
 | 
			
		||||
		if iSeason != jSeason {
 | 
			
		||||
			return iSeason < jSeason
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		iEpisode := extractEpisodeNumber(items[i].Filename)
 | 
			
		||||
		jEpisode := extractEpisodeNumber(items[j].Filename)
 | 
			
		||||
 | 
			
		||||
		return iEpisode < jEpisode
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func extractNumber(s string) int {
 | 
			
		||||
	num, _ := strconv.Atoi(strings.TrimLeft(s, "S"))
 | 
			
		||||
	return num
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func extractEpisodeNumber(filename string) int {
 | 
			
		||||
	parts := strings.Split(filename, "E")
 | 
			
		||||
	if len(parts) > 1 {
 | 
			
		||||
		num, _ := strconv.Atoi(parts[1])
 | 
			
		||||
		return num
 | 
			
		||||
	}
 | 
			
		||||
	return 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func processItems(filename string, items []Item) error {
 | 
			
		||||
	jobsMutex.Lock()
 | 
			
		||||
	jobInfo := &JobInfo{
 | 
			
		||||
@@ -207,6 +253,8 @@ func processItems(filename string, items []Item) error {
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	sortItems(items)
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < len(items); i++ {
 | 
			
		||||
		select {
 | 
			
		||||
		case <-jobInfo.AbortChan:
 | 
			
		||||
@@ -217,7 +265,6 @@ func processItems(filename string, items []Item) error {
 | 
			
		||||
				select {
 | 
			
		||||
				case <-jobInfo.ResumeChan:
 | 
			
		||||
					jobInfo.Paused = false
 | 
			
		||||
					fmt.Printf("Resuming download for %s\n", filename)
 | 
			
		||||
				case <-jobInfo.AbortChan:
 | 
			
		||||
					updateProgress(filename, 100, "Aborted")
 | 
			
		||||
					return fmt.Errorf("download aborted")
 | 
			
		||||
@@ -231,7 +278,6 @@ func processItems(filename string, items []Item) error {
 | 
			
		||||
					i--
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
				fmt.Printf("Error downloading file: %v\n", err)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
		Reference in New Issue
	
	Block a user