-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
566 lines (459 loc) · 15.4 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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package main
import (
"encoding/base64"
"fmt"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase/forms"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/models/schema"
"github.com/pocketbase/pocketbase/plugins/ghupdate"
"github.com/pocketbase/pocketbase/plugins/jsvm"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
"github.com/pocketbase/pocketbase/tools/filesystem"
"github.com/pocketbase/pocketbase/tools/types"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
)
func main() {
app := pocketbase.New()
// ---------------------------------------------------------------
// Optional plugin flags:
// ---------------------------------------------------------------
var hooksDir string
app.RootCmd.PersistentFlags().StringVar(
&hooksDir,
"hooksDir",
"",
"the directory with the JS app hooks",
)
var hooksWatch bool
app.RootCmd.PersistentFlags().BoolVar(
&hooksWatch,
"hooksWatch",
true,
"auto restart the app on pb_hooks file change",
)
var hooksPool int
app.RootCmd.PersistentFlags().IntVar(
&hooksPool,
"hooksPool",
25,
"the total prewarm goja.Runtime instances for the JS app hooks execution",
)
var migrationsDir string
app.RootCmd.PersistentFlags().StringVar(
&migrationsDir,
"migrationsDir",
"",
"the directory with the user defined migrations",
)
var automigrate bool
app.RootCmd.PersistentFlags().BoolVar(
&automigrate,
"automigrate",
true,
"enable/disable auto migrations",
)
var publicDir string
app.RootCmd.PersistentFlags().StringVar(
&publicDir,
"publicDir",
defaultPublicDir(),
"the directory to serve static files",
)
var indexFallback bool
app.RootCmd.PersistentFlags().BoolVar(
&indexFallback,
"indexFallback",
true,
"fallback the request to index.html on missing static path (eg. when pretty urls are used with SPA)",
)
var queryTimeout int
app.RootCmd.PersistentFlags().IntVar(
&queryTimeout,
"queryTimeout",
30,
"the default SELECT queries timeout in seconds",
)
app.RootCmd.ParseFlags(os.Args[1:])
// ---------------------------------------------------------------
// Plugins and hooks:
// ---------------------------------------------------------------
// load jsvm (hooks and migrations)
jsvm.MustRegister(app, jsvm.Config{
MigrationsDir: migrationsDir,
HooksDir: hooksDir,
HooksWatch: hooksWatch,
HooksPoolSize: hooksPool,
})
// migrate command (with js templates)
migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{
TemplateLang: migratecmd.TemplateLangJS,
Automigrate: automigrate,
Dir: migrationsDir,
})
// GitHub selfupdate
ghupdate.MustRegister(app, app.RootCmd, ghupdate.Config{})
app.OnAfterBootstrap().PreAdd(func(e *core.BootstrapEvent) error {
app.Dao().ModelQueryTimeout = time.Duration(queryTimeout) * time.Second
return nil
})
uploadAndCleanup := func(uploadId string) error {
upload, err := app.Dao().FindRecordById("uploads", uploadId)
if err != nil {
return err
}
if upload.GetInt("size") == 0 || upload.GetInt("size") != upload.GetInt("current_offset") {
return fmt.Errorf("upload not finished")
}
// check if local file exists
_, err = os.Stat(filepath.Join(app.DataDir(), "tus_uploads", upload.Id+".part"))
if err != nil && os.IsNotExist(err) {
return nil // nothing we can do
} else if err != nil {
return err
}
form := forms.NewRecordUpsert(app, upload)
file, err := filesystem.NewFileFromPath(filepath.Join(app.DataDir(), "tus_uploads", upload.Id+".part"))
if err != nil {
return err
}
file.Name = upload.GetString("filename")
err = form.AddFiles("file", file)
if err != nil {
return err
}
if err = form.Submit(); err != nil {
return err
}
err = os.Remove(filepath.Join(app.DataDir(), "tus_uploads", upload.Id+".part"))
if err != nil {
return err
}
app.Logger().Debug("uploaded and cleaned up", "uploadId", upload.Id)
return nil
}
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
var uploadsCollectionId string
existingUploadsCollection, err := app.Dao().FindCollectionByNameOrId("uploads")
if err != nil {
collection := &models.Collection{}
form := forms.NewCollectionUpsert(app, collection)
form.Name = "uploads"
form.Type = models.CollectionTypeBase
form.ListRule = types.Pointer("user.id = @request.auth.id")
form.ViewRule = types.Pointer("user.id = @request.auth.id")
form.CreateRule = nil
form.UpdateRule = nil
form.DeleteRule = nil
form.Schema.AddField(&schema.SchemaField{
Name: "current_offset",
Type: schema.FieldTypeNumber,
})
form.Schema.AddField(&schema.SchemaField{
Name: "size",
Type: schema.FieldTypeNumber,
Required: true,
})
form.Schema.AddField(&schema.SchemaField{
Name: "filename",
Type: schema.FieldTypeText,
Required: true,
})
form.Schema.AddField(&schema.SchemaField{
Name: "mime_type",
Type: schema.FieldTypeText,
Required: true,
})
form.Schema.AddField(&schema.SchemaField{
Name: "user",
Type: schema.FieldTypeRelation,
Required: true,
Options: &schema.RelationOptions{
MinSelect: nil,
MaxSelect: types.Pointer(1),
CollectionId: "_pb_users_auth_",
CascadeDelete: true,
},
})
form.Schema.AddField(&schema.SchemaField{
Name: "file",
Type: schema.FieldTypeFile,
Required: false,
Options: &schema.FileOptions{
MaxSelect: 1,
MaxSize: 4 * 1024 * 1024 * 1024, // 4GB
Protected: true,
},
})
if err := form.Submit(); err != nil {
return err
}
existingUploadsCollection, err = app.Dao().FindCollectionByNameOrId("uploads")
if err != nil {
panic(fmt.Errorf("uploads collection not found even after creating it"))
}
}
uploadsCollectionId = existingUploadsCollection.Id
app.Logger().Debug("", "uploadsCollectionId", uploadsCollectionId)
_, err = app.Dao().FindCollectionByNameOrId("accessRefs")
if err != nil {
collection := &models.Collection{}
form := forms.NewCollectionUpsert(app, collection)
form.Name = "accessRefs"
form.Type = models.CollectionTypeBase
form.ListRule = types.Pointer("user.id = @request.auth.id")
form.ViewRule = types.Pointer("user.id = @request.auth.id")
form.CreateRule = types.Pointer("user.id = @request.auth.id && upload.user.id = @request.auth.id")
form.UpdateRule = types.Pointer("user.id = @request.auth.id")
form.DeleteRule = types.Pointer("user.id = @request.auth.id")
form.Schema.AddField(&schema.SchemaField{
Name: "upload",
Type: schema.FieldTypeRelation,
Required: true,
Options: &schema.RelationOptions{
MinSelect: nil,
MaxSelect: types.Pointer(1),
CollectionId: uploadsCollectionId,
CascadeDelete: true,
},
})
form.Schema.AddField(&schema.SchemaField{
Name: "user",
Type: schema.FieldTypeRelation,
Required: true,
Options: &schema.RelationOptions{
MinSelect: nil,
MaxSelect: types.Pointer(1),
CollectionId: "_pb_users_auth_",
CascadeDelete: true,
},
})
if err := form.Submit(); err != nil {
return err
}
}
// check stray finished uploads
uploads, err := app.Dao().FindRecordsByFilter(
"uploads",
"size != 0 && size = current_offset",
"-updated",
0,
0,
)
if err != nil {
return err
}
for _, upload := range uploads {
err = uploadAndCleanup(upload.Id)
if err != nil {
app.Logger().Error("upload failed", "uploadId", upload.Id)
}
}
e.Router.POST("/uploads", func(c echo.Context) error {
c.Response().Header().Set("Tus-Resumable", "1.0.0")
headers := apis.RequestInfo(c).Headers
authRecord := apis.RequestInfo(c).AuthRecord
if v, ok := headers["tus_resumable"]; !ok || v != "1.0.0" {
c.Response().Header().Set("Tus-Version", "1.0.0")
return apis.NewApiError(http.StatusPreconditionFailed, "", nil)
}
var (
err error
metadata = map[string]string{}
size = 0
filename = ""
mimeType = ""
)
// headers are lowercased with underscore (_) instead of dashes (-)
if size, err = strconv.Atoi(c.Request().Header.Get("Upload-Length")); err != nil {
return apis.NewBadRequestError("", nil)
}
if h := c.Request().Header.Get("Upload-Metadata"); h != "" {
for _, str := range strings.Split(h, ",") {
items := strings.Split(strings.TrimSpace(str), " ")
if len(items) != 2 {
continue
}
var valBytes []byte
valBytes, err = base64.StdEncoding.DecodeString(items[1])
if err != nil {
continue
}
metadata[items[0]] = string(valBytes)
}
}
var ok bool
if filename, ok = metadata["filename"]; !ok {
c.Response().Header().Set("Tus-Version", "1.0.0")
return apis.NewBadRequestError("", nil)
}
if mimeType, ok = metadata["filetype"]; !ok {
c.Response().Header().Set("Tus-Version", "1.0.0")
return apis.NewBadRequestError("", nil)
}
collection, err := app.Dao().FindCollectionByNameOrId("uploads")
if err != nil {
return apis.NewApiError(http.StatusInternalServerError, "", nil)
}
record := models.NewRecord(collection)
form := forms.NewRecordUpsert(app, record)
err = form.LoadData(map[string]any{
"current_offset": 0,
"size": size,
"filename": filename,
"mime_type": mimeType,
"user": authRecord.Id,
})
if err != nil {
return apis.NewApiError(http.StatusInternalServerError, "failed to load form data", err)
}
if err = form.Submit(); err != nil {
return apis.NewApiError(http.StatusInternalServerError, "failed to create new upload record", err)
}
// TODO: use host header for app url, otherwise it's bound for only one url with cors restrictions
location := fmt.Sprintf("%s/uploads/%s", app.Settings().Meta.AppUrl, record.Id)
c.Response().Header().Set("Location", location)
return c.NoContent(http.StatusCreated)
}, apis.RequireRecordAuth())
e.Router.HEAD("/uploads/:upload_id", func(c echo.Context) error {
c.Response().Header().Set("Tus-Resumable", "1.0.0")
headers := apis.RequestInfo(c).Headers
if v, ok := headers["tus_resumable"]; !ok || v != "1.0.0" {
c.Response().Header().Set("Tus-Version", "1.0.0")
return apis.NewApiError(http.StatusPreconditionFailed, "", nil)
}
var uploadId string
if uploadId = c.PathParam("upload_id"); uploadId == "" {
return apis.NewBadRequestError("", nil)
}
upload, err := app.Dao().FindRecordById("uploads", uploadId)
if err != nil {
return apis.NewNotFoundError("", nil)
}
c.Response().Header().Set("Cache-Control", "no-store")
c.Response().Header().Set("Upload-Offset", upload.GetString("current_offset"))
c.Response().Header().Set("Upload-Length", upload.GetString("size"))
return c.NoContent(http.StatusOK)
}, apis.RequireRecordAuth())
e.Router.PATCH("/uploads/:upload_id", func(c echo.Context) error {
c.Response().Header().Set("Tus-Resumable", "1.0.0")
headers := apis.RequestInfo(c).Headers
if v, ok := headers["tus_resumable"]; !ok || v != "1.0.0" {
c.Response().Header().Set("Tus-Version", "1.0.0")
return apis.NewApiError(http.StatusPreconditionFailed, "", nil)
}
if v, ok := headers["content_type"]; !ok || v != "application/offset+octet-stream" {
return apis.NewApiError(http.StatusUnsupportedMediaType, "", nil)
}
var (
err error
contentLength int
uploadOffset int
uploadId string
)
if contentLength, err = strconv.Atoi(c.Request().Header.Get("Content-Length")); err != nil {
return apis.NewBadRequestError("no Content-Length", nil)
}
if uploadOffset, err = strconv.Atoi(c.Request().Header.Get("Upload-Offset")); err != nil {
return apis.NewBadRequestError("no Upload-Offset", nil)
}
if uploadId = c.PathParam("upload_id"); uploadId == "" {
return apis.NewBadRequestError("no upload_id", nil)
}
upload, err := app.Dao().FindRecordById("uploads", uploadId)
if err != nil {
return apis.NewNotFoundError("", nil)
}
if uploadOffset != upload.GetInt("current_offset") || uploadOffset >= upload.GetInt("size") {
return apis.NewApiError(http.StatusConflict, "", nil)
}
err = os.MkdirAll(filepath.Join(app.DataDir(), "tus_uploads"), 0750)
if err != nil {
return apis.NewApiError(http.StatusInternalServerError, "", nil)
}
f, err := os.OpenFile(filepath.Join(app.DataDir(), "tus_uploads", upload.Id+".part"), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return apis.NewApiError(http.StatusInternalServerError, "", nil)
}
defer f.Close()
_, err = f.Seek(int64(uploadOffset), 0)
if err != nil {
return apis.NewApiError(http.StatusInternalServerError, "", nil)
}
body, err := io.ReadAll(c.Request().Body)
if err != nil {
return apis.NewApiError(http.StatusInternalServerError, "", nil)
}
defer c.Request().Body.Close()
written, err := f.Write(body)
if err != nil {
return apis.NewApiError(http.StatusInternalServerError, "", nil)
}
if written != contentLength {
return apis.NewApiError(http.StatusInternalServerError, "written != contentLength", nil)
}
upload.Set("current_offset", strconv.Itoa(uploadOffset+contentLength))
if err = app.Dao().SaveRecord(upload); err != nil {
return apis.NewApiError(http.StatusInternalServerError, "", nil)
}
if upload.GetInt("size") != 0 && upload.GetInt("size") == upload.GetInt("current_offset") {
go uploadAndCleanup(upload.Id)
}
c.Response().Header().Set("Upload-Offset", upload.GetString("current_offset"))
c.Response().Header().Set("Upload-Length", upload.GetString("size"))
return c.NoContent(http.StatusOK)
}, apis.RequireRecordAuth())
e.Router.GET("/accref/:access_ref_id", func(c echo.Context) error {
var accessRefId string
if accessRefId = c.PathParam("access_ref_id"); accessRefId == "" {
return apis.NewBadRequestError("no access_ref_id", nil)
}
accessRef, err := app.Dao().FindRecordById("accessRefs", accessRefId)
if err != nil {
return apis.NewNotFoundError("", nil)
}
if errs := app.Dao().ExpandRecord(accessRef, []string{"upload"}, nil); len(errs) > 0 {
return apis.NewApiError(http.StatusInternalServerError, fmt.Sprintf("failed to expand: %v", errs), nil)
}
upload := accessRef.ExpandedOne("upload")
if upload == nil {
return apis.NewNotFoundError("", nil)
}
key := upload.BaseFilesPath() + "/" + upload.GetString("file")
fsys, _ := app.NewFilesystem()
defer fsys.Close()
blob, _ := fsys.GetFile(key)
defer blob.Close()
c.Response().Header().Set("Content-Type", upload.GetString("mime_type"))
http.ServeContent(c.Response(), c.Request(), upload.GetString("filename"), upload.Updated.Time(), blob)
//c.Response().Header().Set("Accept-Ranges", "bytes")
//c.Response().Header().Set("Content-Disposition", "attachment; filename=\""+upload.GetString("filename")+"\"")
//
//return c.Stream(200, upload.GetString("mime_type"), blob)
return nil
})
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS(publicDir), indexFallback))
return nil
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}
// the default pb_public dir location is relative to the executable
func defaultPublicDir() string {
if strings.HasPrefix(os.Args[0], os.TempDir()) {
// most likely ran with go run
return "./pb_public"
}
return filepath.Join(os.Args[0], "../pb_public")
}