From 37c390f91123b38bd4259b1acae71458248e8d99 Mon Sep 17 00:00:00 2001 From: Joren Date: Fri, 13 Sep 2024 22:43:22 +0200 Subject: [PATCH] Pause n abort --- config.go | 7 ++++--- config.toml | 1 + downloaders.go | 18 +++++++++++++++--- handlers.go | 4 ++++ utils.go | 5 +++++ 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index b481b97..b7253bb 100644 --- a/config.go +++ b/config.go @@ -9,9 +9,10 @@ import ( ) type Config struct { - BaseDir string - Format string - N_m3u8DLRE struct { + BaseDir string + Format string + TempBaseDir string + N_m3u8DLRE struct { Path string } } diff --git a/config.toml b/config.toml index 90f1faa..7fe8fdb 100644 --- a/config.toml +++ b/config.toml @@ -1,5 +1,6 @@ BaseDir = "/mnt/media" Format = "mkv" +TempBaseDir = "/tmp/nre" [N_m3u8DLRE] Path = "nre" diff --git a/downloaders.go b/downloaders.go index f11d206..3681151 100644 --- a/downloaders.go +++ b/downloaders.go @@ -21,6 +21,14 @@ func removeBOM(input []byte) []byte { func downloadFile(item Item, jobInfo *JobInfo) error { 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 if !isValidURL(item.MPD) { decodedMPD, err := base64.StdEncoding.DecodeString(item.MPD) @@ -75,7 +83,7 @@ func downloadFile(item Item, jobInfo *JobInfo) error { mpdPath = tempFile.Name() } - command := getDownloadCommand(item, mpdPath) + command := getDownloadCommand(item, mpdPath, tempDir) if item.Subtitles != "" { subtitlePaths, err := downloadAndConvertSubtitles(item.Subtitles) @@ -98,7 +106,7 @@ func downloadFile(item Item, jobInfo *JobInfo) error { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - err := cmd.Start() + err = cmd.Start() if err != nil { return fmt.Errorf("error starting download command: %v", err) } @@ -113,6 +121,7 @@ func downloadFile(item Item, jobInfo *JobInfo) error { if cmd.Process != nil { cmd.Process.Kill() } + os.RemoveAll(tempDir) return fmt.Errorf("download aborted") case err := <-done: if jobInfo.Paused { @@ -124,10 +133,11 @@ func downloadFile(item Item, jobInfo *JobInfo) error { } fmt.Println("Download completed successfully") + os.RemoveAll(tempDir) return nil } -func getDownloadCommand(item Item, mpdPath string) string { +func getDownloadCommand(item Item, mpdPath string, tempDir string) string { metadata := parseMetadata(item.Metadata) keys := getKeys(item.Keys) @@ -156,6 +166,8 @@ func getDownloadCommand(item Item, mpdPath string) string { } command += fmt.Sprintf(" --save-dir \"%s\"", saveDir) + command += fmt.Sprintf(" --tmp-dir \"%s\"", tempDir) + fmt.Println(command) return command diff --git a/handlers.go b/handlers.go index 8d2074e..6e92aa5 100644 --- a/handlers.go +++ b/handlers.go @@ -203,5 +203,9 @@ func handleAbort(w http.ResponseWriter, r *http.Request) { jobInfo.Cmd.Process.Kill() } + if jobInfo.TempDir != "" { + os.RemoveAll(jobInfo.TempDir) + } + fmt.Fprintf(w, "Abort signal sent for %s", filename) } diff --git a/utils.go b/utils.go index 366af0e..6fc3cf7 100644 --- a/utils.go +++ b/utils.go @@ -21,6 +21,7 @@ type JobInfo struct { ResumeChan chan struct{} Cmd *exec.Cmd Paused bool + TempDir string } var ( @@ -200,6 +201,10 @@ func processItems(filename string, items []Item) error { jobsMutex.Lock() delete(jobs, filename) jobsMutex.Unlock() + + if jobInfo.TempDir != "" { + os.RemoveAll(jobInfo.TempDir) + } }() for i := 0; i < len(items); i++ {