fix:修复上传

This commit is contained in:
Hiven 2023-08-10 14:29:59 +08:00
parent a5a15664c9
commit 7f44f46336
2 changed files with 10 additions and 1 deletions

View File

@ -52,6 +52,7 @@ 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 { 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() tx := m.db.Table(m.name).WithContext(ctx).Begin()
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
@ -60,6 +61,7 @@ func (m *FsResourceModel) Trans(ctx context.Context, fn func(ctx context.Context
}() }()
if err := tx.Error; err != nil { if err := tx.Error; err != nil {
tx.Rollback()
return err return err
} }
@ -68,7 +70,13 @@ func (m *FsResourceModel) Trans(ctx context.Context, fn func(ctx context.Context
return err return err
} }
return tx.Commit().Error err := tx.Commit().Error
if err != nil {
tx.Rollback()
return err
}
return err
} }
func (m *FsResourceModel) TableName() string { func (m *FsResourceModel) TableName() string {

View File

@ -67,6 +67,7 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er
var uploadBaseRes = UploadBaseRes{} var uploadBaseRes = UploadBaseRes{}
resourceModel := gmodel.NewFsResourceModel(upload.MysqlConn) resourceModel := gmodel.NewFsResourceModel(upload.MysqlConn)
err := resourceModel.Trans(upload.Ctx, func(ctx context.Context, connGorm *gorm.DB) error { err := resourceModel.Trans(upload.Ctx, func(ctx context.Context, connGorm *gorm.DB) error {
resourceModelTS := gmodel.NewFsResourceModel(connGorm) resourceModelTS := gmodel.NewFsResourceModel(connGorm)
resourceInfo, err := resourceModelTS.FindOneById(ctx, resourceId) resourceInfo, err := resourceModelTS.FindOneById(ctx, resourceId)