-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
53 lines (43 loc) · 951 Bytes
/
utils.go
File metadata and controls
53 lines (43 loc) · 951 Bytes
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
package main
import (
"os"
"path/filepath"
)
func OsExists(path string) bool {
_, err := os.Stat(path)
return err != os.ErrNotExist
}
func IsDir(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
return fileInfo.IsDir(), nil
}
func GetSongPathList(paths []string) ([]string, error) {
songs := make([]string, 0)
for _, path := range paths {
isDir, err := IsDir(path)
if err != nil {
return songs, err
}
if isDir {
dir, err := os.Open(path)
if err != nil {
return songs, err
}
files, _ := dir.Readdirnames(0)
for _, fileName := range files {
songpath := filepath.Join(path, fileName)
isDir, errdir := IsDir(songpath)
if errdir != nil || isDir || filepath.Ext(songpath) != ".mp3" {
continue
}
songs = append(songs, songpath)
}
} else if filepath.Ext(path) == ".mp3" {
songs = append(songs, path)
}
}
return songs, nil
}