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 {
BaseDir string
Format string
N_m3u8DLRE struct {
BaseDir string
Format string
TempBaseDir string
N_m3u8DLRE struct {
Path string
}
}

View File

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

View File

@ -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

View File

@ -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)
}

View File

@ -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++ {