-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
117 lines (105 loc) · 2.33 KB
/
files.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"io/ioutil"
"os"
fp "path/filepath"
"strings"
)
type Dir struct {
Root string
Dirs []string
Files []string
Images []string
AbsDirs []string
AbsFiles []string
AbsImages []string
}
func NewDir(path string) *Dir {
fi, err := os.Stat(path)
if err != nil {
fmt.Println(err)
return nil
}
if !fi.IsDir() {
return nil
} else {
dir := Dir{Root: path}
files, _ := ioutil.ReadDir(path)
for _, fi := range files {
absPath := fp.Join(path, fi.Name())
relPath := fi.Name()
if fi.IsDir() {
dir.Dirs = append(dir.Dirs, relPath)
dir.AbsDirs = append(dir.AbsDirs, absPath)
} else {
dir.Files = append(dir.Files, relPath)
dir.AbsFiles = append(dir.AbsFiles, absPath)
switch strings.ToLower(fp.Ext(relPath)) {
case ".jpg", ".jpeg", ".png", ".gif", ".bmp":
dir.Images = append(dir.Images, relPath)
dir.AbsImages = append(dir.AbsImages, absPath)
default:
}
}
}
return &dir
}
}
func size2text(size int64) string {
const ratio = 1024
size_float := float64(size)
units := []string{"B", "KB", "MB", "GB", "TB", "EB"}
index := 0
for ; size_float > ratio; index += 1 {
size_float /= ratio
}
return fmt.Sprintf("%.2f %s", size_float, units[index])
}
func get_size(path string) int64 {
fileInfo, err := os.Stat(path)
if err != nil {
panic(err)
}
return fileInfo.Size()
}
func some_files_size_int64(files []string) (total int64) {
for _, path := range files {
total += get_size(path)
}
return
}
func some_sub_dir_images_size_int64(dirs []string) (total int64) {
for _, path := range dirs {
tmp := NewDir(path)
total = total + some_files_size_int64(tmp.AbsImages) + some_sub_dir_images_size_int64(tmp.AbsDirs)
}
return
}
func file_size_str(path string) string {
return size2text(get_size(path))
}
func some_files_size_str(files []string) string {
var total int64
for _, file := range files {
total += get_size(file)
}
return size2text(total)
}
func dir_images_size_str(dir string) string {
tmp := NewDir(dir)
return size2text(some_files_size_int64(tmp.AbsImages) + some_sub_dir_images_size_int64(tmp.AbsDirs))
}
func hasPhoto(path string) bool {
dir := NewDir(path)
if len(dir.Images) > 0 {
return true
} else {
for _, subpath := range dir.AbsDirs {
if hasPhoto(subpath) {
return true
}
}
}
return false
}