feat(files): enable upload limiter

This commit is contained in:
hexxa 2021-08-07 21:06:43 +08:00 committed by Hexxa
parent e01f5f8351
commit fd5da3db37
5 changed files with 118 additions and 0 deletions

View file

@ -10,6 +10,7 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
@ -289,6 +290,21 @@ func (h *FileHandlers) UploadChunk(c *gin.Context) {
return
}
userIDInt, err := strconv.ParseUint(userID, 10, 64)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
ok, err := h.deps.Limiter().CanUpload(userIDInt, len([]byte(req.Content)))
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
} else if !ok {
c.JSON(q.ErrResp(c, 503, errors.New("retry later")))
return
}
tmpFilePath := q.UploadPath(userID, req.Path)
locker := h.NewAutoLocker(c, lockName(tmpFilePath))
locker.Exec(func() {