From ace398d87b4575d207e97ff195690819faf3eaca Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sun, 3 Apr 2022 20:39:50 +0800 Subject: [PATCH] Fix: file size is ready dirty when clean upload sessions After listing to be deleted files, before delete is committed to database, file size might be changed by ongoing upload, causing inconsistent user storage. --- models/file.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/models/file.go b/models/file.go index 004d157..171c94c 100644 --- a/models/file.go +++ b/models/file.go @@ -221,14 +221,17 @@ func DeleteFiles(files []*File, uid uint) error { for _, file := range files { if file.UserID != uid { tx.Rollback() - return errors.New("User id not consistent") + return errors.New("user id not consistent") } - result := tx.Unscoped().Delete(file) - if result.RowsAffected != 0 { - size += file.Size + result := tx.Unscoped().Where("size = ?", file.Size).Delete(file) + if result.RowsAffected == 0 { + tx.Rollback() + return errors.New("file size is dirty") } + size += file.Size + if result.Error != nil { tx.Rollback() return result.Error