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),
"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")
}

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
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
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,
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)