From 055e9db8e29db15e08dde1b6a649778a07eea9ef Mon Sep 17 00:00:00 2001 From: ston1th Date: Wed, 5 Oct 2022 20:35:15 +0200 Subject: [PATCH] added file size --- pkg/fs/fs.go | 28 ++++++++++++++++++++++++++++ pkg/srv/interceptor.go | 2 ++ pkg/srv/templates/cache.html | 2 +- pkg/srv/templates/index.html | 2 +- pkg/srv/templates/preloads.html | 1 + 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pkg/fs/fs.go b/pkg/fs/fs.go index f8ef58c..93d5546 100644 --- a/pkg/fs/fs.go +++ b/pkg/fs/fs.go @@ -12,6 +12,7 @@ import ( stdfs "io/fs" "net/http" "os" + "strconv" "strings" "github.com/go-logr/logr" @@ -100,6 +101,33 @@ func (fs *FS) Stat(name string) (fi stdfs.FileInfo, err error) { return } +const ( + _ = 1 << (iota * 10) + KiB + MiB + GiB + TiB +) + +func (fs *FS) FileSize(name string) (size string) { + fi, err := fs.Stat(name) + if err != nil { + return + } + s := fi.Size() + switch { + case s >= TiB: + return strconv.Itoa(int(s/TiB)) + " TiB" + case s >= GiB: + return strconv.Itoa(int(s/GiB)) + " GiB" + case s >= MiB: + return strconv.Itoa(int(s/MiB)) + " MiB" + case s >= KiB: + return strconv.Itoa(int(s/KiB)) + " KiB" + } + return strconv.Itoa(int(s)) + " B" +} + func (fs *FS) RemoveDst(name string) error { return fs.dst.Remove(name) } diff --git a/pkg/srv/interceptor.go b/pkg/srv/interceptor.go index 6b840c5..00f3d15 100644 --- a/pkg/srv/interceptor.go +++ b/pkg/srv/interceptor.go @@ -59,6 +59,7 @@ type file struct { Name template.HTML URI template.HTML Anchor string + Size string Status int Prio int Errc int @@ -122,6 +123,7 @@ func (r *responseInterceptor) GetPaths(path string, fs *fs.FS, relative bool) (d Name: name, URI: uri, Anchor: anchor(p), + Size: fs.FileSize(full), Status: fs.CacheStatus(full), }) } diff --git a/pkg/srv/templates/cache.html b/pkg/srv/templates/cache.html index 7b5dca7..0bfc06b 100644 --- a/pkg/srv/templates/cache.html +++ b/pkg/srv/templates/cache.html @@ -17,7 +17,7 @@ {{$s.Name}} - {{if ge $s.Status 0}}{{$s.Status}}%{{end}}[v] + {{if ge $s.Status 0}}{{$s.Status}}%{{end}}{{$s.Size}}[v] diff --git a/pkg/srv/templates/index.html b/pkg/srv/templates/index.html index 18ecf88..55e44f3 100644 --- a/pkg/srv/templates/index.html +++ b/pkg/srv/templates/index.html @@ -20,7 +20,7 @@ {{$s.Name}} - {{if ge $s.Status 0}}{{$s.Status}}%{{end}}[v][n][p] + {{if ge $s.Status 0}}{{$s.Status}}%{{end}}{{$s.Size}}[v][n][p] diff --git a/pkg/srv/templates/preloads.html b/pkg/srv/templates/preloads.html index 86e242e..2fd6b1a 100644 --- a/pkg/srv/templates/preloads.html +++ b/pkg/srv/templates/preloads.html @@ -24,6 +24,7 @@ Quota: {{.QuotaCur}} / {{.QuotaMax}} GiB [queued] {{end -}} {{end -}} + {{$s.Size}} {{$s.Prio}}[v][s][+][-]