-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (74 loc) · 1.54 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
package main
import (
"context"
"flag"
"log"
"path/filepath"
"cloud.google.com/go/storage"
firebase "firebase.google.com/go"
"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
)
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.Parse()
if bucketURL == "" {
log.Fatalln("bucketURL must be set")
}
config := &firebase.Config{
StorageBucket: bucketURL,
}
opt := option.WithCredentialsFile(credPath)
ctx := context.Background()
app, err := firebase.NewApp(ctx, config, opt)
if err != nil {
log.Fatalln(err)
}
client, err := app.Storage(context.Background())
if err != nil {
log.Fatalln(err)
}
bucket, err := client.DefaultBucket()
if err != nil {
log.Fatalln(err)
}
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
}
log.Printf("Updating %s\n", objectAttr.Name)
obj := bucket.Object(objectAttr.Name)
if _, err := obj.Update(
ctx,
storage.ObjectAttrsToUpdate{
CacheControl: "public, max-age=86400",
},
); err != nil {
panic(err)
}
}
}