more cleaning logics

This commit is contained in:
stefanodvx 2025-04-15 21:32:56 +02:00
parent de628f4911
commit 20245c109b
4 changed files with 28 additions and 1 deletions

View file

@ -194,7 +194,15 @@ func DownloadMedias(
} }
if firstError != nil { if firstError != nil {
return results, firstError for _, result := range results {
if result.FilePath != "" {
os.Remove(result.FilePath)
}
if result.ThumbnailFilePath != "" {
os.Remove(result.ThumbnailFilePath)
}
}
return nil, firstError
} }
if len(results) > 1 { if len(results) > 1 {

View file

@ -14,6 +14,7 @@ func main() {
if err != nil { if err != nil {
log.Fatal("error loading .env file") log.Fatal("error loading .env file")
} }
util.ClearDownlods()
ok := util.CheckFFmpeg() ok := util.CheckFFmpeg()
if !ok { if !ok {
log.Fatal("ffmpeg executable not found. please install it or add it to your PATH") log.Fatal("ffmpeg executable not found. please install it or add it to your PATH")

View file

@ -481,9 +481,19 @@ func downloadSegments(
errChan := make(chan error, len(segmentURLs)) errChan := make(chan error, len(segmentURLs))
var errMutex sync.Mutex var errMutex sync.Mutex
var firstErr error var firstErr error
downloadedFiles := make([]string, len(segmentURLs)) downloadedFiles := make([]string, len(segmentURLs))
defer func() {
if firstErr != nil {
for _, path := range downloadedFiles {
if path != "" {
os.Remove(path)
}
}
}
}()
downloadCtx, cancelDownload := context.WithCancel(ctx) downloadCtx, cancelDownload := context.WithCancel(ctx)
defer cancelDownload() defer cancelDownload()

View file

@ -110,3 +110,11 @@ func CheckFFmpeg() bool {
_, err := exec.LookPath("ffmpeg") _, err := exec.LookPath("ffmpeg")
return err == nil return err == nil
} }
func ClearDownlods() {
downloadsDir := os.Getenv("DOWNLOADS_DIR")
if downloadsDir == "" {
downloadsDir = "downloads"
}
os.RemoveAll(downloadsDir)
}