refactor(doc): 重构文档上传逻辑

- 将文档上传分为两个阶段:插入数据库记录和调用 Python API
- 修改了 Doc 模型的 InsertDocumentData 方法,返回插入记录的 ID 和状态
- 在控制器中处理了插入数据库失败的情况
- 保留了 TODO 注释,以便后续继续开发
This commit is contained in:
Havoc412 2024-11-15 22:53:19 +08:00
parent e636bbb9c2
commit 07391268a9
2 changed files with 16 additions and 12 deletions

View File

@ -14,15 +14,19 @@ type Docs struct {
}
func (d *Docs) Upload(context *gin.Context) {
// TODO 1. 读取源文件,调用 py API 分块上传。
path := context.GetString(consts.ValidatorPrefix + "path")
filePath := filepath.Join(variable.ConfigYml.GetString("FileUploadSetting.UploadFileSavePath"), variable.ConfigYml.GetString("FileUploadSetting.DocsRootPath"), path)
_ = filePath
// STAGE 2.
if ok := model.CreateDocFactory("").InsertDocumentData(context); ok {
response.Success(context, consts.CurdStatusOkMsg, "")
} else {
// STAGE 1. 插入 MySQL 记录。
var doc_id int64
ok := false
if doc_id, ok = model.CreateDocFactory("").InsertDocumentData(context); !ok {
response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg, "上传文档错误")
}
// STAGE 2. 调用 python API
path := context.GetString(consts.ValidatorPrefix + "path")
filePath := filepath.Join(variable.ConfigYml.GetString("FileUploadSetting.UploadFileSavePath"), variable.ConfigYml.GetString("FileUploadSetting.DocsRootPath"), path)
// TODO
_ = filePath
_ = doc_id
response.Success(context, consts.CurdStatusOkMsg, "")
}

View File

@ -22,16 +22,16 @@ type Doc struct {
func (d *Doc) TableName() string { return "docs" }
func (d *Doc) InsertDocumentData(c *gin.Context) bool {
func (d *Doc) InsertDocumentData(c *gin.Context) (int64, bool) {
var tmp Doc
if err := data_bind.ShouldBindFormDataToModel(c, &tmp); err == nil {
if res := d.Create(&tmp); res.Error == nil {
return true
return tmp.Id, true
} else {
variable.ZapLog.Error("Doc 数据新增出错", zap.Error(res.Error))
}
} else {
variable.ZapLog.Error("Doc 数据绑定出错", zap.Error(err))
}
return false
return 0, false
}