diff --git a/bot/core/download.go b/bot/core/download.go index 217670d..4e638a3 100644 --- a/bot/core/download.go +++ b/bot/core/download.go @@ -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,