-
Notifications
You must be signed in to change notification settings - Fork 14
/
helpers.go
138 lines (124 loc) · 2.81 KB
/
helpers.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"io/fs"
"os"
"path/filepath"
"github.com/tcolgate/mp3"
)
var loglevel int8 = 1
func logM(level int8, message ...any) {
if loglevel >= level {
fmt.Println(message...)
}
}
func filter[T any](ss []T, test func(T) bool) (ret []T) {
for _, s := range ss {
if test(s) {
ret = append(ret, s)
}
}
return
}
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func unwrapHelper(cachepath string, relpath string, entries []fs.DirEntry) {
for _, e := range entries {
//embed path uses forward slash
embedPath := relpath + "/" + e.Name()
absolutePath := filepath.Join(cachepath, embedPath)
logM(4, "embedding", embedPath, "to", absolutePath)
if e.IsDir() {
os.MkdirAll(absolutePath, 0777)
childEntries, _ := defaults.ReadDir(embedPath)
logM(4, "calling unwrap recursively", cachepath, embedPath, childEntries)
unwrapHelper(cachepath, embedPath, childEntries)
} else {
embeddedFile, _ := defaults.Open(embedPath)
defer embeddedFile.Close()
destinationFile, _ := os.Create(absolutePath)
defer destinationFile.Close()
_, e := io.Copy(destinationFile, embeddedFile)
if e != nil {
fmt.Println(absolutePath)
Fatal("Error copying file:", e)
}
}
}
}
func unwrapEmbeddedDefaultCharacter() {
logM(1, "Unpacking Default Files (Concurrent)...")
defaultsPath := filepath.Join(generateDir, "defaults")
defaultsPathExists, _ := pathExists(defaultsPath)
if defaultsPathExists {
os.Remove(defaultsPath)
}
dir, err := defaults.ReadDir("defaults")
if err != nil {
Fatal(err)
}
unwrapHelper(generateDir, "defaults", dir)
}
func openImage(filePath string) image.Image {
f, err := os.Open(filePath)
if err != nil {
fmt.Println("Error opening image at ", filePath)
Fatal(err)
}
defer f.Close()
image, _, err := image.Decode(f)
if err != nil {
fmt.Println("Error decoding image at ", filePath)
Fatal(err)
}
return image
}
func getAudioFileDuration(filePath string) float64 {
file, err := os.Open(filePath)
if err != nil {
Fatal(err)
}
defer file.Close()
switch getFileFormat(filePath) {
case "mp3":
t := 0.0
d := mp3.NewDecoder(file)
var f mp3.Frame
skipped := 0
for {
if err := d.Decode(&f, &skipped); err != nil {
if err == io.EOF {
break
}
Fatal(err)
}
t = t + f.Duration().Seconds()
}
return t
default:
Fatal("unsupported audio file format, please use an mp3")
}
return -1
}
// helper function to extract the file format from file path
func getFileFormat(filePath string) string {
fileExtension := filePath[len(filePath)-3:]
if fileExtension == "wav" {
return "wav"
} else if fileExtension == "mp3" {
return "mp3"
}
return ""
}