From b13919543718f886269058bff2624d4f48ce41d7 Mon Sep 17 00:00:00 2001 From: rroller Date: Sun, 15 Dec 2024 13:02:38 -0800 Subject: [PATCH] Add about page --- go.mod | 1 + go.sum | 2 ++ src/main.go | 1 + src/media/about.go | 42 +++++++++++++++++++++++++++++ src/media/updater.go | 14 ++-------- src/utils/commands.go | 23 ++++++++++++++++ templates/media/about.html | 55 ++++++++++++++++++++++++++++++++++++++ templates/media/index.html | 5 ++-- 8 files changed, 128 insertions(+), 15 deletions(-) create mode 100644 src/media/about.go create mode 100644 src/utils/commands.go create mode 100644 templates/media/about.html diff --git a/go.mod b/go.mod index 6f9d0a5..cb6bd1e 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.23 require ( github.com/dustin/go-humanize v1.0.1 github.com/go-chi/chi/v5 v5.1.0 + github.com/matishsiao/goInfo v0.0.0-20240924010139-10388a85396f github.com/rs/zerolog v1.33.0 golang.org/x/sync v0.10.0 ) diff --git a/go.sum b/go.sum index 4ae980e..58c7210 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+m github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/matishsiao/goInfo v0.0.0-20240924010139-10388a85396f h1:XDrsC/9hdgiU9ecceSmYsS2E3fBtFiYc34dAMFgegnM= +github.com/matishsiao/goInfo v0.0.0-20240924010139-10388a85396f/go.mod h1:aEt7p9Rvh67BYApmZwNDPpgircTO2kgdmDUoF/1QmwA= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= diff --git a/src/main.go b/src/main.go index 243fd8e..e2e4aa4 100644 --- a/src/main.go +++ b/src/main.go @@ -24,6 +24,7 @@ func main() { router.Get("/fetch", media.FetchMedia) router.Get("/api/download", media.FetchMediaApi) router.Get("/download", media.ServeMedia) + router.Get("/about", media.AboutIndex) }) fileServer(router, "/static", "static/") diff --git a/src/media/about.go b/src/media/about.go new file mode 100644 index 0000000..1b75574 --- /dev/null +++ b/src/media/about.go @@ -0,0 +1,42 @@ +package media + +import ( + "github.com/matishsiao/goInfo" + "github.com/rs/zerolog/log" + "html/template" + "media-roller/src/utils" + "net/http" + "regexp" + "strings" +) + +var aboutIndexTmpl = template.Must(template.ParseFiles("templates/media/about.html")) + +var newlineRegex = regexp.MustCompile("\r?\n") + +func AboutIndex(w http.ResponseWriter, _ *http.Request) { + pythonVersion := utils.RunCommand("python3", "--version") + if pythonVersion == "" { + pythonVersion = utils.RunCommand("python", "--version") + } + + gi, _ := goInfo.GetInfo() + + data := map[string]interface{}{ + "ytDlpVersion": CachedYtDlpVersion, + "goVersion": strings.TrimPrefix(utils.RunCommand("go", "version"), "go version "), + "pythonVersion": strings.TrimPrefix(pythonVersion, "Python "), + "ffmpegVersion": newlineRegex.Split(utils.RunCommand("ffmpeg", "-version"), -1), + "os": gi.OS, + "kernel": gi.Kernel, + "core": gi.Core, + "platform": gi.Platform, + "hostname": gi.Hostname, + "cpus": gi.CPUs, + } + + if err := aboutIndexTmpl.Execute(w, data); err != nil { + log.Error().Msgf("Error rendering template: %v", err) + http.Error(w, "Internal error", http.StatusInternalServerError) + } +} diff --git a/src/media/updater.go b/src/media/updater.go index e320d6f..041cf33 100644 --- a/src/media/updater.go +++ b/src/media/updater.go @@ -4,9 +4,9 @@ import ( "bytes" "github.com/rs/zerolog/log" "io" + "media-roller/src/utils" "os" "os/exec" - "strings" "sync" ) @@ -61,17 +61,7 @@ func UpdateYtDlp() error { } func GetInstalledVersion() string { - cmd := exec.Command("yt-dlp", "--version") - - var s bytes.Buffer - cmd.Stdout = &s - cmd.Stderr = os.Stderr - - if err := cmd.Run(); err != nil { - log.Error().Err(err).Msgf("Error getting installed version") - } - - version := strings.TrimSpace(s.String()) + version := utils.RunCommand("yt-dlp", "--version") if version == "" { version = "unknown" } diff --git a/src/utils/commands.go b/src/utils/commands.go new file mode 100644 index 0000000..eb17512 --- /dev/null +++ b/src/utils/commands.go @@ -0,0 +1,23 @@ +package utils + +import ( + "bytes" + "github.com/rs/zerolog/log" + "os" + "os/exec" + "strings" +) + +func RunCommand(name string, args ...string) string { + cmd := exec.Command(name, args...) + + var s bytes.Buffer + cmd.Stdout = &s + cmd.Stderr = os.Stderr + + if err := cmd.Run(); err != nil { + log.Error().Err(err).Msgf("Error running command " + strings.Join(args, " ")) + } + + return strings.TrimSpace(s.String()) +} diff --git a/templates/media/about.html b/templates/media/about.html new file mode 100644 index 0000000..f8954d2 --- /dev/null +++ b/templates/media/about.html @@ -0,0 +1,55 @@ + + + + about - media-roller + + + + + +
+
+
+

media roller

+
+ {{ .details }} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Sourcehttps://github.com/rroller/media-roller +
yt-dlp{{ $.ytDlpVersion }}
Golang{{ $.goVersion }}
Python{{ $.pythonVersion }}
os{{ $.os }}
kernel{{ $.kernel }}
core{{ $.core }}
platform{{ $.platform }}
hostname{{ $.hostname }}
cpus{{ $.cpus }}
ffmpeg{{range $element := .ffmpegVersion}}{{ $element }}
{{end}}
+
+
+
+
+
+
+
+ + diff --git a/templates/media/index.html b/templates/media/index.html index 6c226ff..af1e995 100644 --- a/templates/media/index.html +++ b/templates/media/index.html @@ -45,9 +45,8 @@

Error