diff --git a/web/routes.go b/web/routes.go index d737e24..2ee1894 100644 --- a/web/routes.go +++ b/web/routes.go @@ -31,7 +31,7 @@ func (r *routes) IndexHandler(c *fiber.Ctx) error { "Flash": flash.Get(c), "Version": version.Version, "Build": version.Build, - "YtdlpVersion": ytdl.GetVersion(), + "YtdlpVersion": r.ytdl.Version(), }, "views/layouts/main") } @@ -66,7 +66,7 @@ func (r *routes) DownloadHandler(c *fiber.Ctx) error { "Videos": GetVideos(meta), "Version": version.Version, "Build": version.Build, - "YtdlpVersion": ytdl.GetVersion(), + "YtdlpVersion": r.ytdl.Version, }, "views/layouts/main") } diff --git a/ytdl/version.go b/ytdl/version.go deleted file mode 100644 index cd42289..0000000 --- a/ytdl/version.go +++ /dev/null @@ -1,41 +0,0 @@ -package ytdl - -import ( - "bytes" - "os/exec" - "strings" - "sync" - - "github.com/spf13/viper" -) - -var ( - version string - versionOnce sync.Once -) - -func GetVersion() string { - var err error - versionOnce.Do(func() { - cmd := exec.Command( - viper.GetString("ytdlp_path"), - "--version", - ) - - var out bytes.Buffer - cmd.Stdout = &out - - err = cmd.Run() - if err != nil { - return - } - - version = strings.TrimSpace(out.String()) - }) - - if err != nil { - return "Error getting version" - } - - return version -} diff --git a/ytdl/ytdl.go b/ytdl/ytdl.go index cd3f2a4..04b4806 100644 --- a/ytdl/ytdl.go +++ b/ytdl/ytdl.go @@ -1,7 +1,10 @@ package ytdl import ( + "bytes" "io" + "os/exec" + "strings" "go.fifitido.net/ytdl-web/config" "go.fifitido.net/ytdl-web/ytdl/cache" @@ -12,19 +15,30 @@ import ( type Ytdl interface { GetMetadata(url string) (*metadata.Metadata, error) Download(w io.Writer, url, format string, index int) error + Version() string } type ytdlImpl struct { - cfg *config.Config - logger *slog.Logger - cache cache.MetadataCache + cfg *config.Config + logger *slog.Logger + cache cache.MetadataCache + version string } func NewYtdl(cfg *config.Config, logger *slog.Logger, cache cache.MetadataCache) Ytdl { + cmd := exec.Command( + cfg.BinaryPath, + "--version", + ) + var out bytes.Buffer + cmd.Stdout = &out + _ = cmd.Run() + return &ytdlImpl{ - cfg: cfg, - logger: logger.With(slog.String("module", "ytdl")), - cache: cache, + cfg: cfg, + logger: logger.With(slog.String("module", "ytdl")), + cache: cache, + version: strings.TrimSpace(out.String()), } } @@ -56,6 +70,10 @@ func (y *ytdlImpl) baseOptions(url string) []Option { return options } +func (y *ytdlImpl) Version() string { + return y.version +} + // GetMetadata implements Ytdl func (y *ytdlImpl) GetMetadata(url string) (*metadata.Metadata, error) { meta, err := y.cache.Get(url)