Manager: always close file when saving to JPEG

Always close the output file; previously this was not done when the
JPEG encoding would fail.
This commit is contained in:
Sybren A. Stüvel 2022-06-26 13:24:37 +02:00
parent 15ad890646
commit e6af6a708c

View File

@ -15,6 +15,7 @@ import (
_ "image/png" _ "image/png"
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
"github.com/rs/zerolog/log"
) )
var ( var (
@ -55,6 +56,13 @@ func saveJPEG(targetpath string, img image.Image) error {
return fmt.Errorf("creating file: %w", err) 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{ options := jpeg.Options{
Quality: thumbnailJPEGQuality, Quality: thumbnailJPEGQuality,
} }
@ -63,10 +71,6 @@ func saveJPEG(targetpath string, img image.Image) error {
return fmt.Errorf("encoding as JPEG: %w", err) return fmt.Errorf("encoding as JPEG: %w", err)
} }
err = file.Close()
if err != nil {
return fmt.Errorf("closing file: %w", err)
}
return nil return nil
} }