fix(files): add boltdb store and refactor files handlers

This commit is contained in:
hexxa 2022-03-05 00:16:04 +08:00 committed by Hexxa
parent 044cdea1d4
commit 17b4544487
19 changed files with 751 additions and 402 deletions

View file

@ -23,6 +23,7 @@ import (
"golang.org/x/crypto/bcrypt"
"github.com/ihexxa/quickshare/src/cryptoutil/jwt"
"github.com/ihexxa/quickshare/src/db/boltstore"
"github.com/ihexxa/quickshare/src/db/fileinfostore"
"github.com/ihexxa/quickshare/src/db/sitestore"
"github.com/ihexxa/quickshare/src/db/userstore"
@ -141,6 +142,7 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
if err := filesystem.MkdirAll(dbPath); err != nil {
panic(fmt.Sprintf("fail to create path for db: %s", err))
}
kv := boltdbpvd.New(dbPath, 1024)
users, err := userstore.NewKVUserStore(kv)
if err != nil {
@ -152,7 +154,11 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
}
siteStore, err := sitestore.NewSiteStore(kv)
if err != nil {
panic(fmt.Sprintf("fail to new site config store: %s", err))
panic(fmt.Sprintf("fail to init site config store: %s", err))
}
boltDB, err := boltstore.NewBoltStore(kv.Bolt())
if err != nil {
panic(fmt.Sprintf("fail to init bolt store: %s", err))
}
err = siteStore.Init(&sitestore.SiteConfig{
@ -182,6 +188,7 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
deps.SetUsers(users)
deps.SetFileInfos(fileInfos)
deps.SetSiteStore(siteStore)
deps.SetBoltStore(boltDB)
deps.SetID(ider)
deps.SetLog(logger)
deps.SetLimiter(limiter)

View file

@ -11,7 +11,7 @@ import (
"time"
"github.com/ihexxa/quickshare/src/client"
"github.com/ihexxa/quickshare/src/db/fileinfostore"
"github.com/ihexxa/quickshare/src/db"
q "github.com/ihexxa/quickshare/src/handlers"
"github.com/ihexxa/quickshare/src/handlers/fileshdr"
)
@ -631,7 +631,7 @@ func TestFileHandlers(t *testing.T) {
t.Fatal(res.StatusCode)
}
gotInfos := map[string]*fileinfostore.UploadInfo{}
gotInfos := map[string]*db.UploadInfo{}
for _, info := range lResp.UploadInfos {
gotInfos[info.RealFilePath] = info
}

View file

@ -10,6 +10,7 @@ import (
"testing"
"github.com/ihexxa/quickshare/src/client"
"github.com/ihexxa/quickshare/src/db"
"github.com/ihexxa/quickshare/src/db/userstore"
q "github.com/ihexxa/quickshare/src/handlers"
"github.com/ihexxa/quickshare/src/handlers/settings"
@ -105,7 +106,7 @@ func TestPermissions(t *testing.T) {
testUsersAPIs := func(desc, user string, pwd string, requireAuth bool, expectedCodes map[string]int) {
newPwd := "12345"
newRole := userstore.AdminRole
newQuota := &userstore.Quota{
newQuota := &db.Quota{
SpaceLimit: int64(2046),
UploadSpeedLimit: int(8 * 1024 * 1024),
DownloadSpeedLimit: int(8 * 1024 * 1024),

View file

@ -125,8 +125,8 @@ func TestSpaceLimit(t *testing.T) {
res, _, errs := cl.Create(filePath, 1)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 429 {
t.Fatal("(space limit): this request should be rejected")
} else if res.StatusCode != 500 {
t.Fatalf("(space limit): this request should be rejected: %d", res.StatusCode)
}
for i := 0; i < spaceLimit/fileSize; i++ {

View file

@ -8,6 +8,7 @@ import (
"testing"
"github.com/ihexxa/quickshare/src/client"
"github.com/ihexxa/quickshare/src/db"
"github.com/ihexxa/quickshare/src/db/sitestore"
"github.com/ihexxa/quickshare/src/db/userstore"
q "github.com/ihexxa/quickshare/src/handlers"
@ -120,14 +121,14 @@ func TestUsersHandlers(t *testing.T) {
})
t.Run("test users APIs: Login-Self-SetPwd-Logout-Login", func(t *testing.T) {
users := []*userstore.User{
users := []*db.User{
{
ID: 0,
Name: adminName,
Pwd: adminPwd,
Role: userstore.AdminRole,
UsedSpace: 0,
Quota: &userstore.Quota{
Quota: &db.Quota{
UploadSpeedLimit: 50 * 1024 * 1024,
DownloadSpeedLimit: 50 * 1024 * 1024,
SpaceLimit: 1024 * 1024 * 1024,
@ -139,7 +140,7 @@ func TestUsersHandlers(t *testing.T) {
Pwd: "Quicksh@re",
Role: userstore.UserRole,
UsedSpace: 0,
Quota: &userstore.Quota{
Quota: &db.Quota{
UploadSpeedLimit: 409600,
DownloadSpeedLimit: 409600,
SpaceLimit: 1024,
@ -309,7 +310,7 @@ func TestUsersHandlers(t *testing.T) {
}
}
newRole, newQuota := userstore.AdminRole, &userstore.Quota{
newRole, newQuota := userstore.AdminRole, &db.Quota{
SpaceLimit: 3,
UploadSpeedLimit: 3,
DownloadSpeedLimit: 3,
@ -445,8 +446,8 @@ func TestUsersHandlers(t *testing.T) {
token := client.GetCookie(resp.Cookies(), su.TokenCookie)
prefers := []*userstore.Preferences{
&userstore.Preferences{
prefers := []*db.Preferences{
&db.Preferences{
Bg: &sitestore.BgConfig{
Url: "/bgurl",
Repeat: "no-repeat",
@ -456,7 +457,7 @@ func TestUsersHandlers(t *testing.T) {
CSSURL: "/cssurl",
LanPackURL: "/lanpack",
},
&userstore.Preferences{
&db.Preferences{
Bg: &sitestore.BgConfig{
Url: "/bgurl2",
Repeat: "no-repeat2",

Binary file not shown.