From f568f9e3668cfa9804c530358f458bb266367d06 Mon Sep 17 00:00:00 2001 From: Fredrik Date: Mon, 17 Jun 2024 22:47:21 +0200 Subject: [PATCH 1/2] Fix temporary file deletion issue during composite video download --- downloader/downloader.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/downloader/downloader.go b/downloader/downloader.go index b2a61dc2..1f335b19 100644 --- a/downloader/downloader.go +++ b/downloader/downloader.go @@ -85,14 +85,34 @@ func (dl *Downloader) DownloadComposite(ctx context.Context, outputFile string, if err != nil { return err } - defer os.Remove(videoFile.Name()) + // defer os.Remove(videoFile.Name()) +defer func() { + if err := videoFile.Close(); err != nil { + log.Error("Failed to close video file", "error", err) + return + } + err := os.Remove(videoFile.Name()) + if err != nil { + log.Error("Failed to delete video file", "error", err) + } + }() // Create temporary audio file audioFile, err := os.CreateTemp(outputDir, "youtube_*.m4a") if err != nil { return err } - defer os.Remove(audioFile.Name()) + // defer os.Remove(audioFile.Name()) + defer func() { + if err := audioFile.Close(); err != nil { + log.Error("Failed to close audio file", "error", err) + return + } + err := os.Remove(audioFile.Name()) + if err != nil { + log.Error("Failed to delete audio file", "error", err) + } + }() log.Debug("Downloading video file...") err = dl.videoDLWorker(ctx, videoFile, v, videoFormat) From 08d8481b65494e009344323e1ce4021bdad6705c Mon Sep 17 00:00:00 2001 From: Fredrik Date: Mon, 17 Jun 2024 22:47:21 +0200 Subject: [PATCH 2/2] Fix temporary file deletion issue during composite video download --- downloader/downloader.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/downloader/downloader.go b/downloader/downloader.go index b2a61dc2..a766a972 100644 --- a/downloader/downloader.go +++ b/downloader/downloader.go @@ -4,6 +4,7 @@ import ( "context" "errors" "io" + "log/slog" "os" "os/exec" "path/filepath" @@ -85,14 +86,14 @@ func (dl *Downloader) DownloadComposite(ctx context.Context, outputFile string, if err != nil { return err } - defer os.Remove(videoFile.Name()) + defer closeAndRemoveFile(videoFile, log) // Create temporary audio file audioFile, err := os.CreateTemp(outputDir, "youtube_*.m4a") if err != nil { return err } - defer os.Remove(audioFile.Name()) + defer closeAndRemoveFile(audioFile, log) log.Debug("Downloading video file...") err = dl.videoDLWorker(ctx, videoFile, v, videoFormat) @@ -122,6 +123,17 @@ func (dl *Downloader) DownloadComposite(ctx context.Context, outputFile string, return ffmpegVersionCmd.Run() } +func closeAndRemoveFile(file *os.File, log *slog.Logger) { + if err := file.Close(); err != nil { + log.Error("Failed to close file", "error", err) + return + } + err := os.Remove(file.Name()) + if err != nil { + log.Error("Failed to delete file", "error", err) + } +} + func getVideoAudioFormats(v *youtube.Video, quality string, mimetype, language string) (*youtube.Format, *youtube.Format, error) { var videoFormats, audioFormats youtube.FormatList