Added deferred fd close for checkIfImage, and deferred getting filenames until after checking if file is an image

This commit is contained in:
Seednode 2022-09-10 13:09:39 -05:00
parent 824965f2c3
commit fbd4357afe
2 changed files with 4 additions and 6 deletions

View File

@ -19,6 +19,7 @@ func checkIfImage(path string) (bool, error) {
if err != nil { if err != nil {
return false, err return false, err
} }
defer file.Close()
head := make([]byte, 261) head := make([]byte, 261)
file.Read(head) file.Read(head)
@ -102,17 +103,14 @@ func pickFile(fileList []string) (string, string, error) {
rand.Shuffle(len(fileList), func(i, j int) { fileList[i], fileList[j] = fileList[j], fileList[i] }) rand.Shuffle(len(fileList), func(i, j int) { fileList[i], fileList[j] = fileList[j], fileList[i] })
var filePath string
var fileName string
for i := 0; i < len(fileList); i++ { for i := 0; i < len(fileList); i++ {
filePath = fileList[i] filePath := fileList[i]
fileName = filepath.Base(filePath)
isImage, err := checkIfImage(filePath) isImage, err := checkIfImage(filePath)
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
if isImage { if isImage {
fileName := filepath.Base(filePath)
return fileName, filePath, nil return fileName, filePath, nil
} }
} }

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version = "0.3.0" var Version = "0.3.1"
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)