From e6af6a708c1d7304b48d73594c518fc14f604fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 26 Jun 2022 13:24:37 +0200 Subject: [PATCH] Manager: always close file when saving to JPEG Always close the output file; previously this was not done when the JPEG encoding would fail. --- internal/manager/last_rendered/image_processing.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/manager/last_rendered/image_processing.go b/internal/manager/last_rendered/image_processing.go index b2f6697d..a49514bd 100644 --- a/internal/manager/last_rendered/image_processing.go +++ b/internal/manager/last_rendered/image_processing.go @@ -15,6 +15,7 @@ import ( _ "image/png" "github.com/disintegration/imaging" + "github.com/rs/zerolog/log" ) var ( @@ -55,6 +56,13 @@ func saveJPEG(targetpath string, img image.Image) error { return fmt.Errorf("creating file: %w", err) } + defer func() { + err = file.Close() + if err != nil { + log.Warn().Err(err).Str("filename", targetpath).Msg("last-rendered: error closing file") + } + }() + options := jpeg.Options{ Quality: thumbnailJPEGQuality, } @@ -63,10 +71,6 @@ func saveJPEG(targetpath string, img image.Image) error { return fmt.Errorf("encoding as JPEG: %w", err) } - err = file.Close() - if err != nil { - return fmt.Errorf("closing file: %w", err) - } return nil }