Display size of file served when verbose flag is passed

This commit is contained in:
Seednode 2022-10-25 00:11:55 -05:00
parent 1fe42cb217
commit 3801421567
3 changed files with 21 additions and 2 deletions

View File

@ -79,6 +79,24 @@ func (p *Path) Decrement() {
p.Number = p.Number - 1 p.Number = p.Number - 1
} }
func humanReadableSize(bytes int) string {
const unit = 1000
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(bytes)/float64(div), "KMGTPE"[exp])
}
func preparePath(path string) string { func preparePath(path string) string {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
path = fmt.Sprintf("/%v", filepath.ToSlash(path)) path = fmt.Sprintf("/%v", filepath.ToSlash(path))

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version = "0.18.3" var Version = "0.18.4"
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)

View File

@ -218,9 +218,10 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
w.Write(buf) w.Write(buf)
if Verbose { if Verbose {
fmt.Printf("%v | Served \"%v\" to %v in %v\n", fmt.Printf("%v | Served %v (%v) to %v in %v\n",
startTime.Format(LOGDATE), startTime.Format(LOGDATE),
filePath, filePath,
humanReadableSize(len(buf)),
r.RemoteAddr, r.RemoteAddr,
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
) )