DeleteFile godoc @Summary 删除文件 @Description 删除文件接口 @Tags 文件管理 @Accept json @Produce json @Security BearerAuth @Param file_id path int true "文件ID" @Success 200 {object} model.CommonResponse @Router /api/files/{file_id} [delete]
(c *gin.Context)
| 1023 | // @Success 200 {object} model.CommonResponse |
| 1024 | // @Router /api/files/{file_id} [delete] |
| 1025 | func DeleteFile(c *gin.Context) { |
| 1026 | eid := config.GetEID(c) |
| 1027 | userID := config.GetUserId(c) |
| 1028 | |
| 1029 | id := c.Param("file_id") |
| 1030 | if id == "" { |
| 1031 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("文件ID不能为空"))) |
| 1032 | return |
| 1033 | } |
| 1034 | |
| 1035 | fileID, err := parseMCPStyleID(id) |
| 1036 | if err != nil { |
| 1037 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("无效的文件ID"))) |
| 1038 | return |
| 1039 | } |
| 1040 | |
| 1041 | // 获取文件信息以检查权限和记录日志 |
| 1042 | file, err := model.GetFileByID(eid, fileID) |
| 1043 | if err != nil { |
| 1044 | c.JSON(http.StatusNotFound, model.NotFound.ToResponse(err)) |
| 1045 | return |
| 1046 | } |
| 1047 | |
| 1048 | // 获取知识库和空间信息用于日志 |
| 1049 | library, _ := model.GetLibraryByID(eid, file.LibraryID) |
| 1050 | space, _ := model.GetSpaceByID(eid, library.SpaceID) |
| 1051 | isPersonalLibrary := library.IsPersonalLibrary() |
| 1052 | |
| 1053 | permisson, err := service.GetUserPermission(eid, model.RESOURCE_TYPE_FILE, fileID, userID) |
| 1054 | if err != nil || permisson < model.PERMISSION_MANAGE { |
| 1055 | c.JSON(http.StatusForbidden, model.AuthFailed.ToResponse(err)) |
| 1056 | return |
| 1057 | } |
| 1058 | |
| 1059 | common.SetFileStop(file.ID) |
| 1060 | |
| 1061 | if err := model.SoftDeleteFile(eid, fileID, userID); err != nil { |
| 1062 | logger.SysLogf("软删除失败: eid=%d fileID=%d userID=%d err=%v", eid, fileID, userID, err) |
| 1063 | c.JSON(http.StatusInternalServerError, model.FileError.ToResponse(err)) |
| 1064 | return |
| 1065 | } |
| 1066 | |
| 1067 | // 从 Elasticsearch 移除索引 |
| 1068 | go removeFileIndicesAsync(eid, file) |
| 1069 | |
| 1070 | // 记录系统日志 |
| 1071 | fileName := filepath.Base(file.Path) |
| 1072 | scopeName := "知识库" |
| 1073 | if isPersonalLibrary { |
| 1074 | scopeName = "个人空间" |
| 1075 | } |
| 1076 | log := model.SystemLog{ |
| 1077 | Eid: eid, |
| 1078 | UserID: config.GetUserId(c), |
| 1079 | Nickname: config.GetUserNickname(c), |
| 1080 | Module: model.SystemLogModuleFile, |
| 1081 | Action: model.SystemLogActionDelete, |
| 1082 | Content: fmt.Sprintf("从【%s】%s【%s】删除了《%s》", space.Name, scopeName, library.Name, fileName), |
nothing calls this directly
no test coverage detected