CreateFile godoc @Summary 创建文件 @Description 创建文件接口。请求体可选携带 origin_type / origin_source / origin_ref_id,用于显式标记“我的录音”来源;不传则保持默认来源逻辑。 @Tags 文件管理 @Accept json @Produce json @Security BearerAuth @Param request body FileRequst true "文件信息" @Success 200 {object} model.CommonResponse{data=model.File} @Router
(c *gin.Context)
| 335 | // @Success 200 {object} model.CommonResponse{data=model.File} |
| 336 | // @Router /api/files [post] |
| 337 | func CreateFile(c *gin.Context) { |
| 338 | eid := config.GetEID(c) |
| 339 | userID := config.GetUserId(c) |
| 340 | |
| 341 | // 获取上级文件对象 |
| 342 | |
| 343 | // 同时解析为请求体与模型体:请求体包含 permissions,模型体用于保存 |
| 344 | var req FileRequst |
| 345 | if err := c.ShouldBindJSON(&req); err != nil { |
| 346 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err)) |
| 347 | return |
| 348 | } |
| 349 | |
| 350 | if req.LibraryID == 0 { |
| 351 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("知识库ID不能为空"))) |
| 352 | return |
| 353 | } |
| 354 | |
| 355 | library, err := model.GetLibraryByID(eid, req.LibraryID) |
| 356 | if err != nil { |
| 357 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("知识库不存在"))) |
| 358 | return |
| 359 | } |
| 360 | isPersonalLibrary := library.IsPersonalLibrary() |
| 361 | |
| 362 | if !isPersonalLibrary { |
| 363 | // 检查功能是否可用 |
| 364 | params := map[string]interface{}{ |
| 365 | "from": "document", |
| 366 | "op": "add", |
| 367 | } |
| 368 | _, err := knowledgeBaseFeatureAvailable(c, "knowledge_base", params) |
| 369 | if err != nil { |
| 370 | c.JSON(http.StatusForbidden, model.FeatureNotAvailableError.ToResponse(err)) |
| 371 | return |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | file := model.File{ |
| 376 | Eid: eid, |
| 377 | Path: req.Path, |
| 378 | Type: req.Type, |
| 379 | LibraryID: req.LibraryID, |
| 380 | UserID: userID, // 设置文件创建人 |
| 381 | OriginType: model.FileOriginTypeManualCreate, |
| 382 | OriginRefID: 0, |
| 383 | } |
| 384 | |
| 385 | switch strings.TrimSpace(req.OriginType) { |
| 386 | case model.FileOriginTypeRecordingAudio: |
| 387 | file.SetRecordingAudioOrigin(req.OriginRefID) |
| 388 | case model.FileOriginTypeRecordingFolder: |
| 389 | file.SetRecordingFolderOrigin(req.OriginRefID) |
| 390 | case model.FileOriginTypeRecordingImported: |
| 391 | file.SetRecordingImportedOrigin(req.OriginRefID) |
| 392 | } |
| 393 | if originSource := strings.TrimSpace(req.OriginSource); originSource != "" && strings.TrimSpace(req.OriginType) != "" { |
| 394 | file.OriginSource = originSource |
nothing calls this directly
no test coverage detected