feat(files): enable space limit
This commit is contained in:
parent
921a7c1ef9
commit
41654e36d0
5 changed files with 181 additions and 64 deletions
|
@ -26,17 +26,18 @@ const (
|
|||
)
|
||||
|
||||
type Quota struct {
|
||||
SpaceLimit int `json:"spaceLimit"`
|
||||
UploadSpeedLimit int `json:"uploadSpeedLimit"`
|
||||
DownloadSpeedLimit int `json:"downloadSpeedLimit"`
|
||||
SpaceLimit int64 `json:"spaceLimit,space"`
|
||||
UploadSpeedLimit int `json:"uploadSpeedLimit"`
|
||||
DownloadSpeedLimit int `json:"downloadSpeedLimit"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID uint64 `json:"id,string"`
|
||||
Name string `json:"name"`
|
||||
Pwd string `json:"pwd"`
|
||||
Role string `json:"role"`
|
||||
Quota *Quota `json:"quota"`
|
||||
ID uint64 `json:"id,string"`
|
||||
Name string `json:"name"`
|
||||
Pwd string `json:"pwd"`
|
||||
Role string `json:"role"`
|
||||
UsedSpace int64 `json:"usedSpace,string"`
|
||||
Quota *Quota `json:"quota"`
|
||||
}
|
||||
|
||||
type IUserStore interface {
|
||||
|
@ -47,6 +48,7 @@ type IUserStore interface {
|
|||
GetUser(id uint64) (*User, error)
|
||||
GetUserByName(name string) (*User, error)
|
||||
SetInfo(id uint64, user *User) error
|
||||
SetUsed(id uint64, incr bool, capacity int64) error
|
||||
SetPwd(id uint64, pwd string) error
|
||||
ListUsers() ([]*User, error)
|
||||
AddRole(role string) error
|
||||
|
@ -215,31 +217,6 @@ func (us *KVUserStore) GetUserByName(name string) (*User, error) {
|
|||
|
||||
}
|
||||
|
||||
// func (us *KVUserStore) SetName(id uint64, name string) error {
|
||||
// us.mtx.Lock()
|
||||
// defer us.mtx.Unlock()
|
||||
|
||||
// _, ok := us.store.GetStringIn(IDsNs, name)
|
||||
// if ok {
|
||||
// return fmt.Errorf("user name (%s) exists", name)
|
||||
// }
|
||||
|
||||
// userID := fmt.Sprint(id)
|
||||
// _, ok = us.store.GetStringIn(UsersNs, userID)
|
||||
// if !ok {
|
||||
// return fmt.Errorf("Name (%d) does not exist", id)
|
||||
// }
|
||||
// if name == "" {
|
||||
// return fmt.Errorf("Name can not be empty")
|
||||
// }
|
||||
|
||||
// err := us.store.SetStringIn(IDsNs, name, userID)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return us.store.SetStringIn(UsersNs, userID, name)
|
||||
// }
|
||||
|
||||
func (us *KVUserStore) SetPwd(id uint64, pwd string) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
@ -265,6 +242,44 @@ func (us *KVUserStore) SetPwd(id uint64, pwd string) error {
|
|||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) SetUsed(id uint64, incr bool, capacity int64) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("user (%d) does not exist", id)
|
||||
}
|
||||
gotUser := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if gotUser.ID != id {
|
||||
return fmt.Errorf("user id key(%d) info(%d) does match", id, gotUser.ID)
|
||||
}
|
||||
|
||||
if incr && gotUser.UsedSpace+capacity > int64(gotUser.Quota.SpaceLimit) {
|
||||
return fmt.Errorf(
|
||||
"reached space limit (%d): file size(%d), used(%d)",
|
||||
gotUser.Quota.SpaceLimit,
|
||||
capacity,
|
||||
gotUser.UsedSpace,
|
||||
)
|
||||
}
|
||||
|
||||
if incr {
|
||||
gotUser.UsedSpace = gotUser.UsedSpace + capacity
|
||||
} else {
|
||||
gotUser.UsedSpace = gotUser.UsedSpace - capacity
|
||||
}
|
||||
infoBytes, err := json.Marshal(gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) SetInfo(id uint64, user *User) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
@ -289,6 +304,9 @@ func (us *KVUserStore) SetInfo(id uint64, user *User) error {
|
|||
if user.Quota != nil {
|
||||
gotUser.Quota = user.Quota
|
||||
}
|
||||
if user.UsedSpace > 0 {
|
||||
gotUser.UsedSpace = user.UsedSpace
|
||||
}
|
||||
|
||||
infoBytes, err := json.Marshal(gotUser)
|
||||
if err != nil {
|
||||
|
@ -297,19 +315,6 @@ func (us *KVUserStore) SetInfo(id uint64, user *User) error {
|
|||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||
}
|
||||
|
||||
// func (us *KVUserStore) SetRole(id uint64, role string) error {
|
||||
// us.mtx.Lock()
|
||||
// defer us.mtx.Unlock()
|
||||
|
||||
// userID := fmt.Sprint(id)
|
||||
// _, ok := us.store.GetStringIn(RolesNs, userID)
|
||||
// if !ok {
|
||||
// return fmt.Errorf("Role (%d) does not exist", id)
|
||||
// }
|
||||
|
||||
// return us.store.SetStringIn(RolesNs, userID, role)
|
||||
// }
|
||||
|
||||
func (us *KVUserStore) ListUsers() ([]*User, error) {
|
||||
us.mtx.RLock()
|
||||
defer us.mtx.RUnlock()
|
||||
|
|
|
@ -3,6 +3,7 @@ package userstore
|
|||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
|
||||
|
@ -38,8 +39,8 @@ func TestUserStores(t *testing.T) {
|
|||
id, name1 := uint64(1), "test_user1"
|
||||
pwd1, pwd2 := "666", "888"
|
||||
role1, role2 := UserRole, AdminRole
|
||||
spaceLimit1, upLimit1, downLimit1 := 3, 5, 7
|
||||
spaceLimit2, upLimit2, downLimit2 := 11, 13, 17
|
||||
spaceLimit1, upLimit1, downLimit1 := 17, 5, 7
|
||||
spaceLimit2, upLimit2, downLimit2 := 19, 13, 17
|
||||
|
||||
err = store.AddUser(&User{
|
||||
ID: id,
|
||||
|
@ -110,6 +111,22 @@ func TestUserStores(t *testing.T) {
|
|||
},
|
||||
})
|
||||
|
||||
usedIncr, usedDecr := int64(spaceLimit2), int64(7)
|
||||
err = store.SetUsed(id, true, usedIncr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = store.SetUsed(id, false, usedDecr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = store.SetUsed(id, true, int64(spaceLimit2)-(usedIncr-usedDecr)+1)
|
||||
if err == nil || !strings.Contains(err.Error(), "reached space limit") {
|
||||
t.Fatal("should reject big file")
|
||||
} else {
|
||||
err = nil
|
||||
}
|
||||
|
||||
user, err = store.GetUser(id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -129,6 +146,9 @@ func TestUserStores(t *testing.T) {
|
|||
if user.Quota.DownloadSpeedLimit != downLimit2 {
|
||||
t.Fatalf("down limit not matched %d %d", downLimit2, user.Quota.DownloadSpeedLimit)
|
||||
}
|
||||
if user.UsedSpace != usedIncr-usedDecr {
|
||||
t.Fatalf("used space not matched %d %d", user.UsedSpace, usedIncr-usedDecr)
|
||||
}
|
||||
|
||||
user, err = store.GetUserByName(name1)
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue