-
Notifications
You must be signed in to change notification settings - Fork 3
/
file.go
62 lines (53 loc) · 1.13 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package nav
import (
"fmt"
"io/fs"
)
type FileInfo struct {
Name string `json:"name"`
RawSize int64 `json:"rawSize"`
IsDir bool `json:"isDir"`
Mode string `json:"mode"`
Username string `json:"username"`
GroupName string `json:"groupName"`
Size string `json:"size"`
ModTime string `json:"modTime"`
}
func NewFileInfo(name string, f fs.FileInfo) *FileInfo {
username, groupName := sysNames(f)
return &FileInfo{
Name: name,
RawSize: f.Size(),
IsDir: f.IsDir(),
Mode: f.Mode().String(),
Username: username,
GroupName: groupName,
Size: readableSize(f.Size()),
ModTime: f.ModTime().Format("2006-01-02 15:04:05Z07:00"),
}
}
func readableSize(size int64) string {
if size < 1000 {
return fmt.Sprintf("%dB", size)
}
suffix := []string{
"K", // kilo
"M", // mega
"G", // giga
"T", // tera
"P", // peta
"E", // exa
"Z", // zeta
"Y", // yotta
}
curr := float64(size) / 1000
for _, s := range suffix {
if curr < 10 {
return fmt.Sprintf("%.1f%s", curr-0.0499, s)
} else if curr < 1000 {
return fmt.Sprintf("%d%s", int(curr), s)
}
curr /= 1000
}
return ""
}