download: delete files on error

This commit is contained in:
stefanodvx 2025-04-15 19:44:17 +02:00
parent c6cb552dc8
commit 6a790440de

View file

@ -3,6 +3,7 @@ package core
import (
"context"
"fmt"
"os"
"path/filepath"
"sort"
"sync"
@ -31,6 +32,18 @@ func downloadMediaItem(
var filePath string
var thumbnailFilePath string
cleanup := true
defer func() {
if cleanup {
if filePath != "" {
os.Remove(filePath)
}
if thumbnailFilePath != "" {
os.Remove(thumbnailFilePath)
}
}
}()
if format.Type != enums.MediaTypePhoto {
if len(format.Segments) == 0 {
path, err := util.DownloadFile(
@ -77,6 +90,8 @@ func downloadMediaItem(
filePath = path
}
// all good, no need to delete files
cleanup = false
return &models.DownloadedMedia{
FilePath: filePath,
ThumbnailFilePath: thumbnailFilePath,