fix(files): add work around for calculating the used space

This commit is contained in:
hexxa 2022-03-04 10:47:00 +08:00 committed by Hexxa
parent e5728aa96c
commit 693886f988
3 changed files with 84 additions and 3 deletions

View file

@ -9,6 +9,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/ihexxa/quickshare/src/db"
"github.com/ihexxa/quickshare/src/kvstore" "github.com/ihexxa/quickshare/src/kvstore"
) )
@ -39,6 +40,7 @@ type FileInfo struct {
Shared bool `json:"shared"` Shared bool `json:"shared"`
ShareID string `json:"shareID"` // for short url ShareID string `json:"shareID"` // for short url
Sha1 string `json:"sha1"` Sha1 string `json:"sha1"`
Size int64 `json:"size"`
} }
type IFileInfoStore interface { type IFileInfoStore interface {
@ -117,7 +119,44 @@ func migrate(fi *FileInfoStore) error {
return err return err
} }
case "v1": case "v1":
// no op // add size to file info
infoStrs, err := fi.store.ListStringsIn(InfoNs)
if err != nil {
return err
}
type FileInfoV1 struct {
IsDir bool `json:"isDir"`
Shared bool `json:"shared"`
Sha1 string `json:"sha1"`
ShareID string `json:"shareID"` // for short url
}
infoV1 := &FileInfoV1{}
for itemPath, infoStr := range infoStrs {
err = json.Unmarshal([]byte(infoStr), infoV1)
if err != nil {
return fmt.Errorf("list sharing error: %w", err)
}
newInfo := &FileInfo{
IsDir: infoV1.IsDir,
Shared: infoV1.Shared,
ShareID: infoV1.ShareID,
Sha1: infoV1.Sha1,
Size: 0, // need to run an async task to refresh this
}
if err = fi.SetInfo(itemPath, newInfo); err != nil {
return err
}
}
err = fi.store.SetStringIn(InitNs, SchemaVerKey, db.SchemaV2)
if err != nil {
return err
}
case db.SchemaV2:
// no need to migrate
default: default:
return fmt.Errorf("file info: unknown schema version (%s)", ver) return fmt.Errorf("file info: unknown schema version (%s)", ver)
} }

View file

@ -18,6 +18,7 @@ import (
"github.com/ihexxa/gocfg" "github.com/ihexxa/gocfg"
"github.com/ihexxa/multipart" "github.com/ihexxa/multipart"
"github.com/ihexxa/quickshare/src/db/fileinfostore"
"github.com/ihexxa/quickshare/src/db/userstore" "github.com/ihexxa/quickshare/src/db/userstore"
"github.com/ihexxa/quickshare/src/depidx" "github.com/ihexxa/quickshare/src/depidx"
q "github.com/ihexxa/quickshare/src/handlers" q "github.com/ihexxa/quickshare/src/handlers"
@ -195,6 +196,7 @@ 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() {
// TODO:
ok, err := h.deps.Users().CanIncrUsed(userIDInt, req.FileSize) ok, err := h.deps.Users().CanIncrUsed(userIDInt, req.FileSize)
if err != nil { if err != nil {
c.JSON(q.ErrResp(c, 500, err)) c.JSON(q.ErrResp(c, 500, err))
@ -257,7 +259,7 @@ func (h *FileHandlers) Delete(c *gin.Context) {
locker := h.NewAutoLocker(c, lockName(filePath)) locker := h.NewAutoLocker(c, lockName(filePath))
locker.Exec(func() { locker.Exec(func() {
info, err := h.deps.FS().Stat(filePath) info, err := h.deps.FileInfos().GetInfo(filePath)
if err != nil { if err != nil {
c.JSON(q.ErrResp(c, 500, err)) c.JSON(q.ErrResp(c, 500, err))
return return
@ -269,11 +271,18 @@ func (h *FileHandlers) Delete(c *gin.Context) {
return return
} }
err = h.deps.Users().SetUsed(userIDInt, false, info.Size()) err = h.deps.Users().SetUsed(userIDInt, false, info.Size)
if err != nil { if err != nil {
c.JSON(q.ErrResp(c, 500, err)) c.JSON(q.ErrResp(c, 500, err))
return return
} }
err = h.deps.FileInfos().DelInfo(filePath)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(q.Resp(200)) c.JSON(q.Resp(200))
}) })
} }
@ -471,6 +480,35 @@ func (h *FileHandlers) UploadChunk(c *gin.Context) {
return return
} }
fsInfo, err := h.deps.FS().Stat(fsFilePath)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
err = h.deps.FileInfos().SetInfo(fsFilePath, &fileinfostore.FileInfo{
IsDir: false,
Shared: false,
Size: fsInfo.Size(),
})
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
// TODO: check space quota?
if fsInfo.Size()-fileSize != 0 {
sizeDiff := fsInfo.Size() - fileSize
if sizeDiff < 0 {
sizeDiff = -sizeDiff
}
err = h.deps.Users().SetUsed(userIDInt, fsInfo.Size()-fileSize > 0, sizeDiff)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
}
msg, err := json.Marshal(Sha1Params{ msg, err := json.Marshal(Sha1Params{
FilePath: fsFilePath, FilePath: fsFilePath,
}) })

View file

@ -432,3 +432,7 @@ func (bp *BoltPvd) ListStringsByPrefixIn(prefix, ns string) (map[string]string,
return results, err return results, err
} }
func (bp *BoltPvd) StartUpdateTxBolt(op func(*bolt.Tx) error) error {
return bp.db.Update(op)
}