RestoreFile godoc @Summary 恢复文件或文件夹 @Description 恢复被软删除的文件(支持根据参数在父级不存在时恢复到根目录) @Tags 文件管理 @Accept json @Produce json @Security BearerAuth @Param file_id path int true "文件ID" @Param request body RestoreFileRequest false "恢复文件请求参数" @Success 200 {object} model.CommonResponse @Router /api/files/{file_i
(c *gin.Context)
| 98 | // @Success 200 {object} model.CommonResponse |
| 99 | // @Router /api/files/{file_id}/restore [post] |
| 100 | func RestoreFile(c *gin.Context) { |
| 101 | eid := config.GetEID(c) |
| 102 | userID := config.GetUserId(c) |
| 103 | |
| 104 | id := c.Param("file_id") |
| 105 | if id == "" { |
| 106 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("文件ID不能为空"))) |
| 107 | return |
| 108 | } |
| 109 | fileID, err := parseMCPStyleID(id) |
| 110 | if err != nil { |
| 111 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("无效的文件ID"))) |
| 112 | return |
| 113 | } |
| 114 | file, err := model.GetFileByID(eid, fileID) |
| 115 | if err != nil { |
| 116 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("无效的文件ID"))) |
| 117 | return |
| 118 | } |
| 119 | |
| 120 | // 权限 |
| 121 | maxPermission, err := service.GetUserPermission(eid, model.RESOURCE_TYPE_LIBRARY, file.LibraryID, userID) |
| 122 | if err != nil || maxPermission < model.PERMISSION_MANAGE { |
| 123 | c.JSON(http.StatusForbidden, model.AuthFailed.ToResponse(errors.New("无权限管理该知识库"))) |
| 124 | return |
| 125 | } |
| 126 | |
| 127 | // 解析请求体参数 |
| 128 | var req RestoreFileRequest |
| 129 | if err := c.ShouldBindJSON(&req); err != nil { |
| 130 | // 如果没有提供请求体或解析失败,使用默认值 |
| 131 | req.RestoreToRootIfParentMissing = false |
| 132 | } |
| 133 | |
| 134 | if err := model.RestoreDeletedFile(eid, fileID, req.RestoreToRootIfParentMissing, userID); err != nil { |
| 135 | logger.SysLogf("恢复失败: eid=%d fileID=%d userID=%d err=%v", eid, fileID, userID, err) |
| 136 | c.JSON(http.StatusInternalServerError, model.SystemError.ToResponse(err)) |
| 137 | return |
| 138 | } |
| 139 | common.DeleteFileStopCache(fileID) // 删除文件的停止信号缓存 |
| 140 | |
| 141 | // 更新 file 对象的 IsDeleted 状态,确保 ES 索引正确 |
| 142 | file.IsDeleted = false |
| 143 | |
| 144 | // 恢复完成后,重新建立 Elasticsearch 索引 |
| 145 | go indexRestoredFilesAsync(eid, file) |
| 146 | |
| 147 | // 记录系统日志 |
| 148 | library, _ := model.GetLibraryByID(eid, file.LibraryID) |
| 149 | space, _ := model.GetSpaceByID(eid, library.SpaceID) |
| 150 | fileName := filepath.Base(file.Path) |
| 151 | |
| 152 | log := model.SystemLog{ |
| 153 | Eid: eid, |
| 154 | UserID: userID, |
| 155 | Nickname: config.GetUserNickname(c), |
| 156 | Module: model.SystemLogModuleFile, |
| 157 | Action: model.SystemLogActionRestore, |
nothing calls this directly
no test coverage detected