14 Commits

Author SHA1 Message Date
f6a447d7f4 Hm 2024-09-14 03:39:15 +02:00
551e53ad63 Merge pull request 'Support uploading multiple files' (#5) from multiUpload into main
Reviewed-on: #5
2024-09-14 02:03:26 +02:00
707de8fcf1 Support uploading multiple files 2024-09-14 02:02:35 +02:00
64a6eb20a0 Merge pull request 'Implement correct pause state' (#4) from issue-3 into main
Reviewed-on: #4
2024-09-14 01:09:48 +02:00
67b17c1df7 Merge branch 'main' of ssh://git.directme.in:2222/Joren/DRMDTool into issue-3 2024-09-14 01:08:54 +02:00
142c09e624 Implement correct pause state 2024-09-14 00:53:55 +02:00
0b3797dc19 Update README.md 2024-09-13 23:32:56 +02:00
d4dae21d8f Update README.md 2024-09-13 23:03:59 +02:00
9f2677485e Merge pull request 'abortPause' (#1) from abortPause into main
Reviewed-on: #1
2024-09-13 22:59:31 +02:00
8aa915e6dc Clear jobs 2024-09-13 22:57:55 +02:00
37c390f911 Pause n abort 2024-09-13 22:43:22 +02:00
2f9552e771 Pause imp 2024-09-13 22:29:03 +02:00
dfe21445e5 Kill downloader process to instantly abort 2024-09-13 22:17:16 +02:00
5397ba0907 Abort (kinda) 2024-09-13 22:15:20 +02:00
11 changed files with 605 additions and 115 deletions

View File

@@ -9,6 +9,7 @@ Create a `config.toml` file in the same directory as the drmdtool executable:
```toml ```toml
BaseDir = "/path/to/save/downloads" BaseDir = "/path/to/save/downloads"
Format = "mkv" Format = "mkv"
TempBaseDir = "/tmp/nre"
[N_m3u8DL-RE] [N_m3u8DL-RE]
Path = "/path/to/N_m3u8DL-RE" Path = "/path/to/N_m3u8DL-RE"
@@ -36,10 +37,4 @@ To process a file directly from the command line:
./drmdtool -f /path/to/file.drmd ./drmdtool -f /path/to/file.drmd
``` ```
This will download the file and save it in the base directory specified in the config. 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?

View File

@@ -9,9 +9,10 @@ import (
) )
type Config struct { type Config struct {
BaseDir string BaseDir string
Format string Format string
N_m3u8DLRE struct { TempBaseDir string
N_m3u8DLRE struct {
Path string Path string
} }
} }

View File

@@ -1,5 +1,6 @@
BaseDir = "/mnt/media" BaseDir = "/mnt/media"
Format = "mkv" Format = "mkv"
TempBaseDir = "/tmp/nre"
[N_m3u8DLRE] [N_m3u8DLRE]
Path = "nre" Path = "nre"

View File

@@ -18,9 +18,17 @@ func removeBOM(input []byte) []byte {
return input return input
} }
func downloadFile(item Item) error { func downloadFile(item Item, jobInfo *JobInfo) error {
fmt.Println("Downloading:", item.Filename) fmt.Println("Downloading:", item.Filename)
tempDir := filepath.Join(config.TempBaseDir, sanitizeFilename(item.Filename))
err := os.MkdirAll(tempDir, 0755)
if err != nil {
return fmt.Errorf("error creating temporary directory: %v", err)
}
jobInfo.TempDir = tempDir
mpdPath := item.MPD mpdPath := item.MPD
if !isValidURL(item.MPD) { if !isValidURL(item.MPD) {
decodedMPD, err := base64.StdEncoding.DecodeString(item.MPD) decodedMPD, err := base64.StdEncoding.DecodeString(item.MPD)
@@ -75,7 +83,7 @@ func downloadFile(item Item) error {
mpdPath = tempFile.Name() mpdPath = tempFile.Name()
} }
command := getDownloadCommand(item, mpdPath) command := getDownloadCommand(item, mpdPath, tempDir)
if item.Subtitles != "" { if item.Subtitles != "" {
subtitlePaths, err := downloadAndConvertSubtitles(item.Subtitles) subtitlePaths, err := downloadAndConvertSubtitles(item.Subtitles)
@@ -91,19 +99,45 @@ func downloadFile(item Item) error {
cmd := exec.Command("bash", "-c", command) cmd := exec.Command("bash", "-c", command)
jobsMutex.Lock()
jobInfo.Cmd = cmd
jobsMutex.Unlock()
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
err := cmd.Run() err = cmd.Start()
if err != nil { if err != nil {
return fmt.Errorf("error executing download command: %v", err) return fmt.Errorf("error starting download command: %v", err)
}
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
select {
case <-jobInfo.AbortChan:
if cmd.Process != nil {
cmd.Process.Kill()
}
os.RemoveAll(tempDir)
return fmt.Errorf("download aborted")
case err := <-done:
if jobInfo.Paused {
return fmt.Errorf("download paused")
}
if err != nil {
return fmt.Errorf("error executing download command: %v", err)
}
} }
fmt.Println("Download completed successfully") fmt.Println("Download completed successfully")
os.RemoveAll(tempDir)
return nil return nil
} }
func getDownloadCommand(item Item, mpdPath string) string { func getDownloadCommand(item Item, mpdPath string, tempDir string) string {
metadata := parseMetadata(item.Metadata) metadata := parseMetadata(item.Metadata)
keys := getKeys(item.Keys) keys := getKeys(item.Keys)
@@ -132,6 +166,8 @@ func getDownloadCommand(item Item, mpdPath string) string {
} }
command += fmt.Sprintf(" --save-dir \"%s\"", saveDir) command += fmt.Sprintf(" --save-dir \"%s\"", saveDir)
command += fmt.Sprintf(" --tmp-dir \"%s\"", tempDir)
fmt.Println(command) fmt.Println(command)
return command return command

2
go.sum
View File

@@ -10,6 +10,8 @@ github.com/beevik/etree v1.4.1 h1:PmQJDDYahBGNKDcpdX8uPy1xRCwoCGVUiW669MEirVI=
github.com/beevik/etree v1.4.1/go.mod h1:gPNJNaBGVZ9AwsidazFZyygnd+0pAU38N4D+WemwKNs= github.com/beevik/etree v1.4.1/go.mod h1:gPNJNaBGVZ9AwsidazFZyygnd+0pAU38N4D+WemwKNs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/exec v0.0.0-20150614095509-0bd164ad2a5a h1:EN123kAtAAE2pg/+TvBsUBZfHCWNNFyL2ZBPPfNWAc0=
github.com/pkg/exec v0.0.0-20150614095509-0bd164ad2a5a/go.mod h1:b95YoNrAnScjaWG+asr8lxqlrsPUcT2ZEBcjvVGshMo=
github.com/pkg/profile v1.4.0/go.mod h1:NWz/XGvpEW1FyYQ7fCx4dqYBLlfTcE+A9FLAkNKqjFE= github.com/pkg/profile v1.4.0/go.mod h1:NWz/XGvpEW1FyYQ7fCx4dqYBLlfTcE+A9FLAkNKqjFE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

View File

@@ -5,77 +5,148 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
type ProgressInfo struct {
Percentage float64
CurrentFile string
Paused bool
}
func handleRoot(w http.ResponseWriter, r *http.Request) { func handleRoot(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
progressMutex.Lock() progressMutex.Lock()
jobs := make(map[string]*ProgressInfo) defer progressMutex.Unlock()
for k, v := range progress {
jobs[k] = v
}
progressMutex.Unlock()
err := templates.ExecuteTemplate(w, "index", struct{ Jobs map[string]*ProgressInfo }{jobs}) 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 { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
} }
func handleUpload(w http.ResponseWriter, r *http.Request) { func handleUpload(w http.ResponseWriter, r *http.Request) {
file, header, err := r.FormFile("file") err := r.ParseMultipartForm(32 << 20)
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()
tempFile, err := os.CreateTemp(uploadDir, header.Filename) files := r.MultipartForm.File["files"]
if err != nil { if len(files) == 0 {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, "No files uploaded", http.StatusBadRequest)
return
}
defer tempFile.Close()
_, err = io.Copy(tempFile, file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
tempFilename := filepath.Base(tempFile.Name()) uploadedFiles := []string{}
_, err = parseInputFile(tempFile.Name()) for _, fileHeader := range files {
if err != nil { file, err := fileHeader.Open()
http.Error(w, err.Error(), http.StatusInternalServerError) 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 return
} }
http.Redirect(w, r, "/select?filename="+tempFilename, http.StatusSeeOther) http.Redirect(w, r, "/select?files="+url.QueryEscape(strings.Join(validFiles, ",")), http.StatusSeeOther)
} }
func handleSelect(w http.ResponseWriter, r *http.Request) { func handleSelect(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Query().Get("filename") filesParam := r.URL.Query().Get("files")
items, err := parseInputFile(filepath.Join(uploadDir, filename)) filenames := strings.Split(filesParam, ",")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) 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
}
groupedItems := groupItemsBySeason(items)
allItems[filename] = groupedItems
}
if len(allItems) == 0 {
http.Error(w, "No valid files were processed", http.StatusBadRequest)
return return
} }
groupedItems := groupItemsBySeason(items) err := templates.ExecuteTemplate(w, "select", struct {
Filenames string
err = templates.ExecuteTemplate(w, "select", struct { AllItems map[string]map[string][]Item
Filename string
Items map[string][]Item
}{ }{
Filename: filename, Filenames: filesParam,
Items: groupedItems, AllItems: allItems,
}) })
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -83,36 +154,47 @@ func handleSelect(w http.ResponseWriter, r *http.Request) {
} }
func handleProcess(w http.ResponseWriter, r *http.Request) { func handleProcess(w http.ResponseWriter, r *http.Request) {
filename := r.FormValue("filename") if err := r.ParseForm(); err != nil {
selectedItems := r.Form["items"] http.Error(w, err.Error(), http.StatusBadRequest)
items, err := parseInputFile(filepath.Join(uploadDir, filename))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
filteredItems := filterSelectedItems(items, selectedItems) selectedItems := r.Form["items"]
if len(selectedItems) == 0 {
http.Error(w, "No items selected", http.StatusBadRequest)
return
}
go func() { itemsByFile := make(map[string][]string)
err := processItems(filename, filteredItems) 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 { if err != nil {
fmt.Printf("Error processing file: %v\n", err) http.Error(w, err.Error(), http.StatusInternalServerError)
return
} }
os.Remove(filepath.Join(uploadDir, filename)) selectedItems := filterSelectedItems(allItems, items)
}() go processItems(filename, selectedItems)
}
http.Redirect(w, r, "/progress?filename="+filename, http.StatusSeeOther) http.Redirect(w, r, "/", 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)
@@ -123,7 +205,6 @@ 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
} }
@@ -135,3 +216,138 @@ func handleProgress(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) 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,
}
}
}

19
main.go
View File

@@ -37,11 +37,6 @@ var progress = make(map[string]*ProgressInfo)
const uploadDir = "uploads" const uploadDir = "uploads"
type ProgressInfo struct {
Percentage float64
CurrentFile string
}
var templates *template.Template var templates *template.Template
//go:embed templates //go:embed templates
@@ -78,21 +73,15 @@ func startWebServer() {
http.HandleFunc("/select", handleSelect) http.HandleFunc("/select", handleSelect)
http.HandleFunc("/process", handleProcess) http.HandleFunc("/process", handleProcess)
http.HandleFunc("/progress", handleProgress) http.HandleFunc("/progress", handleProgress)
http.HandleFunc("/abort", handleAbort)
http.HandleFunc("/pause", handlePause)
http.HandleFunc("/resume", handleResume)
http.HandleFunc("/clear-completed", handleClearCompleted)
fmt.Println("Starting web server on http://0.0.0.0:8080") fmt.Println("Starting web server on http://0.0.0.0:8080")
http.ListenAndServe(":8080", nil) 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 { func getProgress(filename string) *ProgressInfo {
progressMutex.Lock() progressMutex.Lock()
defer progressMutex.Unlock() defer progressMutex.Unlock()

View File

@@ -45,6 +45,7 @@
ul { ul {
list-style-type: none; list-style-type: none;
padding: 0; padding: 0;
margin-bottom: 10px;
} }
li { li {
background-color: #2d2d2d; background-color: #2d2d2d;
@@ -73,6 +74,9 @@
display: inline-block; display: inline-block;
width: 5em; width: 5em;
} }
.paused {
color: #ffa500;
}
@media (max-width: 600px) { @media (max-width: 600px) {
body { body {
padding: 10px; padding: 10px;
@@ -83,13 +87,32 @@
input[type="file"], input[type="submit"] { input[type="file"], input[type="submit"] {
font-size: 16px; font-size: 16px;
} }
input[type="submit"], #clear-completed {
font-size: 16px;
}
}
input[type="submit"], #clear-completed {
cursor: pointer;
color: white;
border: 1px solid #444;
padding: 8px 12px;
border-radius: 4px;
margin-bottom: 10px;
max-width: 100%;
width: 100%;
}
#clear-completed {
background-color: #f44336;
}
#clear-completed:hover {
background-color: #d32f2f;
} }
</style> </style>
</head> </head>
<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="file" accept=".drmd"> <input type="file" name="files" accept=".drmd" multiple>
<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>
@@ -100,12 +123,30 @@
<a href="/progress?filename={{$filename}}">{{$filename}}</a> <a href="/progress?filename={{$filename}}">{{$filename}}</a>
</div> </div>
<div class="job-info"> <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> </div>
</li> </li>
{{else}} {{else}}
<li>No active jobs</li> <li>No active jobs</li>
{{end}} {{end}}
</ul> </ul>
<button id="clear-completed" onclick="clearCompleted()">Clear Completed Jobs</button>
<script>
function clearCompleted() {
fetch('/clear-completed', { method: 'POST' })
.then(response => response.json())
.then(data => {
if (data.success) {
location.reload();
} else {
alert('Failed to clear completed jobs');
}
});
}
</script>
</body> </body>
</html> </html>

View File

@@ -56,6 +56,46 @@
margin-top: 10px; margin-top: 10px;
word-wrap: break-word; word-wrap: break-word;
} }
#abort-button {
background-color: #f44336;
color: white;
border: none;
padding: 10px 15px;
margin-top: 10px;
border-radius: 4px;
cursor: pointer;
}
#abort-button:hover {
background-color: #d32f2f;
}
#pause-button, #resume-button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 15px;
margin-top: 10px;
border-radius: 4px;
cursor: pointer;
}
#pause-button:hover, #resume-button:hover {
background-color: #45a049;
}
#resume-button {
display: none;
}
#back-button {
background-color: #2196F3;
color: white;
border: none;
padding: 10px 15px;
margin-top: 10px;
border-radius: 4px;
cursor: pointer;
float: right;
}
#back-button:hover {
background-color: #1976D2;
}
@media (max-width: 600px) { @media (max-width: 600px) {
body { body {
padding: 10px; padding: 10px;
@@ -84,7 +124,15 @@
</div> </div>
<div id="currentFile"></div> <div id="currentFile"></div>
</div> </div>
<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()" style="display: none;">Resume Download</button>
<button id="back-button" onclick="window.location.href='/'">Back to Index</button>
</div>
<script> <script>
let isPaused = false;
function updateProgress() { function updateProgress() {
fetch('/progress?filename={{.Filename}}', { fetch('/progress?filename={{.Filename}}', {
headers: { headers: {
@@ -97,11 +145,64 @@
document.getElementById('progress-bar').style.width = progress + '%'; document.getElementById('progress-bar').style.width = progress + '%';
document.getElementById('progress-text').innerText = progress + '%'; document.getElementById('progress-text').innerText = progress + '%';
document.getElementById('currentFile').innerText = 'Current file: ' + (data.CurrentFile || 'None'); document.getElementById('currentFile').innerText = 'Current file: ' + (data.CurrentFile || 'None');
if (progress < 100) {
isPaused = data.Paused;
updatePauseResumeButtons();
if (progress < 100 && !isPaused) {
setTimeout(updateProgress, 1000); setTimeout(updateProgress, 1000);
} }
}); });
} }
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' })
.then(response => {
if (response.ok) {
console.log('Abort signal sent. The download will stop soon.');
} else {
alert('Failed to abort the download.');
}
});
}
function pauseDownload() {
fetch('/pause?filename={{.Filename}}', { method: 'POST' })
.then(response => {
if (response.ok) {
console.log('Pause signal sent. The download will pause soon.');
isPaused = true;
updatePauseResumeButtons();
} else {
alert('Failed to pause the download.');
}
});
}
function resumeDownload() {
fetch('/resume?filename={{.Filename}}', { method: 'POST' })
.then(response => {
if (response.ok) {
console.log('Resume signal sent. The download will resume soon.');
isPaused = false;
updatePauseResumeButtons();
updateProgress();
} else {
alert('Failed to resume the download.');
}
});
}
updateProgress(); updateProgress();
</script> </script>
</body> </body>

View File

@@ -55,22 +55,25 @@
<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="filename" value="{{.Filename}}"> <input type="hidden" name="filenames" value="{{.Filenames}}">
{{range $season, $items := .Items}} {{range $filename, $fileItems := .AllItems}}
<div class="season"> <h2>{{$filename}}</h2>
<div class="season-title"> {{range $season, $items := $fileItems}}
<input type="checkbox" class="season-checkbox" id="season-{{$season}}" checked onchange="toggleSeason('{{$season}}')"> <div class="season">
<label for="season-{{$season}}">{{$season}}</label> <div class="season-title">
</div> <input type="checkbox" class="season-checkbox" id="season-{{$filename}}-{{$season}}" checked onchange="toggleSeason('{{$filename}}-{{$season}}')">
{{range $item := $items}} <label for="season-{{$filename}}-{{$season}}">{{$season}}</label>
<div class="item">
<label>
<input type="checkbox" name="items" value="{{$item.Filename}}" checked class="episode-{{$season}}">
{{$item.Filename}}
</label>
</div> </div>
{{end}} {{range $item := $items}}
</div> <div class="item">
<label>
<input type="checkbox" name="items" value="{{$filename}}:{{$item.Filename}}" checked class="episode-{{$filename}}-{{$season}}">
{{$item.Filename}}
</label>
</div>
{{end}}
</div>
{{end}}
{{end}} {{end}}
<div> <div>
<button type="button" onclick="selectAll(true)">Select All</button> <button type="button" onclick="selectAll(true)">Select All</button>

129
utils.go
View File

@@ -6,13 +6,29 @@ import (
"io" "io"
"net/url" "net/url"
"os" "os"
"os/exec"
"path/filepath"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/beevik/etree" "github.com/beevik/etree"
) )
type JobInfo struct {
AbortChan chan struct{}
ResumeChan chan struct{}
Cmd *exec.Cmd
Paused bool
TempDir string
}
var (
jobsMutex sync.Mutex
jobs = make(map[string]*JobInfo)
)
func sanitizeFilename(filename string) string { func sanitizeFilename(filename string) string {
filename = regexp.MustCompile(`[<>:"/\\|?*]`).ReplaceAllString(filename, "_") filename = regexp.MustCompile(`[<>:"/\\|?*]`).ReplaceAllString(filename, "_")
@@ -122,16 +138,24 @@ func findOrCreateSegmentTimeline(adaptationSet *etree.Element) *etree.Element {
return segmentTemplate.CreateElement("SegmentTimeline") return segmentTemplate.CreateElement("SegmentTimeline")
} }
func parseInputFile(inputFile string) ([]Item, error) { func parseInputFile(filename string) ([]Item, error) {
jsonFile, err := os.Open(inputFile) fileInfo, err := os.Stat(filename)
if err != nil { 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 { 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) byteValue = removeBOM(byteValue)
@@ -139,7 +163,7 @@ func parseInputFile(inputFile 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, fmt.Errorf("error unmarshaling JSON: %v", err) return nil, err
} }
return items.Items, nil return items.Items, nil
@@ -173,13 +197,94 @@ func filterSelectedItems(items []Item, selectedItems []string) []Item {
} }
func processItems(filename string, items []Item) error { func processItems(filename string, items []Item) error {
for i, item := range items { jobsMutex.Lock()
updateProgress(filename, float64(i)/float64(len(items))*100, item.Filename) jobInfo := &JobInfo{
err := downloadFile(item) AbortChan: make(chan struct{}),
if err != nil { ResumeChan: make(chan struct{}),
fmt.Printf("Error downloading file: %v\n", err) }
jobs[filename] = jobInfo
jobsMutex.Unlock()
defer func() {
jobsMutex.Lock()
delete(jobs, filename)
jobsMutex.Unlock()
if jobInfo.TempDir != "" {
os.RemoveAll(jobInfo.TempDir)
}
}()
for i := 0; i < len(items); i++ {
select {
case <-jobInfo.AbortChan:
updateProgress(filename, 100, "Aborted")
return fmt.Errorf("download aborted")
default:
if jobInfo.Paused {
select {
case <-jobInfo.ResumeChan:
jobInfo.Paused = false
case <-jobInfo.AbortChan:
updateProgress(filename, 100, "Aborted")
return fmt.Errorf("download aborted")
}
}
updateProgress(filename, float64(i)/float64(len(items))*100, items[i].Filename)
err := downloadFile(items[i], jobInfo)
if err != nil {
if err.Error() == "download paused" {
removeCompletedEpisodes(filename, items[:i])
i--
continue
}
}
} }
} }
updateProgress(filename, 100, "") updateProgress(filename, 100, "")
return nil return nil
} }
func removeCompletedEpisodes(filename string, completedItems []Item) error {
inputFile := filepath.Join(uploadDir, filename)
items, err := parseInputFile(inputFile)
if err != nil {
return fmt.Errorf("error parsing input file: %v", err)
}
remainingItems := make([]Item, 0)
for _, item := range items {
if !isItemCompleted(item, completedItems) || isLastCompletedItem(item, completedItems) {
remainingItems = append(remainingItems, item)
}
}
updatedItems := Items{Items: remainingItems}
jsonData, err := json.MarshalIndent(updatedItems, "", " ")
if err != nil {
return fmt.Errorf("error marshaling updated items: %v", err)
}
err = os.WriteFile(inputFile, jsonData, 0644)
if err != nil {
return fmt.Errorf("error writing updated DRMD file: %v", err)
}
return nil
}
func isItemCompleted(item Item, completedItems []Item) bool {
for _, completedItem := range completedItems {
if item.Filename == completedItem.Filename {
return true
}
}
return false
}
func isLastCompletedItem(item Item, completedItems []Item) bool {
if len(completedItems) == 0 {
return false
}
return item.Filename == completedItems[len(completedItems)-1].Filename
}