feat(files): enable space limit

This commit is contained in:
hexxa 2021-08-16 22:18:07 +08:00 committed by Hexxa
parent 921a7c1ef9
commit 41654e36d0
5 changed files with 181 additions and 64 deletions

View file

@ -129,17 +129,29 @@ func (h *FileHandlers) Create(c *gin.Context) {
return
}
role := c.MustGet(q.RoleParam).(string)
userID := c.MustGet(q.UserIDParam).(string)
userName := c.MustGet(q.UserParam).(string)
if !h.canAccess(userName, role, "create", req.Path) {
c.JSON(q.ErrResp(c, 403, q.ErrAccessDenied))
return
}
userID := c.MustGet(q.UserIDParam).(string)
userIDInt, err := strconv.ParseUint(userID, 10, 64)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
tmpFilePath := q.UploadPath(userName, req.Path)
locker := h.NewAutoLocker(c, lockName(tmpFilePath))
locker.Exec(func() {
err := h.deps.FS().Create(tmpFilePath)
err = h.deps.Users().SetUsed(userIDInt, true, req.FileSize)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
err = h.deps.FS().Create(tmpFilePath)
if err != nil {
if os.IsExist(err) {
c.JSON(q.ErrResp(c, 304, err))
@ -177,13 +189,35 @@ func (h *FileHandlers) Delete(c *gin.Context) {
return
}
err := h.deps.FS().Remove(filePath)
userID := c.MustGet(q.UserIDParam).(string)
userIDInt, err := strconv.ParseUint(userID, 10, 64)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(q.Resp(200))
locker := h.NewAutoLocker(c, lockName(filePath))
locker.Exec(func() {
info, err := h.deps.FS().Stat(filePath)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
err = h.deps.Users().SetUsed(userIDInt, false, info.Size())
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
err = h.deps.FS().Remove(filePath)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(q.Resp(200))
})
}
type MetadataResp struct {
@ -648,13 +682,30 @@ func (h *FileHandlers) DelUploading(c *gin.Context) {
c.JSON(q.ErrResp(c, 400, errors.New("invalid file path")))
return
}
userID := c.MustGet(q.UserIDParam).(string)
var err error
userID := c.MustGet(q.UserIDParam).(string)
userIDInt, err := strconv.ParseUint(userID, 10, 64)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
userName := c.MustGet(q.UserParam).(string)
tmpFilePath := q.UploadPath(userName, filePath)
locker := h.NewAutoLocker(c, lockName(tmpFilePath))
locker.Exec(func() {
_, size, _, err := h.uploadMgr.GetInfo(userID, tmpFilePath)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
err = h.deps.Users().SetUsed(userIDInt, false, size)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
err = h.deps.FS().Remove(tmpFilePath)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
@ -666,6 +717,7 @@ func (h *FileHandlers) DelUploading(c *gin.Context) {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(q.Resp(200))
})
}

View file

@ -364,7 +364,7 @@ func (h *MultiUsersSvc) AddUser(c *gin.Context) {
Pwd: string(pwdHash),
Role: req.Role,
Quota: &userstore.Quota{
SpaceLimit: h.cfg.IntOr("Users.SpaceLimit", 1024),
SpaceLimit: int64(h.cfg.IntOr("Users.SpaceLimit", 100*1024*1024)), // TODO: support int64
UploadSpeedLimit: h.cfg.IntOr("Users.UploadSpeedLimit", 100*1024),
DownloadSpeedLimit: h.cfg.IntOr("Users.DownloadSpeedLimit", 100*1024),
},
@ -556,9 +556,10 @@ func (h *MultiUsersSvc) isValidRole(role string) error {
}
type SelfResp struct {
ID string `json:"id"`
Name string `json:"name"`
Role string `json:"role"`
ID string `json:"id"`
Name string `json:"name"`
Role string `json:"role"`
UsedSpace int64 `json:"usedSpace,string"`
}
func (h *MultiUsersSvc) Self(c *gin.Context) {
@ -568,9 +569,16 @@ func (h *MultiUsersSvc) Self(c *gin.Context) {
return
}
user, err := h.deps.Users().GetUserByName(claims[q.UserParam])
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(200, &SelfResp{
ID: claims[q.UserIDParam],
Name: claims[q.UserParam],
Role: claims[q.RoleParam],
ID: claims[q.UserIDParam],
Name: claims[q.UserParam],
Role: claims[q.RoleParam],
UsedSpace: user.UsedSpace,
})
}