forked from jessevdk/webgl-play
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
399 lines (317 loc) · 7.39 KB
/
database.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package main
import (
"database/sql"
"fmt"
"log"
"math/rand"
"os"
"path"
"strings"
"time"
sqlite3 "github.com/mattn/go-sqlite3"
)
type Db struct {
*sql.DB
}
var db Db
const databaseVersion int32 = 1
const (
StateNew = iota
StatePublished
StateRevision
StateDeleted
)
func (d *Db) createIndices(tx *sql.Tx, table string, unique bool, fields ...[]string) {
for _, nfield := range fields {
field := strings.Join(nfield, ", ")
name := strings.Join(nfield, "_")
q := "CREATE "
if unique {
q += "UNIQUE "
}
q += "INDEX " + table + "_" + name + " ON " + table + " (" + field + ")"
if _, err := tx.Exec(q); err != nil {
panic(err)
}
}
}
func (d *Db) generateToken(length int) string {
const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
ret := make([]byte, length)
for i := 0; i < length; i++ {
r := rand.Intn(len(alphabet))
ret[i] = alphabet[r]
}
return string(ret[:])
}
func (d *Db) DeleteRequest(token string) {
d.Exec("DELETE FROM gallery WHERE token = ? AND state = ?", token, StateNew)
}
const DefaultTokenLength = 6
var tokenLength = DefaultTokenLength
func (d *Db) NewRequest() (string, error) {
tries := 0
for {
if tries > 5 {
tokenLength++
tries = 0
}
tries++
tok := d.generateToken(tokenLength)
_, err := d.Exec(`INSERT INTO gallery (token, state, modificationDate) VALUES (?, ?, ?)`, tok, StateNew, time.Now())
if err == nil {
return tok, nil
}
// Retry on collisions with existing tokens
if err != nil && err.(*sqlite3.Error).ExtendedCode != sqlite3.ErrConstraintPrimaryKey {
log.Printf("Failed to generate new request: %v", err)
return "", err
}
}
}
func (d *Db) Migrate() {
row := db.QueryRow("PRAGMA user_version")
var vers int32
if err := row.Scan(&vers); err != nil {
panic(err)
}
if vers == databaseVersion {
return
}
tx, err := db.Begin()
if err != nil {
panic(err)
}
defer func() {
if tx != nil {
tx.Rollback()
}
}()
if vers < 1 {
if _, err := tx.Exec(`CREATE TABLE gallery (
id INTEGER PRIMARY KEY AUTOINCREMENT,
parent INTEGER DEFAULT 0,
token TEXT UNIQUE,
document TEXT,
title TEXT,
description TEXT,
screenshot TEXT,
author TEXT,
license TEXT,
views INTEGER DEFAULT 0,
modificationDate DATETIME,
state INTEGER DEFAULT 0
)`); err != nil {
panic(err)
}
d.createIndices(tx, "gallery", false,
[]string{"token"},
[]string{"views", "state"},
[]string{"modificationDate", "state"})
if _, err := tx.Exec(`CREATE TABLE views (
id INTEGER,
ip TEXT
)`); err != nil {
panic(err)
}
d.createIndices(tx, "views", true, []string{"id", "ip"})
}
if _, err := tx.Exec(fmt.Sprintf("PRAGMA user_version = %v", databaseVersion)); err != nil {
panic(err)
}
if err := tx.Commit(); err != nil {
panic(err)
}
tx = nil
}
type GalleryItem struct {
Id int `json:"id"`
Parent int `json:"parent"`
Token string `json:"-"`
Document string `json:"document"`
Title string `json:"title"`
Description string `json:"description"`
Screenshot string `json:"screenshot"`
Author string `json:"author"`
License string `json:"license"`
Views int `json:"views"`
ModificationDate time.Time `json:"modificationDate"`
State int `json:"-"`
}
func (d *Db) PutGallery(item *GalleryItem, screenshotData []byte) error {
tx, err := d.Begin()
defer func() {
if tx != nil {
tx.Rollback()
}
}()
if err != nil {
return err
}
// Transfer views
cur := tx.QueryRow("SELECT id, parent, views, state FROM gallery WHERE token = ?", item.Token)
state := 0
if cur != nil {
if err := cur.Scan(&item.Id, &item.Parent, &item.Views, &state); err != nil {
log.Printf("Error while scanning current document: %v", err)
return err
}
}
// Demote current document to revision
if state != StateNew {
if _, err := tx.Exec(`
UPDATE OR FAIL
gallery
SET
state = ?,
token = NULL
WHERE
token = ?
`, StateRevision, item.Token); err != nil {
log.Printf("Error while demoting document to revision: %v", err)
return err
}
} else {
if _, err := tx.Exec(`
DELETE FROM
gallery
WHERE
token = ?
`, item.Token); err != nil {
log.Printf("Error while deleting original publishing request: %v", err)
return err
}
}
if screenshotId, err := ScreenshotsStorage.Store(screenshotData); err != nil {
return err
} else {
item.Screenshot = screenshotId
}
item.ModificationDate = time.Now()
item.State = StatePublished
ret, err := tx.Exec(`
INSERT INTO
gallery
(
parent, token, document, title, description, screenshot, author, license, views, modificationDate, state
) VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)`,
item.Parent,
item.Token,
item.Document,
item.Title,
item.Description,
item.Screenshot,
item.Author,
item.License,
item.Views,
item.ModificationDate,
item.State)
if err != nil {
log.Printf("Error while inserting new document: %v", err)
return err
}
nid, err := ret.LastInsertId()
if err != nil {
log.Printf("Error while obtaining newly inserted document id: %v", err)
return err
}
if err := tx.Commit(); err != nil {
log.Printf("Error while committing document update the transaction: %v", err)
return err
}
tx = nil
item.Id = int(nid)
return nil
}
func (d *Db) Gallery(page int, n int, sort string, reversed bool) ([]*GalleryItem, error) {
var orderBy string
switch sort {
case "views":
orderBy = "views"
default:
orderBy = "modificationDate"
}
var orderDir string
if !reversed {
orderDir = "DESC"
} else {
orderDir = "ASC"
}
q := fmt.Sprintf(`
SELECT
id,
parent,
document,
title,
description,
screenshot,
author,
license,
views,
modificationDate
FROM
gallery
WHERE
state = ?
ORDER BY
%s %s
LIMIT
%d
OFFSET
%d`, orderBy, orderDir, n, page*n)
rows, err := d.Query(q, StatePublished)
if err != nil {
return nil, err
}
defer rows.Close()
ret := make([]*GalleryItem, 0, n)
for rows.Next() {
var item = new(GalleryItem)
err = rows.Scan(&item.Id, &item.Parent, &item.Document, &item.Title, &item.Description, &item.Screenshot, &item.Author, &item.License, &item.Views, &item.ModificationDate)
if err != nil {
return nil, err
}
ret = append(ret, item)
}
return ret, nil
}
func (d *Db) GalleryView(parent int, id int, iphash string) {
tx, err := d.Begin()
if err != nil {
log.Printf("Failed to create view update transaction: %v", err)
return
}
defer func() {
if tx != nil {
tx.Rollback()
}
}()
if _, err := tx.Exec("INSERT INTO views (id, ip) VALUES (?, ?)", parent, iphash); err != nil {
log.Printf("Failed to create view: %v", err)
return
}
if _, err := tx.Exec("UPDATE gallery SET views = views + 1 WHERE parent = ? AND id = ?", parent, id); err != nil {
log.Printf("Failed to update item views: %v", err)
return
}
if err := tx.Commit(); err != nil {
log.Printf("Failed to commit view update: %v", err)
return
}
tx = nil
}
func (d *Db) Open() {
rand.Seed(time.Now().UTC().UnixNano())
os.MkdirAll(dataRoot, 0755)
if d.DB != nil {
d.Close()
}
var err error
d.DB, err = sql.Open("sqlite3", path.Join(dataRoot, "gallery.db"))
if err != nil {
panic(err)
}
d.Migrate()
}