forked from maxence-charriere/go-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.go
More file actions
58 lines (51 loc) · 1.3 KB
/
storage.go
File metadata and controls
58 lines (51 loc) · 1.3 KB
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
package app
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/murlokswarm/log"
)
// FileHaveExtension returns a boolean indicating whether or not name have an
// extension defined in exts.
func FileHaveExtension(name string, exts ...string) bool {
ext := filepath.Ext(name)
for _, e := range exts {
if ext == e {
return true
}
}
return false
}
// FileIsSupportedIcon returns a boolean indicating whether or not name is a
// supported icon.
func FileIsSupportedIcon(name string) bool {
return FileHaveExtension(name, ".jpg", ".jpeg", ".png")
}
// GetFilenamesFromDir returns the filenames within dirname.
// names are not prefixed by dirname.
func GetFilenamesFromDir(dirname string, extension ...string) (names []string) {
info, err := os.Stat(dirname)
if err != nil {
return
}
if !info.IsDir() {
log.Errorf("%v is not a directory", dirname)
return
}
files, _ := ioutil.ReadDir(dirname)
for _, f := range files {
if f.IsDir() {
subdirname := filepath.Join(dirname, f.Name())
subfilenames := GetFilenamesFromDir(subdirname, extension...)
for _, n := range subfilenames {
subfilename := filepath.Join(f.Name(), n)
names = append(names, subfilename)
}
continue
}
if FileHaveExtension(f.Name(), extension...) {
names = append(names, f.Name())
}
}
return
}