diff --git a/upd/api/common.go b/upd/api/common.go index 029907e..ba1f3b1 100644 --- a/upd/api/common.go +++ b/upd/api/common.go @@ -37,15 +37,6 @@ type Meta struct { Expire string `json:"expire" form:"expire"` } -// stores 1 upload object, gets into db -type Upload struct { - Id string `json:"id"` - Expire string `json:"expire"` - File string `json:"file"` // final filename (visible to the downloader) - Members []string `json:"members"` // contains multiple files, so File is an archive - Uploaded time.Time `json:"uploaded"` -} - // vaious helbers func Log(format string, values ...any) { fmt.Printf("[DEBUG] "+format+"\n", values...) diff --git a/upd/api/db.go b/upd/api/db.go index f25c498..917e030 100644 --- a/upd/api/db.go +++ b/upd/api/db.go @@ -20,14 +20,37 @@ package api import ( "encoding/json" "fmt" - bolt "go.etcd.io/bbolt" ) const Bucket string = "uploads" -func DbInsert(db *bolt.DB, id string, entry *Upload) { - err := db.Update(func(tx *bolt.Tx) error { +// wrapper for bolt db +type Db struct { + bolt *bolt.DB +} + +// stores 1 upload object, gets into db +type Upload struct { + Id string `json:"id"` + Expire string `json:"expire"` + File string `json:"file"` // final filename (visible to the downloader) + Members []string `json:"members"` // contains multiple files, so File is an archive + Uploaded Timestamp `json:"uploaded"` +} + +func NewDb(file string) (*Db, error) { + b, err := bolt.Open(file, 0600, nil) + db := Db{bolt: b} + return &db, err +} + +func (db *Db) Close() { + db.bolt.Close() +} + +func (db *Db) Insert(id string, entry *Upload) error { + err := db.bolt.Update(func(tx *bolt.Tx) error { bucket, err := tx.CreateBucketIfNotExists([]byte(Bucket)) if err != nil { return fmt.Errorf("create bucket: %s", err) @@ -51,12 +74,14 @@ func DbInsert(db *bolt.DB, id string, entry *Upload) { if err != nil { Log("DB error: %s", err.Error()) } + + return err } -func DbLookupId(db *bolt.DB, id string) (Upload, error) { +func (db *Db) Lookup(id string) (Upload, error) { var upload Upload - err := db.View(func(tx *bolt.Tx) error { + err := db.bolt.View(func(tx *bolt.Tx) error { bucket := tx.Bucket([]byte(Bucket)) j := bucket.Get([]byte(id)) @@ -79,8 +104,8 @@ func DbLookupId(db *bolt.DB, id string) (Upload, error) { return upload, nil } -func DbDeleteId(db *bolt.DB, id string) error { - err := db.Update(func(tx *bolt.Tx) error { +func (db *Db) Delete(id string) error { + err := db.bolt.Update(func(tx *bolt.Tx) error { bucket := tx.Bucket([]byte(Bucket)) j := bucket.Get([]byte(id)) diff --git a/upd/api/handlers.go b/upd/api/handlers.go index 47af5a1..5595a7f 100644 --- a/upd/api/handlers.go +++ b/upd/api/handlers.go @@ -21,7 +21,6 @@ import ( "github.com/gofiber/fiber/v2" "github.com/google/uuid" "github.com/tlinden/up/upd/cfg" - bolt "go.etcd.io/bbolt" "os" "path/filepath" @@ -29,7 +28,7 @@ import ( "time" ) -func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *bolt.DB) (string, error) { +func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) (string, error) { // supports upload of multiple files with: // // curl -X POST localhost:8080/putfile \ @@ -57,7 +56,8 @@ func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *bolt.DB) (string, error) { return "", err } - entry := &Upload{Id: id, Uploaded: time.Now()} + //entry := &Upload{Id: id, Uploaded: time.Now()} + entry := &Upload{Id: id, Uploaded: Timestamp{Time: time.Now()}} // init upload obj @@ -125,18 +125,18 @@ func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *bolt.DB) (string, error) { Log("Expire set to: %s", entry.Expire) // we do this in the background to not thwart the server - go DbInsert(db, id, entry) + go db.Insert(id, entry) return returnUrl, nil } -func FileGet(c *fiber.Ctx, cfg *cfg.Config, db *bolt.DB) error { +func FileGet(c *fiber.Ctx, cfg *cfg.Config, db *Db) error { // deliver a file and delete it after a (configurable?) delay id := c.Params("id") file := c.Params("file") - upload, err := DbLookupId(db, id) + upload, err := db.Lookup(id) if err != nil { // non existent db entry with that id, or other db error, see logs return fiber.NewError(404, "No download with that id could be found!") @@ -151,7 +151,7 @@ func FileGet(c *fiber.Ctx, cfg *cfg.Config, db *bolt.DB) error { if _, err := os.Stat(filename); err != nil { // db entry is there, but file isn't (anymore?) - go DbDeleteId(db, id) + go db.Delete(id) } // finally put the file to the client @@ -161,7 +161,7 @@ func FileGet(c *fiber.Ctx, cfg *cfg.Config, db *bolt.DB) error { // check if we need to delete the file now if upload.Expire == "asap" { cleanup(filepath.Join(cfg.StorageDir, id)) - go DbDeleteId(db, id) + go db.Delete(id) } }() @@ -172,7 +172,7 @@ type Id struct { Id string `json:"name" xml:"name" form:"name"` } -func FileDelete(c *fiber.Ctx, cfg *cfg.Config, db *bolt.DB) error { +func FileDelete(c *fiber.Ctx, cfg *cfg.Config, db *Db) error { // delete file, id dir and db entry id := c.Params("id") @@ -193,7 +193,7 @@ func FileDelete(c *fiber.Ctx, cfg *cfg.Config, db *bolt.DB) error { cleanup(filepath.Join(cfg.StorageDir, id)) - err := DbDeleteId(db, id) + err := db.Delete(id) if err != nil { // non existent db entry with that id, or other db error, see logs return fiber.NewError(404, "No upload with that id could be found!") diff --git a/upd/api/server.go b/upd/api/server.go index d8b247b..af6aad1 100644 --- a/upd/api/server.go +++ b/upd/api/server.go @@ -22,7 +22,6 @@ import ( "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/requestid" "github.com/tlinden/up/upd/cfg" - bolt "go.etcd.io/bbolt" ) func Runserver(cfg *cfg.Config, args []string) error { @@ -42,7 +41,7 @@ func Runserver(cfg *cfg.Config, args []string) error { Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}​\n", })) - db, err := bolt.Open(cfg.DbFile, 0600, nil) + db, err := NewDb(cfg.DbFile) if err != nil { return err } diff --git a/upd/api/timestamp.go b/upd/api/timestamp.go new file mode 100644 index 0000000..fdec611 --- /dev/null +++ b/upd/api/timestamp.go @@ -0,0 +1,63 @@ +/* +Copyright © 2023 Thomas von Dein + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package api + +import ( + "strconv" + "time" +) + +// https://gist.github.com/rhcarvalho/9338c3ff8850897c68bc74797c5dc25b + +// Timestamp is like time.Time, but knows how to unmarshal from JSON +// Unix timestamp numbers or RFC3339 strings, and marshal back into +// the same JSON representation. +type Timestamp struct { + time.Time + rfc3339 bool +} + +func (t Timestamp) MarshalJSON() ([]byte, error) { + if t.rfc3339 { + return t.Time.MarshalJSON() + } + return t.formatUnix() +} + +func (t *Timestamp) UnmarshalJSON(data []byte) error { + err := t.Time.UnmarshalJSON(data) + if err != nil { + return t.parseUnix(data) + } + t.rfc3339 = true + return nil +} + +func (t Timestamp) formatUnix() ([]byte, error) { + sec := float64(t.Time.UnixNano()) * float64(time.Nanosecond) / float64(time.Second) + return strconv.AppendFloat(nil, sec, 'f', -1, 64), nil +} + +func (t *Timestamp) parseUnix(data []byte) error { + f, err := strconv.ParseFloat(string(data), 64) + if err != nil { + return err + } + t.Time = time.Unix(0, int64(f*float64(time.Second/time.Nanosecond))) + return nil +}