mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 17:41:57 +08:00
Feat: use transactions to manipulate user's used storage
This commit is contained in:
@@ -39,12 +39,23 @@ func init() {
|
||||
}
|
||||
|
||||
// Create 创建文件记录
|
||||
func (file *File) Create() (uint, error) {
|
||||
if err := DB.Create(file).Error; err != nil {
|
||||
func (file *File) Create() error {
|
||||
tx := DB.Begin()
|
||||
|
||||
if err := tx.Create(file).Error; err != nil {
|
||||
util.Log().Warning("无法插入文件记录, %s", err)
|
||||
return 0, err
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
return file.ID, nil
|
||||
|
||||
user := &User{}
|
||||
user.ID = file.UserID
|
||||
if err := user.ChangeStorage(tx, "+", file.Size); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit().Error
|
||||
}
|
||||
|
||||
// AfterFind 找到文件后的钩子
|
||||
|
||||
Reference in New Issue
Block a user