forked from robbi5/gomumblesoundboard
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfiles.go
71 lines (61 loc) · 1.14 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
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
)
type File struct {
Name string `json:"name"`
Folder string `json:"folder"`
FullPath string
}
func (f File) String() string {
return f.Folder + "/" + f.Name
}
var soundfiles map[string]File
func scanDirsFunc(l string, info os.FileInfo, err error) error {
if err != nil {
return err
}
validSuffix := []string{
".mp3",
".m4a",
".ogg",
".flac",
".opus",
".wav",
".mpg",
}
validSuffixCheck := false
for _, s := range validSuffix {
if strings.HasSuffix(strings.ToLower(info.Name()), s) {
validSuffixCheck = true
}
}
if !validSuffixCheck {
return nil
}
if info.IsDir() == false {
fmt.Printf("File: %s\t%s\n", info.Name(), l)
dir, file := path.Split(l)
split := strings.Split(dir, "/")
f := File{
FullPath: l,
Name: file,
Folder: split[len(split)-2],
}
soundfiles[f.String()] = f
}
return nil
}
func scanDirs(directories []string) {
soundfiles = make(map[string]File)
for _, dir := range directories {
err := filepath.Walk(dir, scanDirsFunc)
if err != nil {
fmt.Printf("Error at %s: %v", dir, err)
}
}
}