-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (188 loc) · 4.59 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"context"
"crypto/md5"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"cloud.google.com/go/storage"
firebase "firebase.google.com/go"
"google.golang.org/api/option"
)
var exts = []string{".mp3", ".wav", ".ogg", ".aac"}
func isValidExt(ext string) bool {
for _, e := range exts {
if ext == e {
return true
}
}
return false
}
type localSound struct {
CmdName string
FileName string
Path string
Ext string
}
func isOnFirebase(ctx context.Context, bucket *storage.BucketHandle, cmdName, fileName string) (bool, error) {
h := md5.New()
io.WriteString(h, cmdName)
p := filepath.Join("sounds", fmt.Sprintf("%x", h.Sum(nil))[:2], fileName)
_, err := bucket.Object(p).Attrs(ctx)
if err == storage.ErrObjectNotExist {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func main() {
var (
bucketURL string
credPath string
inputDir string
outputDir string
dryRun bool
)
flag.StringVar(&bucketURL, "b", "", "URL of the storage bucket")
flag.StringVar(&credPath, "c", "../../backend/config/firebase/firebase.json", "Path for file of firebase credential")
flag.StringVar(&inputDir, "i", "../../sounds/saysound/ps_saysounds_2019_0118/sound/misc/", "Input directory")
flag.StringVar(&outputDir, "o", "../../sounds/saysound/output", "Output directory")
flag.BoolVar(&dryRun, "d", false, "Dry run")
flag.Parse()
if bucketURL == "" {
log.Fatalln("bucketURL must be set")
}
ctx := context.Background()
config := &firebase.Config{
StorageBucket: bucketURL,
}
app, err := firebase.NewApp(ctx, config, option.WithCredentialsFile(credPath))
if err != nil {
panic(err)
}
client, err := app.Storage(ctx)
if err != nil {
panic(err)
}
bucket, err := client.DefaultBucket()
if err != nil {
panic(err)
}
sounds := make(map[string][]localSound)
newSounds := make(map[string][]localSound)
// get files
var numSounds int
if err := filepath.Walk(inputDir, func(p string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
ext := filepath.Ext(p)
if !isValidExt(ext) {
return nil
}
sound := localSound{
FileName: filepath.Base(p),
CmdName: strings.ReplaceAll(filepath.Base(p), ext, ""),
Path: p,
Ext: ext,
}
if _, ok := sounds[sound.CmdName]; ok {
sounds[sound.CmdName] = append(sounds[sound.CmdName], sound)
newSounds[sound.CmdName] = append(newSounds[sound.CmdName], sound)
} else {
sounds[sound.CmdName] = []localSound{sound}
newSounds[sound.CmdName] = []localSound{sound}
}
numSounds++
return nil
}); err != nil {
panic(err)
}
// resolve command name
var dupIdx int
for k, v := range sounds {
if len(v) > 1 {
for _, cmd := range v[1:] {
var newName string
lastWord := cmd.CmdName[len(cmd.CmdName)-1]
index, err := strconv.Atoi(string(lastWord))
if err != nil {
index = 1
for {
newName = cmd.CmdName + strconv.Itoa(index)
onFirebase, err := isOnFirebase(ctx, bucket, newName, newName+cmd.Ext)
if err != nil {
panic(err)
}
if _, ok := newSounds[newName]; ok || onFirebase {
index++
continue
}
break
}
} else {
for {
index++
newName = cmd.CmdName[:len(cmd.CmdName)-1] + strconv.Itoa(index)
onFirebase, err := isOnFirebase(ctx, bucket, newName, newName+cmd.Ext)
if err != nil {
panic(err)
}
if _, ok := newSounds[newName]; ok || onFirebase {
continue
}
break
}
}
prevPath := cmd.Path
cmd.FileName = newName + cmd.Ext
cmd.CmdName = newName
cmd.Path = filepath.Join(filepath.Dir(cmd.Path), cmd.FileName)
newSounds[cmd.CmdName] = []localSound{cmd}
log.Printf("%d: %s->%s", dupIdx+1, prevPath, cmd.Path)
dupIdx++
if !dryRun {
if err := os.Rename(prevPath, cmd.Path); err != nil {
panic(err)
}
}
}
}
newSounds[k] = []localSound{v[0]}
}
// assertion
for _, v := range newSounds {
if len(v) != 1 {
log.Fatal("invlid length")
}
}
if len(newSounds) != numSounds {
log.Fatalf("The number of sounds is invalid %d:%d", len(newSounds), numSounds)
}
// Move to output directory
for _, v := range newSounds {
src := v[0].Path
h := md5.New()
io.WriteString(h, v[0].CmdName)
hashed := fmt.Sprintf("%x", h.Sum(nil))
dir := hashed[:2]
dirPath := filepath.Join(outputDir, dir)
dst := filepath.Join(dirPath, v[0].FileName)
log.Printf("%s->%s", src, dst)
if !dryRun {
if err := os.MkdirAll(dirPath, 0755); err != nil {
panic(err)
}
if err := os.Rename(src, dst); err != nil {
panic(err)
}
}
}
}