[bugfix] close files before error return (#3163)

* close files before error return

* use defer statements

* shuffle around some defers
This commit is contained in:
kim
2024-08-02 14:11:24 +00:00
committed by GitHub
parent 0f734a2410
commit e5e996b28a
2 changed files with 9 additions and 9 deletions

View File

@ -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()