MCPcopy Create free account
hub / github.com/53AI/53AIHub / CreateFile

Function CreateFile

api/controller/file.go:337–462  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

335// @Success 200 {object} model.CommonResponse{data=model.File}
336// @Router /api/files [post]
337func 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

Callers

nothing calls this directly

Calls 11

CheckParentPermissionMethod · 0.95
SaveMethod · 0.95
GetEIDMethod · 0.80
ToResponseMethod · 0.80
IsPersonalLibraryMethod · 0.80
NewMethod · 0.45

Tested by

no test coverage detected