Skip to content

Commit

Permalink
fix(fs): cannot delete file while user is deleted (fix #1586)
Browse files Browse the repository at this point in the history
  • Loading branch information
HFO4 committed Feb 7, 2023
1 parent a93ea2c commit 2a1e82a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
10 changes: 6 additions & 4 deletions models/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func DeleteFiles(files []*File, uid uint) error {
user.ID = uid
var size uint64
for _, file := range files {
if file.UserID != uid {
if uid > 0 && file.UserID != uid {
tx.Rollback()
return errors.New("user id not consistent")
}
Expand All @@ -253,9 +253,11 @@ func DeleteFiles(files []*File, uid uint) error {
size += file.Size
}

if err := user.ChangeStorage(tx, "-", size); err != nil {
tx.Rollback()
return err
if uid > 0 {
if err := user.ChangeStorage(tx, "-", size); err != nil {
tx.Rollback()
return err
}
}

return tx.Commit().Error
Expand Down
21 changes: 14 additions & 7 deletions service/admin/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,22 @@ func (service *FileBatchService) Delete(c *gin.Context) serializer.Response {
// 异步执行删除
go func(files map[uint][]model.File) {
for uid, file := range files {
var (
fs *filesystem.FileSystem
err error
)
user, err := model.GetUserByID(uid)
if err != nil {
continue
}

fs, err := filesystem.NewFileSystem(&user)
if err != nil {
fs.Recycle()
continue
fs, err = filesystem.NewAnonymousFileSystem()
if err != nil {
continue
}
} else {
fs, err = filesystem.NewFileSystem(&user)
if err != nil {
fs.Recycle()
continue
}
}

// 汇总文件ID
Expand Down

0 comments on commit 2a1e82a

Please sign in to comment.