summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-08-02 14:11:24 +0000
committerLibravatar GitHub <noreply@github.com>2024-08-02 15:11:24 +0100
commite5e996b28a31612f95961572c20cbd611e6211f9 (patch)
treea2f1fa05df77521e29d4efb97d59cd0de6f35384
parent[chore] move PopulateAccountStats() nil check often performed into function i... (diff)
downloadgotosocial-e5e996b28a31612f95961572c20cbd611e6211f9.tar.xz
[bugfix] close files before error return (#3163)
* close files before error return * use defer statements * shuffle around some defers
-rw-r--r--internal/media/metadata.go6
-rw-r--r--internal/media/util.go12
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()