/
file.go
63 lines (50 loc) · 1.12 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
63
package voice
import (
"errors"
"io/fs"
"math/rand"
"os"
"path/filepath"
"strings"
)
const (
defaultCacheDirAudio = "assets"
CacheDirAudioKey = "CACHE_AUDIO_DIR"
)
// FIXME added name of files or searching file in local storage by ID
func Path(id string) string {
return filepath.Join(assertDir(), id+".mp3")
}
func assertDir() (path string) {
path = defaultCacheDirAudio
if cacheDir, ok := os.LookupEnv(CacheDirAudioKey); ok && cacheDir != "" {
path = cacheDir
}
return
}
func AudioIDs() ([]string, error) {
dirs, err := os.ReadDir(assertDir())
if err != nil {
return nil, err
}
if len(dirs) <= 0 {
return nil, errors.New("assert directory is empty")
}
ids := make([]string, 0, len(dirs))
for _, dir := range dirs {
const suffix = ".mp3"
if dir.Type() == fs.ModeDir || !strings.HasSuffix(dir.Name(), suffix) {
continue
}
filename := strings.Split(dir.Name(), "-")
ids = append(ids, strings.TrimSuffix(filename[1], suffix))
}
return ids, nil
}
func RandomAudioID() (string, error) {
ids, err := AudioIDs()
if err != nil {
return "", err
}
return ids[rand.Int()%len(ids)], nil
}