mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Fix: add optimism lock when updating file size
This commit is contained in:
2
assets
2
assets
Submodule assets updated: 39b5171449...e0da8f4885
@@ -268,6 +268,7 @@ func (file *File) UpdatePicInfo(value string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateSize 更新文件的大小信息
|
// UpdateSize 更新文件的大小信息
|
||||||
|
// TODO: 全局锁
|
||||||
func (file *File) UpdateSize(value uint64) error {
|
func (file *File) UpdateSize(value uint64) error {
|
||||||
tx := DB.Begin()
|
tx := DB.Begin()
|
||||||
var sizeDelta uint64
|
var sizeDelta uint64
|
||||||
@@ -281,7 +282,10 @@ func (file *File) UpdateSize(value uint64) error {
|
|||||||
sizeDelta = file.Size - value
|
sizeDelta = file.Size - value
|
||||||
}
|
}
|
||||||
|
|
||||||
if res := tx.Model(&file).Set("gorm:association_autoupdate", false).Update("size", value); res.Error != nil {
|
if res := tx.Model(&file).
|
||||||
|
Where("size = ?", file.Size).
|
||||||
|
Set("gorm:association_autoupdate", false).
|
||||||
|
Update("size", value); res.Error != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return res.Error
|
return res.Error
|
||||||
}
|
}
|
||||||
@@ -291,6 +295,7 @@ func (file *File) UpdateSize(value uint64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file.Size = value
|
||||||
return tx.Commit().Error
|
return tx.Commit().Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user