forked from andreweick/emms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (122 loc) · 2.82 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
// main.go
package main
import (
"log"
"os"
"strings"
"time"
f "github.com/fauna/faunadb-go/v4/faunadb"
)
var (
secret = os.Getenv("FAUNADB_SECRET")
endpoint = f.Endpoint("https://db.us.fauna.com")
client = f.NewFaunaClient(secret, endpoint)
dbName = "emms"
)
/*
* Check for the existencse of the database. If it exists, return true,
* otherwise create it.
*/
func createDatabase() {
res, err := client.Query(
f.If(
f.Exists(f.Database(dbName)),
true,
f.CreateDatabase((f.Obj{"name": dbName}))))
if err != nil {
panic(err)
}
if res != f.BooleanV(true) {
log.Printf("Created Database: %s\n", dbName)
} else {
log.Printf("Database: %s, Already Exists\n", dbName)
}
}
func getDbClient() (dbClient *f.FaunaClient) {
var res f.Value
var err error
var dbSecret string
res, err = client.Query(
f.CreateKey(f.Obj{
"database": f.Database(dbName),
"role": "server",
}))
if err != nil {
panic(err)
}
err = res.At(f.ObjKey("secret")).Get(&dbSecret)
if err != nil {
panic(err)
}
log.Printf("Database: %s, specific key: %s\n", dbName, dbSecret)
dbClient = client.NewSessionClient(dbSecret)
return
}
func createCollection(collectionName string, dbClient *f.FaunaClient) {
res, err := dbClient.Query(
f.If(
f.Exists(f.Collection(collectionName)),
true,
f.CreateCollection(f.Obj{"name": collectionName})))
if err != nil {
panic(err)
}
if res != f.BooleanV(true) {
log.Printf("Created Collection: %s\n", collectionName)
} else {
log.Printf("Collection: %s, Already Exists\n", collectionName)
}
}
func main() {
createDatabase()
dbClient := getDbClient()
createCollection("photographs", dbClient)
pmd := PhotoMetaData{
Name: "test.jpg",
ParsedName: "testParsedName",
Artist: "testArtist",
CaptureTime: time.Now(),
CaptureYear: "2020",
CaptureYearMonth: "2020-01",
CaptureYearMonthDay: "2020-01-01",
Description: "testDescription",
Caption: "testCaption",
ID: 1,
Height: 100,
Width: 100,
}
_, err := dbClient.Query(
f.Create(
f.Collection("photographs"),
f.Obj{"data": pmd},
),
)
if err != nil {
panic(err)
}
// enumerate directory
files, err := os.ReadDir("/Volumes/Mini Pudge/edc/photographs/")
if err != nil {
panic(err)
}
for _, fi := range files {
if !fi.IsDir() && strings.HasSuffix(fi.Name(), "jpg") {
pmd := populatePMD("/Volumes/Mini Pudge/edc/photographs/" + fi.Name())
pmd.Name = fi.Name()
_, err := dbClient.Query(
f.Create(
f.Collection("photographs"),
f.Obj{"data": pmd},
),
)
if err != nil {
panic(err)
}
}
}
// res, err := client.Query(f.Get(f.Ref(f.Collection("products"), "202")))
// if err != nil {
// panic(err)
// }
// log.Println(res)
}