From 8ae65e4b73eb0d4269a7e0a50827df3d7d761481 Mon Sep 17 00:00:00 2001 From: James Mills Date: Sun, 22 Mar 2020 18:42:35 +1000 Subject: [PATCH] Don't generate new thumbnails if they already exist --- media/video.go | 11 +++++++---- utils/utils.go | 11 +++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/media/video.go b/media/video.go index f37c697..e864be0 100644 --- a/media/video.go +++ b/media/video.go @@ -79,10 +79,13 @@ func ParseVideo(p *Path, name string) (*Video, error) { v.Thumb = pic.Data v.ThumbType = pic.MIMEType } else if utils.CmdExists("mt") { - if err := utils.RunCmd(10, "mt", "-s", "-n", "1", pth); err != nil { - err := fmt.Errorf("error generating thumbnail: %w", err) - log.Error(err.Error()) - return nil, err + thumbFn := fmt.Sprintf("%s.jpg", strings.TrimSuffix(pth, filepath.Ext(pth))) + if !utils.FileExists(thumbFn) { + if err := utils.RunCmd(10, "mt", "-s", "-n", "1", pth); err != nil { + err := fmt.Errorf("error generating thumbnail: %w", err) + log.Error(err.Error()) + return nil, err + } } data, err := ioutil.ReadFile(fmt.Sprintf("%s.jpg", strings.TrimSuffix(pth, filepath.Ext(pth)))) if err != nil { diff --git a/utils/utils.go b/utils/utils.go index 0ca1cea..b7ab28f 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,10 +3,21 @@ package utils import ( "context" "fmt" + "os" "os/exec" "time" ) +// FileExists ... +func FileExists(name string) bool { + if _, err := os.Stat(name); err != nil { + if os.IsNotExist(err) { + return false + } + } + return true +} + // CmdExists ... func CmdExists(cmd string) bool { _, err := exec.LookPath(cmd)