Add disk space to status

This commit is contained in:
Carl Pearson
2024-10-17 06:13:25 -06:00
parent 4087212daa
commit 43bdf02ce9
3 changed files with 56 additions and 2 deletions

View File

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

View File

@@ -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(),
})
}

View File

@@ -28,6 +28,10 @@
{{.ffmpeg}}
</div>
</div>
<div class="disk-space card">
<h2>Disk</h2>
{{.used}} MiB ({{.free}} MiB remaning)
</div>
</div>
{{template "footer" .}}
</body>