DeleteFileAsync godoc @Summary 异步删除文件 @Description 异步删除文件接口,支持级联删除相关数据 @Tags 文件管理 @Accept json @Produce json @Security BearerAuth @Param file_id path int true "文件ID" @Param async query bool false "是否异步删除" default(false) @Success 200 {object} model.CommonResponse @Router /api/files/{file_id}/delete-a
(c *gin.Context)
| 1122 | // @Success 200 {object} model.CommonResponse |
| 1123 | // @Router /api/files/{file_id}/delete-async [delete] |
| 1124 | func DeleteFileAsync(c *gin.Context) { |
| 1125 | eid := config.GetEID(c) |
| 1126 | userID := config.GetUserId(c) |
| 1127 | |
| 1128 | id := c.Param("file_id") |
| 1129 | if id == "" { |
| 1130 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("文件ID不能为空"))) |
| 1131 | return |
| 1132 | } |
| 1133 | |
| 1134 | fileID, err := parseMCPStyleID(id) |
| 1135 | if err != nil { |
| 1136 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("无效的文件ID"))) |
| 1137 | return |
| 1138 | } |
| 1139 | |
| 1140 | // 获取文件信息以检查权限和记录日志 |
| 1141 | file, err := model.GetFileByID(eid, fileID) |
| 1142 | if err != nil { |
| 1143 | c.JSON(http.StatusNotFound, model.NotFound.ToResponse(err)) |
| 1144 | return |
| 1145 | } |
| 1146 | |
| 1147 | // 获取知识库和空间信息用于日志 |
| 1148 | library, _ := model.GetLibraryByID(eid, file.LibraryID) |
| 1149 | space, _ := model.GetSpaceByID(eid, library.SpaceID) |
| 1150 | |
| 1151 | permisson, err := service.GetUserPermission(eid, model.RESOURCE_TYPE_FILE, fileID, userID) |
| 1152 | if err != nil || permisson < model.PERMISSION_MANAGE { |
| 1153 | c.JSON(http.StatusForbidden, model.AuthFailed.ToResponse(err)) |
| 1154 | return |
| 1155 | } |
| 1156 | |
| 1157 | // 检查是否异步删除 |
| 1158 | async := c.DefaultQuery("async", "false") == "true" |
| 1159 | |
| 1160 | // 执行删除(现在都是级联删除) |
| 1161 | if err := model.DeleteFile(eid, fileID); err != nil { |
| 1162 | c.JSON(http.StatusInternalServerError, model.FileError.ToResponse(err)) |
| 1163 | return |
| 1164 | } |
| 1165 | |
| 1166 | // 记录系统日志 |
| 1167 | fileName := filepath.Base(file.Path) |
| 1168 | log := model.SystemLog{ |
| 1169 | Eid: eid, |
| 1170 | UserID: config.GetUserId(c), |
| 1171 | Nickname: config.GetUserNickname(c), |
| 1172 | Module: model.SystemLogModuleFile, |
| 1173 | Action: model.SystemLogActionDelete, |
| 1174 | Content: fmt.Sprintf("从【%s】知识库【%s】删除了《%s》", space.Name, library.Name, fileName), |
| 1175 | IP: utils.GetClientIP(c), |
| 1176 | } |
| 1177 | model.CreateSystemLog(&log) |
| 1178 | |
| 1179 | // 从 Elasticsearch 移除索引 |
| 1180 | elasticsearch.SyncFileToES(file, "delete") |
| 1181 |
nothing calls this directly
no test coverage detected