From 6a790440de22feb882e953b161038c2155d964a5 Mon Sep 17 00:00:00 2001 From: stefanodvx <69367859+stefanodvx@users.noreply.github.com> Date: Tue, 15 Apr 2025 19:44:17 +0200 Subject: [PATCH] download: delete files on error --- bot/core/download.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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,