From 35bc85937d4ca09d2b9752086b25ef38cb6e82d9 Mon Sep 17 00:00:00 2001 From: ache Date: Wed, 20 Feb 2019 19:39:43 +0100 Subject: Size normalisation --- server.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'server.go') diff --git a/server.go b/server.go index b871654..3b5c732 100644 --- a/server.go +++ b/server.go @@ -21,6 +21,21 @@ func normaliseName( name string ) string { return name } +func normaliseSize( size int64) string { + unit := 0 + nb := float64(size) + for nb > 1023. { + unit++ + nb /= 1024. + } + + unitStr := " KMGTPEZY" + + if unit > 0 { + return fmt.Sprintf("%.2f%cio", nb, unitStr[unit]); + } + return fmt.Sprintf("%d", size); +} func getPath( r *http.Request ) string { if r.URL.Path == "/" { return "." @@ -52,6 +67,7 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) { Name string Path string Size int64 + SizePrint string } var files []Entry @@ -82,16 +98,16 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) { initPath := r.URL.Path; - cols = append(cols, Entry{".", initPath, 0}) + cols = append(cols, Entry{".", initPath, 0, ""}) if initPath != "/" { - cols = append(cols, Entry{"..", path.Dir(initPath), 0}) + cols = append(cols, Entry{"..", path.Dir(initPath), 0, ""}) initPath += "/" } for _, file := range fileInfo { if file.IsDir() { - cols = append(cols, Entry{normaliseName(file.Name()), initPath + file.Name(), 0}) + cols = append(cols, Entry{normaliseName(file.Name()), initPath + file.Name(), 0, ""}) } else { - files = append(files, Entry{normaliseName(file.Name()), initPath + file.Name(), file.Size()}) + files = append(files, Entry{normaliseName(file.Name()), initPath + file.Name(), file.Size(), normaliseSize(file.Size())}) } } type Collection struct { -- cgit v1.2.3