-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (84 loc) · 2.01 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
package main
import (
"context"
"flag"
"log"
"path/filepath"
"strings"
firebase "firebase.google.com/go"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/lon9/SaySoundCloud/backend/models"
"google.golang.org/api/iterator"
"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
}
func main() {
var (
bucketURL string
credPath string
dbURL string
dbProvider string
)
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(&dbURL, "u", "../../backend/database/dev.db", "Database url")
flag.StringVar(&dbProvider, "p", "sqlite3", "Database provider. sqlite3 or postgres or mysql")
flag.Parse()
if bucketURL == "" {
log.Fatalln("bucketURL must be set")
}
config := &firebase.Config{
StorageBucket: bucketURL,
}
ctx := context.Background()
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)
}
db, err := gorm.Open(dbProvider, dbURL)
if err != nil {
panic(err)
}
defer db.Close()
it := bucket.Objects(ctx, nil)
for {
objectAttr, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Println(err)
continue
}
ext := filepath.Ext(objectAttr.Name)
if !isValidExt(ext) {
continue
}
sound := &models.Sound{
Name: strings.ReplaceAll(filepath.Base(objectAttr.Name), ext, ""),
Path: filepath.Base(filepath.Dir(objectAttr.Name)) + "/" + filepath.Base(objectAttr.Name),
}
if err := db.Create(sound).Error; err != nil {
log.Println(err)
}
}
}