fix:修复上传

This commit is contained in:
Hiven 2023-08-10 14:49:46 +08:00
parent 4fbc17d707
commit db5b576799
2 changed files with 32 additions and 26 deletions

View File

@ -53,30 +53,35 @@ func (m *FsResourceModel) RowSelectBuilder(selectData []string) *gorm.DB {
// 事务
func (m *FsResourceModel) Trans(ctx context.Context, fn func(ctx context.Context, connGorm *gorm.DB) error) error {
tx := m.db.Table(m.name).WithContext(ctx).Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
if err := tx.Error; err != nil {
tx.Rollback()
return m.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
err := fn(ctx, tx)
return err
}
})
if err := fn(ctx, tx); err != nil {
tx.Rollback()
return err
}
// tx := m.db.Table(m.name).WithContext(ctx)
// defer func() {
// if r := recover(); r != nil {
// tx.Rollback()
// }
// }()
err := tx.Commit().Error
if err != nil {
tx.Rollback()
return err
}
// if err := tx.Error; err != nil {
// tx.Rollback()
// return err
// }
return err
// if err := fn(ctx, tx); err != nil {
// tx.Rollback()
// return err
// }
// err := tx.Commit().Error
// if err != nil {
// tx.Rollback()
// return err
// }
// return err
}
func (m *FsResourceModel) TableName() string {

View File

@ -66,11 +66,9 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er
var resourceId string = req.FileHash
var uploadBaseRes = UploadBaseRes{}
resourceModel := gmodel.NewFsResourceModel(upload.MysqlConn)
err := resourceModel.Trans(upload.Ctx, func(ctx context.Context, connGorm *gorm.DB) error {
resourceModelTS := gmodel.NewFsResourceModel(connGorm)
resourceInfo, err := resourceModelTS.FindOneById(ctx, resourceId)
err := upload.MysqlConn.Transaction(func(tx *gorm.DB) error {
resourceModelTS := gmodel.NewFsResourceModel(tx)
resourceInfo, err := resourceModelTS.FindOneById(upload.Ctx, resourceId)
if err == nil && resourceInfo.ResourceId != "" {
uploadBaseRes.Status = 1
uploadBaseRes.ResourceId = resourceId
@ -110,7 +108,7 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er
uploadBaseRes.ResourceUrl = url
var version string = "0.0.1"
var nowTime = time.Now()
_, err = resourceModelTS.Create(ctx, &gmodel.FsResource{
_, err = resourceModelTS.Create(upload.Ctx, &gmodel.FsResource{
ResourceId: resourceId,
UserId: &req.UserId,
GuestId: &req.GuestId,
@ -128,8 +126,11 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er
}
}
}
// 返回 nil 提交事务
return nil
})
if err != nil {
return nil, err
}