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