Fix yt-dlp version fetching

This commit is contained in:
Evan Fiordeliso 2023-05-23 22:29:56 -04:00
parent e0575cc7de
commit 08a6ab0da7
3 changed files with 26 additions and 49 deletions

View File

@ -31,7 +31,7 @@ func (r *routes) IndexHandler(c *fiber.Ctx) error {
"Flash": flash.Get(c), "Flash": flash.Get(c),
"Version": version.Version, "Version": version.Version,
"Build": version.Build, "Build": version.Build,
"YtdlpVersion": ytdl.GetVersion(), "YtdlpVersion": r.ytdl.Version(),
}, "views/layouts/main") }, "views/layouts/main")
} }
@ -66,7 +66,7 @@ func (r *routes) DownloadHandler(c *fiber.Ctx) error {
"Videos": GetVideos(meta), "Videos": GetVideos(meta),
"Version": version.Version, "Version": version.Version,
"Build": version.Build, "Build": version.Build,
"YtdlpVersion": ytdl.GetVersion(), "YtdlpVersion": r.ytdl.Version,
}, "views/layouts/main") }, "views/layouts/main")
} }

View File

@ -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
}

View File

@ -1,7 +1,10 @@
package ytdl package ytdl
import ( import (
"bytes"
"io" "io"
"os/exec"
"strings"
"go.fifitido.net/ytdl-web/config" "go.fifitido.net/ytdl-web/config"
"go.fifitido.net/ytdl-web/ytdl/cache" "go.fifitido.net/ytdl-web/ytdl/cache"
@ -12,19 +15,30 @@ import (
type Ytdl interface { type Ytdl interface {
GetMetadata(url string) (*metadata.Metadata, error) GetMetadata(url string) (*metadata.Metadata, error)
Download(w io.Writer, url, format string, index int) error Download(w io.Writer, url, format string, index int) error
Version() string
} }
type ytdlImpl struct { type ytdlImpl struct {
cfg *config.Config cfg *config.Config
logger *slog.Logger logger *slog.Logger
cache cache.MetadataCache cache cache.MetadataCache
version string
} }
func NewYtdl(cfg *config.Config, logger *slog.Logger, cache cache.MetadataCache) Ytdl { 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{ return &ytdlImpl{
cfg: cfg, cfg: cfg,
logger: logger.With(slog.String("module", "ytdl")), logger: logger.With(slog.String("module", "ytdl")),
cache: cache, cache: cache,
version: strings.TrimSpace(out.String()),
} }
} }
@ -56,6 +70,10 @@ func (y *ytdlImpl) baseOptions(url string) []Option {
return options return options
} }
func (y *ytdlImpl) Version() string {
return y.version
}
// GetMetadata implements Ytdl // GetMetadata implements Ytdl
func (y *ytdlImpl) GetMetadata(url string) (*metadata.Metadata, error) { func (y *ytdlImpl) GetMetadata(url string) (*metadata.Metadata, error) {
meta, err := y.cache.Get(url) meta, err := y.cache.Get(url)