diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2023-12-17 02:10:06 +0100 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2023-12-17 02:10:06 +0100 |
commit | e0a4d113dadcb1524cd0f9fd818f6d873e75eb5f (patch) | |
tree | 80846c5100668b8920ae0f0629e70fb46fe5931c | |
parent | e213f0792b0d52ee9e4768cd30cf7507d5d83f37 (diff) | |
download | gallery-e0a4d113dadcb1524cd0f9fd818f6d873e75eb5f.tar.gz gallery-e0a4d113dadcb1524cd0f9fd818f6d873e75eb5f.tar.xz gallery-e0a4d113dadcb1524cd0f9fd818f6d873e75eb5f.zip |
Resolve a mild concern
-rw-r--r-- | main.go | 61 |
1 files changed, 6 insertions, 55 deletions
@@ -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) |