fix(files): used space is incorrect
This commit is contained in:
parent
d85a46d039
commit
ffd8131dfd
3 changed files with 37 additions and 16 deletions
|
@ -8,7 +8,7 @@ const defaultChunkLen = 1024 * 1024 * 1;
|
||||||
const speedDownRatio = 0.5;
|
const speedDownRatio = 0.5;
|
||||||
const speedUpRatio = 1.1;
|
const speedUpRatio = 1.1;
|
||||||
const speedLimit = 1024 * 1024 * 10; // 10MB
|
const speedLimit = 1024 * 1024 * 10; // 10MB
|
||||||
const createRetryLimit = 3;
|
const createRetryLimit = 512;
|
||||||
const uploadRetryLimit = 1024;
|
const uploadRetryLimit = 1024;
|
||||||
const backoffMax = 2000;
|
const backoffMax = 2000;
|
||||||
|
|
||||||
|
|
|
@ -145,26 +145,19 @@ func (h *FileHandlers) Create(c *gin.Context) {
|
||||||
tmpFilePath := q.UploadPath(userName, req.Path)
|
tmpFilePath := q.UploadPath(userName, req.Path)
|
||||||
locker := h.NewAutoLocker(c, lockName(tmpFilePath))
|
locker := h.NewAutoLocker(c, lockName(tmpFilePath))
|
||||||
locker.Exec(func() {
|
locker.Exec(func() {
|
||||||
err = h.deps.Users().SetUsed(userIDInt, true, req.FileSize)
|
ok, err := h.deps.Users().CanIncrUsed(userIDInt, req.FileSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if userstore.IsReachedLimitErr(err) {
|
c.JSON(q.ErrResp(c, 500, err))
|
||||||
c.JSON(q.ErrResp(c, 429, err))
|
return
|
||||||
} else {
|
} else if !ok {
|
||||||
c.JSON(q.ErrResp(c, 500, err))
|
c.JSON(q.ErrResp(c, 429, err))
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.deps.FS().Create(tmpFilePath)
|
err = h.deps.FS().Create(tmpFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsExist(err) {
|
if os.IsExist(err) {
|
||||||
// avoid adding file size more than once
|
c.JSON(q.ErrResp(c, 304, fmt.Errorf("file(%s) exists", tmpFilePath)))
|
||||||
err = h.deps.Users().SetUsed(userIDInt, false, req.FileSize)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(q.ErrResp(c, 500, err))
|
|
||||||
} else {
|
|
||||||
c.JSON(q.ErrResp(c, 304, fmt.Errorf("file(%s) exists", tmpFilePath)))
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
c.JSON(q.ErrResp(c, 500, err))
|
c.JSON(q.ErrResp(c, 500, err))
|
||||||
}
|
}
|
||||||
|
@ -182,6 +175,12 @@ func (h *FileHandlers) Create(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = h.deps.Users().SetUsed(userIDInt, true, req.FileSize)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(q.ErrResp(c, 500, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(q.Resp(200))
|
c.JSON(q.Resp(200))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -800,13 +799,13 @@ func (h *FileHandlers) DelUploading(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.deps.Users().SetUsed(userIDInt, false, size)
|
err = h.uploadMgr.DelInfo(userID, tmpFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(q.ErrResp(c, 500, err))
|
c.JSON(q.ErrResp(c, 500, err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.uploadMgr.DelInfo(userID, tmpFilePath)
|
err = h.deps.Users().SetUsed(userIDInt, false, size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(q.ErrResp(c, 500, err))
|
c.JSON(q.ErrResp(c, 500, err))
|
||||||
return
|
return
|
||||||
|
|
|
@ -66,6 +66,7 @@ type IUserStore interface {
|
||||||
GetUser(id uint64) (*User, error)
|
GetUser(id uint64) (*User, error)
|
||||||
GetUserByName(name string) (*User, error)
|
GetUserByName(name string) (*User, error)
|
||||||
SetInfo(id uint64, user *User) error
|
SetInfo(id uint64, user *User) error
|
||||||
|
CanIncrUsed(id uint64, capacity int64) (bool, error)
|
||||||
SetUsed(id uint64, incr bool, capacity int64) error
|
SetUsed(id uint64, incr bool, capacity int64) error
|
||||||
SetPwd(id uint64, pwd string) error
|
SetPwd(id uint64, pwd string) error
|
||||||
ListUsers() ([]*User, error)
|
ListUsers() ([]*User, error)
|
||||||
|
@ -275,6 +276,27 @@ func (us *KVUserStore) SetPwd(id uint64, pwd string) error {
|
||||||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (us *KVUserStore) CanIncrUsed(id uint64, capacity int64) (bool, error) {
|
||||||
|
us.mtx.Lock()
|
||||||
|
defer us.mtx.Unlock()
|
||||||
|
|
||||||
|
userID := fmt.Sprint(id)
|
||||||
|
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||||
|
if !ok {
|
||||||
|
return false, fmt.Errorf("user (%d) does not exist", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
gotUser := &User{}
|
||||||
|
err := json.Unmarshal([]byte(infoStr), gotUser)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
} else if gotUser.ID != id {
|
||||||
|
return false, fmt.Errorf("user id key(%d) info(%d) does match", id, gotUser.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return gotUser.UsedSpace+capacity <= int64(gotUser.Quota.SpaceLimit), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (us *KVUserStore) SetUsed(id uint64, incr bool, capacity int64) error {
|
func (us *KVUserStore) SetUsed(id uint64, incr bool, capacity int64) error {
|
||||||
us.mtx.Lock()
|
us.mtx.Lock()
|
||||||
defer us.mtx.Unlock()
|
defer us.mtx.Unlock()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue