Pause n abort

This commit is contained in:
Joren 2024-09-13 22:43:22 +02:00
parent 2f9552e771
commit 37c390f911
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55
5 changed files with 29 additions and 6 deletions

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

@ -21,6 +21,14 @@ func removeBOM(input []byte) []byte {
func downloadFile(item Item, jobInfo *JobInfo) 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, jobInfo *JobInfo) 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)
@ -98,7 +106,7 @@ func downloadFile(item Item, jobInfo *JobInfo) error {
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
err := cmd.Start() err = cmd.Start()
if err != nil { if err != nil {
return fmt.Errorf("error starting download command: %v", err) return fmt.Errorf("error starting download command: %v", err)
} }
@ -113,6 +121,7 @@ func downloadFile(item Item, jobInfo *JobInfo) error {
if cmd.Process != nil { if cmd.Process != nil {
cmd.Process.Kill() cmd.Process.Kill()
} }
os.RemoveAll(tempDir)
return fmt.Errorf("download aborted") return fmt.Errorf("download aborted")
case err := <-done: case err := <-done:
if jobInfo.Paused { if jobInfo.Paused {
@ -124,10 +133,11 @@ func downloadFile(item Item, jobInfo *JobInfo) error {
} }
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)
@ -156,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

View File

@ -203,5 +203,9 @@ func handleAbort(w http.ResponseWriter, r *http.Request) {
jobInfo.Cmd.Process.Kill() jobInfo.Cmd.Process.Kill()
} }
if jobInfo.TempDir != "" {
os.RemoveAll(jobInfo.TempDir)
}
fmt.Fprintf(w, "Abort signal sent for %s", filename) fmt.Fprintf(w, "Abort signal sent for %s", filename)
} }

View File

@ -21,6 +21,7 @@ type JobInfo struct {
ResumeChan chan struct{} ResumeChan chan struct{}
Cmd *exec.Cmd Cmd *exec.Cmd
Paused bool Paused bool
TempDir string
} }
var ( var (
@ -200,6 +201,10 @@ func processItems(filename string, items []Item) error {
jobsMutex.Lock() jobsMutex.Lock()
delete(jobs, filename) delete(jobs, filename)
jobsMutex.Unlock() jobsMutex.Unlock()
if jobInfo.TempDir != "" {
os.RemoveAll(jobInfo.TempDir)
}
}() }()
for i := 0; i < len(items); i++ { for i := 0; i < len(items); i++ {