From e0a4d113dadcb1524cd0f9fd818f6d873e75eb5f Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Sun, 17 Dec 2023 02:10:06 +0100 Subject: Resolve a mild concern --- main.go | 61 ++++++------------------------------------------------------- 1 file changed, 6 insertions(+), 55 deletions(-) diff --git a/main.go b/main.go index 4cad26f..886e4c7 100644 --- a/main.go +++ b/main.go @@ -482,45 +482,20 @@ func (dm *directoryManager) IDForDirectoryPath( } func isImage(path string) (bool, error) { - cmd := exec.Command("xdg-mime", "query", "filetype", path) - - // XXX: Early returns may leak resources. - stdout, err := cmd.StdoutPipe() - if err != nil { - return false, err - } - if err := cmd.Start(); err != nil { - return false, err - } - out, err := io.ReadAll(stdout) + out, err := exec.Command("xdg-mime", "query", "filetype", path).Output() if err != nil { return false, err } - if err := cmd.Wait(); err != nil { - return false, err - } + return bytes.HasPrefix(out, []byte("image/")), nil } func pingImage(path string) (int, int, error) { - cmd := exec.Command("identify", "-limit", "thread", "1", "-ping", - "-format", "%w %h", path+"[0]") - - // XXX: Early returns may leak resources. - stdout, err := cmd.StdoutPipe() - if err != nil { - return 0, 0, err - } - if err := cmd.Start(); err != nil { - return 0, 0, err - } - out, err := io.ReadAll(stdout) + out, err := exec.Command("identify", "-limit", "thread", "1", "-ping", + "-format", "%w %h", path+"[0]").Output() if err != nil { return 0, 0, err } - if err := cmd.Wait(); err != nil { - return 0, 0, err - } var w, h int _, err = fmt.Fscanf(bytes.NewReader(out), "%d %d", &w, &h) @@ -813,21 +788,10 @@ func makeThumbnail(pathImage, pathThumb string) (int, int, error) { "-resize", "256x128>", "-colorspace", "sRGB", "-format", "%w %h", "+write", pathThumb, "-delete", "1--1", "info:") - // XXX: Early returns may leak resources. - stdout, err := cmd.StdoutPipe() - if err != nil { - return 0, 0, err - } - if err := cmd.Start(); err != nil { - return 0, 0, err - } - out, err := io.ReadAll(stdout) + out, err := cmd.Output() if err != nil { return 0, 0, err } - if err := cmd.Wait(); err != nil { - return 0, 0, err - } var w, h int _, err = fmt.Fscanf(bytes.NewReader(out), "%d %d", &w, &h) @@ -898,23 +862,10 @@ func cmdThumbnail(args []string) error { // --- Perceptual hash --------------------------------------------------------- func makeDhash(hasher, pathThumb string) (uint64, error) { - cmd := exec.Command(hasher, pathThumb) - - // XXX: Early returns may leak resources. - stdout, err := cmd.StdoutPipe() - if err != nil { - return 0, err - } - if err := cmd.Start(); err != nil { - return 0, err - } - out, err := io.ReadAll(stdout) + out, err := exec.Command(hasher, pathThumb).Output() if err != nil { return 0, err } - if err := cmd.Wait(); err != nil { - return 0, err - } var hash uint64 _, err = fmt.Fscanf(bytes.NewReader(out), "%x", &hash) -- cgit v1.2.3-70-g09d2