From e5e996b28a31612f95961572c20cbd611e6211f9 Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Fri, 2 Aug 2024 14:11:24 +0000 Subject: [PATCH] [bugfix] close files before error return (#3163) * close files before error return * use defer statements * shuffle around some defers --- internal/media/metadata.go | 6 ++---- internal/media/util.go | 12 +++++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/media/metadata.go b/internal/media/metadata.go index 3816b2826..cccfc8296 100644 --- a/internal/media/metadata.go +++ b/internal/media/metadata.go @@ -75,12 +75,14 @@ func terminateExif(outpath, inpath string, ext string) error { if err != nil { return gtserror.Newf("error opening input file %s: %w", inpath, err) } + defer inFile.Close() // Open output file at given path. outFile, err := os.Create(outpath) if err != nil { return gtserror.Newf("error opening output file %s: %w", outpath, err) } + defer outFile.Close() // Terminate EXIF data from 'inFile' -> 'outFile'. err = terminator.TerminateInto(outFile, inFile, ext) @@ -88,9 +90,5 @@ func terminateExif(outpath, inpath string, ext string) error { return gtserror.Newf("error terminating exif data: %w", err) } - // Done with files. - _ = inFile.Close() - _ = outFile.Close() - return nil } diff --git a/internal/media/util.go b/internal/media/util.go index fa5c2bfd6..fa170965f 100644 --- a/internal/media/util.go +++ b/internal/media/util.go @@ -120,15 +120,17 @@ func getMimeType(ext string) string { // chance that Linux's sendfile syscall can be utilised for optimal // draining of data source to temporary file storage. func drainToTmp(rc io.ReadCloser) (string, error) { - tmp, err := os.CreateTemp(os.TempDir(), "gotosocial-*") + defer rc.Close() + + // Open new temporary file. + tmp, err := os.CreateTemp( + os.TempDir(), + "gotosocial-*", + ) if err != nil { return "", err } - - // Close readers - // on func return. defer tmp.Close() - defer rc.Close() // Extract file path. path := tmp.Name()