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

Function GetFile

api/controller/file.go:474–535  ·  view source on GitHub ↗

GetFile godoc @Summary 获取文件详情 @Description 获取文件详情接口 @Tags 文件管理 @Accept json @Produce json @Security BearerAuth @Param file_id path int true "文件ID" @Success 200 {object} model.CommonResponse{data=model.File} @Router /api/files/{file_id} [get]

(c *gin.Context)

Source from the content-addressed store, hash-verified

472// @Success 200 {object} model.CommonResponse{data=model.File}
473// @Router /api/files/{file_id} [get]
474func GetFile(c *gin.Context) {
475 eid := config.GetEID(c)
476 userID := config.GetUserId(c)
477
478 id := c.Param("file_id")
479 if id == "" {
480 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("文件ID不能为空")))
481 return
482 }
483
484 fileID, err := parseMCPStyleID(id)
485 if err != nil {
486 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("无效的文件ID")))
487 return
488 }
489
490 file, err := model.GetFileByID(eid, fileID)
491 if err != nil {
492 // 文件不存在时,清理 Elasticsearch 中的对应索引,防止出现"能查到但无法查看"的情况
493
494 go func() {
495 // 异步执行删除操作,避免阻塞主流程
496 elasticsearch.SyncFileToES(&model.File{ID: fileID}, "delete")
497 logger.SysLogf("清理不存在文件的ES索引: fileID=%d", fileID)
498 }()
499
500 c.JSON(http.StatusNotFound, model.NotFound.ToResponse(err))
501 return
502 }
503
504 library, err := model.GetLibraryByID(eid, file.LibraryID)
505 if err != nil {
506 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("知识库不存在")))
507 return
508 }
509 if !library.IsPersonalLibrary() {
510 params := map[string]interface{}{
511 "from": "document",
512 }
513 _, err := knowledgeBaseFeatureAvailable(c, "knowledge_base", params)
514 if err != nil {
515 c.JSON(http.StatusForbidden, model.FeatureNotAvailableError.ToResponse(err))
516 return
517 }
518 }
519
520 permisson, err := service.GetUserPermission(eid, model.RESOURCE_TYPE_FILE, fileID, userID)
521 if err != nil || permisson < model.PERMISSION_VIEW_ONLY {
522 c.JSON(http.StatusForbidden, model.AuthFailed.ToResponse(err))
523 return
524 }
525
526 _ = file.LoadUploadFile()
527 _ = file.LoadLastBodyTime()
528
529 isFav, _ := model.IsFavorited(userID, model.RESOURCE_TYPE_FILE, fileID)
530 file.IsFavorite = isFav
531

Callers 1

GetFileShareMethod · 0.85

Calls 8

parseMCPStyleIDFunction · 0.85
GetEIDMethod · 0.80
ToResponseMethod · 0.80
IsPersonalLibraryMethod · 0.80
LoadUploadFileMethod · 0.80
LoadLastBodyTimeMethod · 0.80
NewMethod · 0.45

Tested by

no test coverage detected