♻️ Use update api instead of overhaul in background tasks

This commit is contained in:
LittleSheep 2025-01-01 11:43:54 +08:00
parent 2bd8dc17d1
commit 5e73d9acd4
3 changed files with 12 additions and 18 deletions

View File

@ -220,16 +220,14 @@ func AnalyzeAttachment(file models.Attachment) error {
tx := database.C.Begin()
file.IsAnalyzed = true
if err := tx.Model(&file).Update("is_analyzed", true).Error; err != nil {
tx.Rollback()
return fmt.Errorf("unable to update file record: %v", err)
}
linked, err := TryLinkAttachment(tx, file, file.HashCode)
if linked && err != nil {
return fmt.Errorf("unable to link file record: %v", err)
} else if !linked {
if err := tx.Save(&file).Error; err != nil {
tx.Rollback()
return fmt.Errorf("unable to save file record: %v", err)
}
}
tx.Commit()

View File

@ -166,19 +166,15 @@ func TryLinkAttachment(tx *gorm.DB, og models.Attachment, hash string) (bool, er
}
}
prev.RefCount++
og.RefID = &prev.ID
og.Uuid = prev.Uuid
og.Destination = prev.Destination
if og.AccountID == prev.AccountID {
og.IsSelfRef = true
}
if err := tx.Save(&og).Error; err != nil {
if err := tx.Model(&og).Updates(&models.Attachment{
RefID: &prev.ID,
Uuid: prev.Uuid,
Destination: prev.Destination,
IsSelfRef: og.AccountID == prev.AccountID,
}).Error; err != nil {
tx.Rollback()
return true, err
} else if err = tx.Save(&prev).Error; err != nil {
} else if err = tx.Model(&prev).Update("ref_count", prev.RefCount+1).Error; err != nil {
tx.Rollback()
return true, err
}

View File

@ -48,7 +48,7 @@ func ReUploadFile(meta models.Attachment, dst int, doNotUpdate ...bool) error {
cleanupDst := func() {
if len(doNotUpdate) == 0 || !doNotUpdate[0] {
database.C.Save(&meta)
database.C.Model(&meta).Update("destination", dst)
}
if prevDst == models.AttachmentDstTemporary {
return