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)
| 472 | // @Success 200 {object} model.CommonResponse{data=model.File} |
| 473 | // @Router /api/files/{file_id} [get] |
| 474 | func 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 |
no test coverage detected