test(files): add move folder test and clean code

This commit is contained in:
hexxa 2022-03-05 11:03:51 +08:00 committed by Hexxa
parent 17b4544487
commit 652adba4ad
6 changed files with 74 additions and 80 deletions

View file

@ -160,7 +160,7 @@ func (bs *BoltStore) AddUploadInfos(userID uint64, tmpPath, filePath string, inf
}
if userInfo.UsedSpace+info.Size > int64(userInfo.Quota.SpaceLimit) {
return errors.New("space limit is reached")
return db.ErrQuota
}
// update used space
@ -370,7 +370,6 @@ func (bs *BoltStore) MoveInfos(userID uint64, oldPath, newPath string, isDir boo
return err
}
fmt.Println("\n\n\n4", err)
// add new info
return bs.setFileInfo(tx, userID, newPath, fileInfo)
})

View file

@ -21,6 +21,7 @@ var (
ErrBucketNotFound = errors.New("bucket not found")
ErrKeyNotFound = errors.New("key not found")
ErrCreateExisting = errors.New("create upload info which already exists")
ErrQuota = errors.New("quota limit reached")
)
type FileInfo struct {

View file

@ -155,7 +155,11 @@ func (h *FileHandlers) Create(c *gin.Context) {
Size: req.FileSize,
})
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
if errors.Is(err, db.ErrQuota) {
c.JSON(q.ErrResp(c, 403, err))
} else {
c.JSON(q.ErrResp(c, 500, err))
}
return
}
err = h.deps.BoltStore().MoveUploadingInfos(userIDInt, tmpFilePath, fsFilePath)
@ -208,33 +212,15 @@ func (h *FileHandlers) Create(c *gin.Context) {
Size: req.FileSize,
})
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
if errors.Is(err, db.ErrQuota) {
c.JSON(q.ErrResp(c, 403, err))
} else {
c.JSON(q.ErrResp(c, 500, err))
}
}
locker := h.NewAutoLocker(c, lockName(tmpFilePath))
locker.Exec(func() {
// ok, err := h.deps.Users().CanIncrUsed(userIDInt, req.FileSize)
// if err != nil {
// c.JSON(q.ErrResp(c, 500, err))
// return
// } else if !ok {
// c.JSON(q.ErrResp(c, 429, userstore.ErrReachedLimit))
// return
// }
// err = h.deps.FileInfos().AddUploadInfo(userID, req.Path, tmpFilePath, req.FileSize)
// if err != nil {
// c.JSON(q.ErrResp(c, 500, err))
// return
// }
// 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) {
@ -516,41 +502,6 @@ func (h *FileHandlers) UploadChunk(c *gin.Context) {
return
}
// err = h.deps.FileInfos().DelUploadInfo(userID, tmpFilePath)
// if err != nil {
// c.JSON(q.ErrResp(c, 500, err))
// 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, &db.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{
FilePath: fsFilePath,
})
@ -926,12 +877,6 @@ func (h *FileHandlers) DelUploading(c *gin.Context) {
tmpFilePath := q.UploadPath(userName, filePath)
locker := h.NewAutoLocker(c, lockName(tmpFilePath))
locker.Exec(func() {
// _, size, _, err := h.deps.FileInfos().GetUploadInfo(userID, tmpFilePath)
// if err != nil {
// c.JSON(q.ErrResp(c, 500, err))
// return
// }
_, err = h.deps.FS().Stat(tmpFilePath)
if err != nil {
if os.IsNotExist(err) {
@ -947,18 +892,6 @@ func (h *FileHandlers) DelUploading(c *gin.Context) {
return
}
}
// err = h.deps.FileInfos().DelUploadInfo(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.BoltStore().DelUploadingInfos(userIDInt, tmpFilePath)

View file

@ -778,6 +778,67 @@ func TestFileHandlers(t *testing.T) {
}
})
t.Run("test folder moving: Mkdir-Create-UploadChunk-AddSharing-Move-IsSharing-List", func(t *testing.T) {
srcDir := "qs/files/folder/move/src"
dstDir := "qs/files/folder/move/dst"
res, _, errs := cl.Mkdir(srcDir)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
files := map[string]string{
"f1.md": "111",
"f2.md": "22222",
}
for fileName, content := range files {
oldPath := filepath.Join(srcDir, fileName)
assertUploadOK(t, oldPath, content, addr, token)
}
res, _, errs = cl.AddSharing(srcDir)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
res, _, errs = cl.Move(srcDir, dstDir)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
res, _, errs = cl.IsSharing(dstDir)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 404 { // should not be in sharing
t.Fatal(res.StatusCode)
}
err = fs.Sync()
if err != nil {
t.Fatal(err)
}
_, lResp, errs := cl.List(dstDir)
if len(errs) > 0 {
t.Fatal(errs)
}
for _, metadata := range lResp.Metadatas {
content, ok := files[metadata.Name]
if !ok {
t.Fatalf("%s not found", metadata.Name)
} else if int64(len(content)) != metadata.Size {
t.Fatalf("size not match %d %d \n", len(content), metadata.Size)
}
}
})
resp, _, errs = usersCl.Logout(token)
if len(errs) > 0 {
t.Fatal(errs)

View file

@ -125,7 +125,7 @@ func TestSpaceLimit(t *testing.T) {
res, _, errs := cl.Create(filePath, 1)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 500 {
} else if res.StatusCode != 403 {
t.Fatalf("(space limit): this request should be rejected: %d", res.StatusCode)
}

Binary file not shown.