diff options
-rw-r--r-- | dir.html | 2 | ||||
-rw-r--r-- | server.go | 24 |
2 files changed, 21 insertions, 5 deletions
@@ -9,7 +9,7 @@ <table> <tbody> {{range .ListFiles}} - <tr><td><a href="{{ .Path }}">{{ .Name }}</a></td><td>{{ .Size }}</td></tr> + <tr><td><a href="{{ .Path }}">{{ .Name }}</a></td><td>{{ .SizePrint }}</td></tr> {{end}} </tbody> </table> @@ -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 { |