add utils.go

This commit is contained in:
codeskyblue
2016-07-23 23:46:56 +08:00
parent 3ef87dcaed
commit 162ed5fa97

33
utils.go Normal file
View File

@@ -0,0 +1,33 @@
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
)
func formatSize(file os.FileInfo) string {
if file.IsDir() {
return "-"
}
size := file.Size()
switch {
case size > 1024*1024:
return fmt.Sprintf("%.1fM", float64(size)/1024/1024)
case size > 1024:
return fmt.Sprintf("%.1fK", float64(size)/1024)
default:
return strconv.Itoa(int(size))
}
return ""
}
func getRealIP(req *http.Request) string {
xip := req.Header.Get("X-Real-IP")
if xip == "" {
xip = strings.Split(req.RemoteAddr, ":")[0]
}
return xip
}