/
file.go
62 lines (55 loc) · 1.15 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 goutils
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
// CheckFile checks if file exists returns "dir"|"file"|""
func CheckFile(filename string) string {
info, err := os.Stat(filename)
if err != nil {
return ""
}
if info.IsDir() {
return "dir"
}
return "file"
}
// ReadDir returns the filenames in directory
func ReadDir(dir string) []string {
filenames := []string{}
files, err := ioutil.ReadDir(dir)
if err != nil {
return filenames
}
for _, file := range files {
filenames = append(filenames, path.Join(dir, file.Name()))
}
return filenames
}
// ReadDirRecursive reads the directory recursively by given depth
func ReadDirRecursive(dir string) []string {
filenames := []string{}
filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.Contains(info.Name(), ".jpg") {
filenames = append(filenames, path)
}
return nil
})
return filenames
}
// MkdirIfNot creates the dir if not exists
func MkdirIfNot(dir string) error {
_, err := os.Stat(dir)
if os.IsNotExist(err) {
os.Mkdir(dir, 0777)
return MkdirIfNot(dir)
}
return nil
}