From 43bdf02ce93a7254a025a1ee07dbea6d17d77e57 Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Thu, 17 Oct 2024 06:13:25 -0600 Subject: [PATCH] Add disk space to status --- README.md | 4 ++-- handlers/status.go | 50 +++++++++++++++++++++++++++++++++++++++++++ templates/status.html | 4 ++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 06b26d9..705d875 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,8 @@ Build and push this container to ghcr - [x] Environment variable to control whether "Secure" flag set on cookie - [x] Allow custom FPS for video transcode - [ ] Provide an about page - - `ffmpeg` version - - `yt-dlp` version + - [x] `ffmpeg` version + - [x] `yt-dlp` version - disk space - [ ] skip buttons for audio player - [ ] Track progress via video URL rather than ID diff --git a/handlers/status.go b/handlers/status.go index 179f604..5ea4252 100644 --- a/handlers/status.go +++ b/handlers/status.go @@ -1,14 +1,50 @@ package handlers import ( + "fmt" "net/http" + "os" + "path/filepath" "github.com/labstack/echo/v4" + "golang.org/x/sys/unix" + "ytdlp-site/config" "ytdlp-site/ffmpeg" "ytdlp-site/ytdlp" ) +// GetFreeSpace returns the free space in bytes for the filesystem containing the given directory +func getFreeSpace(dir string) (uint64, error) { + var stat unix.Statfs_t + err := unix.Statfs(dir, &stat) + if err != nil { + return 0, fmt.Errorf("error getting filesystem stats: %v", err) + } + + // Calculate free space + freeSpace := stat.Bavail * uint64(stat.Bsize) + return freeSpace, nil +} + +// GetDirectorySize calculates the total size of a directory in bytes +func getDirectorySize(dir string) (int64, error) { + var size int64 + err := filepath.Walk(dir, func(_ string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() { + size += info.Size() + } + return nil + }) + if err != nil { + return 0, fmt.Errorf("error walking directory: %v", err) + } + return size, nil +} + func StatusGet(c echo.Context) error { ytdlpStdout, _, err := ytdlp.Run("--version") @@ -20,9 +56,23 @@ func StatusGet(c echo.Context) error { log.Errorln(err) } + free, err := getFreeSpace(config.GetDataDir()) + if err != nil { + log.Errorln(err) + } + used, err := getDirectorySize(config.GetDataDir()) + if err != nil { + log.Errorln(err) + } + + freeMiB := float64(free) / 1024 / 1024 + usedMiB := float64(used) / 1024 / 1024 + return c.Render(http.StatusOK, "status.html", map[string]interface{}{ "ytdlp": string(ytdlpStdout), "ffmpeg": string(ffmpegStdout), + "free": fmt.Sprintf("%.2f", freeMiB), + "used": fmt.Sprintf("%.2f", usedMiB), "Footer": MakeFooter(), }) } diff --git a/templates/status.html b/templates/status.html index f969eba..695acf9 100644 --- a/templates/status.html +++ b/templates/status.html @@ -28,6 +28,10 @@ {{.ffmpeg}} +
+

Disk

+ {{.used}} MiB ({{.free}} MiB remaning) +
{{template "footer" .}}