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: [bugfix] close files before error return (#3163) * close files before error return * use defer statements * shuffle around some defers --- internal/media/util.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'internal/media/util.go') 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() -- cgit v1.2.3