mirror of
https://codeberg.org/scip/ephemerup.git
synced 2025-12-16 20:20:58 +01:00
move to codeberg (#8)
This commit is contained in:
@@ -22,8 +22,8 @@ import (
|
||||
"errors"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/keyauth/v2"
|
||||
"github.com/tlinden/ephemerup/cfg"
|
||||
"github.com/tlinden/ephemerup/common"
|
||||
"codeberg.org/scip/ephemerup/cfg"
|
||||
"codeberg.org/scip/ephemerup/common"
|
||||
)
|
||||
|
||||
// these vars can be savely global, since they don't change ever
|
||||
|
||||
@@ -20,8 +20,8 @@ import (
|
||||
"fmt"
|
||||
//"github.com/alecthomas/repr"
|
||||
"encoding/json"
|
||||
"github.com/tlinden/ephemerup/cfg"
|
||||
"github.com/tlinden/ephemerup/common"
|
||||
"codeberg.org/scip/ephemerup/cfg"
|
||||
"codeberg.org/scip/ephemerup/common"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
@@ -18,8 +18,8 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/tlinden/ephemerup/cfg"
|
||||
"github.com/tlinden/ephemerup/common"
|
||||
"codeberg.org/scip/ephemerup/cfg"
|
||||
"codeberg.org/scip/ephemerup/common"
|
||||
//"github.com/alecthomas/repr"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
"regexp"
|
||||
|
||||
@@ -18,12 +18,13 @@ package api
|
||||
|
||||
import (
|
||||
//"github.com/alecthomas/repr"
|
||||
"github.com/maxatome/go-testdeep/td"
|
||||
"github.com/tlinden/ephemerup/cfg"
|
||||
"github.com/tlinden/ephemerup/common"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"codeberg.org/scip/ephemerup/cfg"
|
||||
"codeberg.org/scip/ephemerup/common"
|
||||
"github.com/maxatome/go-testdeep/td"
|
||||
)
|
||||
|
||||
func finalize(db *Db) {
|
||||
@@ -51,11 +52,11 @@ func TestNew(t *testing.T) {
|
||||
db, err := NewDb(c)
|
||||
defer finalize(db)
|
||||
if err != nil && !tt.wantfail {
|
||||
t.Errorf("expected: &Db{}, got err: " + err.Error())
|
||||
t.Error("expected: &Db{}, got err: " + err.Error())
|
||||
}
|
||||
|
||||
if err == nil && tt.wantfail {
|
||||
t.Errorf("expected: fail, got &Db{}")
|
||||
t.Error("expected: fail, got &Db{}")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -94,8 +95,8 @@ var dbtests = []struct {
|
||||
}
|
||||
|
||||
/*
|
||||
We need to test the whole Db operation in one run, because it
|
||||
doesn't work well if using a global Db.
|
||||
We need to test the whole Db operation in one run, because it
|
||||
doesn't work well if using a global Db.
|
||||
*/
|
||||
func TestDboperation(t *testing.T) {
|
||||
for _, tt := range dbtests {
|
||||
@@ -106,14 +107,14 @@ func TestDboperation(t *testing.T) {
|
||||
defer finalize(db)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Could not open new DB: " + err.Error())
|
||||
t.Error("Could not open new DB: " + err.Error())
|
||||
}
|
||||
|
||||
if tt.upload.Id != "" {
|
||||
// set ts
|
||||
ts, err := time.Parse(timeformat, tt.ts)
|
||||
if err != nil {
|
||||
t.Errorf("Could not parse time: " + err.Error())
|
||||
t.Error("Could not parse time: " + err.Error())
|
||||
}
|
||||
|
||||
tt.upload.Created = common.Timestamp{Time: ts}
|
||||
@@ -121,18 +122,18 @@ func TestDboperation(t *testing.T) {
|
||||
// create new upload db object
|
||||
err = db.Insert(tt.id, tt.upload)
|
||||
if err != nil {
|
||||
t.Errorf("Could not insert new upload object: " + err.Error())
|
||||
t.Error("Could not insert new upload object: " + err.Error())
|
||||
}
|
||||
|
||||
// fetch it
|
||||
response, err := db.Get(tt.context, tt.id, common.TypeUpload)
|
||||
if err != nil {
|
||||
t.Errorf("Could not fetch upload object: " + err.Error())
|
||||
t.Error("Could not fetch upload object: " + err.Error())
|
||||
}
|
||||
|
||||
// is it there?
|
||||
if len(response.Uploads) != 1 {
|
||||
t.Errorf("db.Get() did not return an upload obj")
|
||||
t.Error("db.Get() did not return an upload obj")
|
||||
}
|
||||
|
||||
// compare times
|
||||
@@ -151,24 +152,24 @@ func TestDboperation(t *testing.T) {
|
||||
// fetch list
|
||||
response, err = db.List(tt.context, tt.filter, tt.query, common.TypeUpload)
|
||||
if err != nil {
|
||||
t.Errorf("Could not fetch uploads list: " + err.Error())
|
||||
t.Error("Could not fetch uploads list: " + err.Error())
|
||||
}
|
||||
|
||||
// is it there?
|
||||
if len(response.Uploads) != 1 {
|
||||
t.Errorf("db.List() did not return upload obj[s]")
|
||||
t.Error("db.List() did not return upload obj[s]")
|
||||
}
|
||||
|
||||
// delete
|
||||
err = db.Delete(tt.context, tt.id)
|
||||
if err != nil {
|
||||
t.Errorf("Could not delete upload obj: " + err.Error())
|
||||
t.Error("Could not delete upload obj: " + err.Error())
|
||||
}
|
||||
|
||||
// fetch again, shall return empty
|
||||
_, err = db.Get(tt.context, tt.id, common.TypeUpload)
|
||||
if err == nil {
|
||||
t.Errorf("Could fetch upload object again although we deleted it")
|
||||
t.Error("Could fetch upload object again although we deleted it")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,25 +177,25 @@ func TestDboperation(t *testing.T) {
|
||||
// set ts
|
||||
ts, err := time.Parse(timeformat, tt.ts)
|
||||
if err != nil {
|
||||
t.Errorf("Could not parse time: " + err.Error())
|
||||
t.Error("Could not parse time: " + err.Error())
|
||||
}
|
||||
tt.form.Created = common.Timestamp{Time: ts}
|
||||
|
||||
// create new form db object
|
||||
err = db.Insert(tt.id, tt.form)
|
||||
if err != nil {
|
||||
t.Errorf("Could not insert new form object: " + err.Error())
|
||||
t.Error("Could not insert new form object: " + err.Error())
|
||||
}
|
||||
|
||||
// fetch it
|
||||
response, err := db.Get(tt.context, tt.id, common.TypeForm)
|
||||
if err != nil {
|
||||
t.Errorf("Could not fetch form object: " + err.Error())
|
||||
t.Error("Could not fetch form object: " + err.Error())
|
||||
}
|
||||
|
||||
// is it there?
|
||||
if len(response.Forms) != 1 {
|
||||
t.Errorf("db.Get() did not return an form obj")
|
||||
t.Error("db.Get() did not return an form obj")
|
||||
}
|
||||
|
||||
// compare times
|
||||
@@ -213,24 +214,24 @@ func TestDboperation(t *testing.T) {
|
||||
// fetch list
|
||||
response, err = db.List(tt.context, tt.filter, tt.query, common.TypeForm)
|
||||
if err != nil {
|
||||
t.Errorf("Could not fetch forms list: " + err.Error())
|
||||
t.Error("Could not fetch forms list: " + err.Error())
|
||||
}
|
||||
|
||||
// is it there?
|
||||
if len(response.Forms) != 1 {
|
||||
t.Errorf("db.FormsList() did not return form obj[s]")
|
||||
t.Error("db.FormsList() did not return form obj[s]")
|
||||
}
|
||||
|
||||
// delete
|
||||
err = db.Delete(tt.context, tt.id)
|
||||
if err != nil {
|
||||
t.Errorf("Could not delete form obj: " + err.Error())
|
||||
t.Error("Could not delete form obj: " + err.Error())
|
||||
}
|
||||
|
||||
// fetch again, shall return empty
|
||||
_, err = db.Get(tt.context, tt.id, common.TypeForm)
|
||||
if err == nil {
|
||||
t.Errorf("Could fetch form object again although we deleted it")
|
||||
t.Error("Could fetch form object again although we deleted it")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -20,8 +20,8 @@ import (
|
||||
"archive/zip"
|
||||
"errors"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/tlinden/ephemerup/cfg"
|
||||
"github.com/tlinden/ephemerup/common"
|
||||
"codeberg.org/scip/ephemerup/cfg"
|
||||
"codeberg.org/scip/ephemerup/common"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
|
||||
@@ -20,8 +20,8 @@ import (
|
||||
//"github.com/alecthomas/repr"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/tlinden/ephemerup/cfg"
|
||||
"github.com/tlinden/ephemerup/common"
|
||||
"codeberg.org/scip/ephemerup/cfg"
|
||||
"codeberg.org/scip/ephemerup/common"
|
||||
|
||||
"bytes"
|
||||
"html/template"
|
||||
|
||||
@@ -18,7 +18,7 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/tlinden/ephemerup/cfg"
|
||||
"codeberg.org/scip/ephemerup/cfg"
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
|
||||
@@ -26,8 +26,8 @@ import (
|
||||
"github.com/gofiber/fiber/v2/middleware/requestid"
|
||||
"github.com/gofiber/fiber/v2/middleware/session"
|
||||
"github.com/gofiber/keyauth/v2"
|
||||
"github.com/tlinden/ephemerup/cfg"
|
||||
"github.com/tlinden/ephemerup/common"
|
||||
"codeberg.org/scip/ephemerup/cfg"
|
||||
"codeberg.org/scip/ephemerup/common"
|
||||
)
|
||||
|
||||
// sessions are context specific and can be global savely
|
||||
|
||||
@@ -11,8 +11,8 @@ package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/tlinden/ephemerup/cfg"
|
||||
"github.com/tlinden/ephemerup/common"
|
||||
"codeberg.org/scip/ephemerup/cfg"
|
||||
"codeberg.org/scip/ephemerup/common"
|
||||
)
|
||||
|
||||
func Status(c *fiber.Ctx, cfg *cfg.Config) error {
|
||||
|
||||
@@ -20,8 +20,8 @@ import (
|
||||
//"github.com/alecthomas/repr"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/tlinden/ephemerup/cfg"
|
||||
"github.com/tlinden/ephemerup/common"
|
||||
"codeberg.org/scip/ephemerup/cfg"
|
||||
"codeberg.org/scip/ephemerup/common"
|
||||
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
36
api/utils.go
36
api/utils.go
@@ -18,10 +18,11 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/tlinden/ephemerup/cfg"
|
||||
"github.com/tlinden/ephemerup/common"
|
||||
"time"
|
||||
|
||||
"codeberg.org/scip/ephemerup/cfg"
|
||||
"codeberg.org/scip/ephemerup/common"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
const ApiVersion string = "/v1"
|
||||
@@ -47,17 +48,17 @@ func Ts() string {
|
||||
}
|
||||
|
||||
/*
|
||||
Retrieve the API Context name from the session, assuming is has
|
||||
been successfully authenticated. However, if there are no api
|
||||
contexts defined, we'll use 'default' (set in
|
||||
auth.validateAPIKey()).
|
||||
Retrieve the API Context name from the session, assuming is has
|
||||
been successfully authenticated. However, if there are no api
|
||||
contexts defined, we'll use 'default' (set in
|
||||
auth.validateAPIKey()).
|
||||
|
||||
If there's no apicontext in the session, assume unauth user, return ""
|
||||
If there's no apicontext in the session, assume unauth user, return ""
|
||||
*/
|
||||
func SessionGetApicontext(c *fiber.Ctx) (string, error) {
|
||||
sess, err := Sessionstore.Get(c)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Unable to initialize session store from context: " + err.Error())
|
||||
return "", fmt.Errorf("Unable to initialize session store from context: %s", err.Error())
|
||||
}
|
||||
|
||||
apicontext := sess.Get("apicontext")
|
||||
@@ -69,14 +70,14 @@ func SessionGetApicontext(c *fiber.Ctx) (string, error) {
|
||||
}
|
||||
|
||||
/*
|
||||
Retrieve the formid (aka onetime api key) from the session. It is
|
||||
configured if an upload request has been successfully authenticated
|
||||
using a onetime key.
|
||||
Retrieve the formid (aka onetime api key) from the session. It is
|
||||
configured if an upload request has been successfully authenticated
|
||||
using a onetime key.
|
||||
*/
|
||||
func SessionGetFormId(c *fiber.Ctx) (string, error) {
|
||||
sess, err := Sessionstore.Get(c)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Unable to initialize session store from context: " + err.Error())
|
||||
return "", fmt.Errorf("Unable to initialize session store from context: %s", err.Error())
|
||||
}
|
||||
|
||||
formid := sess.Get("formid")
|
||||
@@ -88,12 +89,13 @@ func SessionGetFormId(c *fiber.Ctx) (string, error) {
|
||||
}
|
||||
|
||||
/*
|
||||
Calculate if time is up based on start time.Time and
|
||||
duration. Returns true if time is expired. Start time comes from
|
||||
the database.
|
||||
Calculate if time is up based on start time.Time and
|
||||
duration. Returns true if time is expired. Start time comes from
|
||||
the database.
|
||||
|
||||
aka:
|
||||
if(now - start) >= duration { time is up}
|
||||
|
||||
if(now - start) >= duration { time is up}
|
||||
*/
|
||||
func IsExpired(conf *cfg.Config, start time.Time, duration string) bool {
|
||||
var expiretime int // seconds
|
||||
|
||||
Reference in New Issue
Block a user