Added deferred fd close for checkIfImage, and deferred getting filenames until after checking if file is an image
This commit is contained in:
parent
824965f2c3
commit
fbd4357afe
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue