defer close on files after opening

This commit is contained in:
2026-02-08 00:14:40 -08:00
parent eb06ddc35c
commit 77796e79a4

View File

@@ -107,7 +107,12 @@ func getExistingTracks(conn *pgx.Conn, userId int, tracks []SpotifyTrack) (map[s
diff = -diff diff = -diff
} }
if diff < 20*time.Second { if diff < 20*time.Second {
key := fmt.Sprintf("%s|%s|%s", newTrack.Artist, newTrack.Name, newTrack.Timestamp) key := fmt.Sprintf(
"%s|%s|%s",
newTrack.Artist,
newTrack.Name,
newTrack.Timestamp,
)
existing[key] = true existing[key] = true
break break
} }
@@ -269,12 +274,13 @@ func ImportSpotify(userId int) error {
return err return err
} }
for _, f := range entries { for _, f := range entries {
_, err := zip.OpenReader(filepath.Join(path, f.Name())) reader, err := zip.OpenReader(filepath.Join(path, f.Name()))
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error opening zip: %s: %v\n", fmt.Fprintf(os.Stderr, "Error opening zip: %s: %v\n",
filepath.Join(path, f.Name()), err) filepath.Join(path, f.Name()), err)
continue continue
} }
defer reader.Close()
fileName := f.Name() fileName := f.Name()
fileFullPath := filepath.Join(path, fileName) fileFullPath := filepath.Join(path, fileName)
fileBaseName := fileName[:(strings.LastIndex(fileName, "."))] fileBaseName := fileName[:(strings.LastIndex(fileName, "."))]
@@ -339,11 +345,13 @@ func Extract(path string, target string) error {
fmt.Fprintf(os.Stderr, "Error opening file: %s: %v\n", filePath, err) fmt.Fprintf(os.Stderr, "Error opening file: %s: %v\n", filePath, err)
return err return err
} }
defer fileToExtract.Close()
extractedFile, err := f.Open() extractedFile, err := f.Open()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error opening file: %s: %v\n", f.Name, err) fmt.Fprintf(os.Stderr, "Error opening file: %s: %v\n", f.Name, err)
return err return err
} }
defer extractedFile.Close()
if _, err := io.Copy(fileToExtract, extractedFile); err != nil { if _, err := io.Copy(fileToExtract, extractedFile); err != nil {
fmt.Fprintf( fmt.Fprintf(
os.Stderr, os.Stderr,
@@ -354,8 +362,6 @@ func Extract(path string, target string) error {
) )
return err return err
} }
fileToExtract.Close()
extractedFile.Close()
} }
return nil return nil
} }