ParentExists godoc @Summary 查看文档父级是否存在 @Description 检查指定文件的父级路径对应的目录是否存在且未被软删除 @Tags 文件管理 @Accept json @Produce json @Security BearerAuth @Param file_id path int true "文件ID" @Success 200 {object} model.CommonResponse{data=map[string]interface{}} @Router /api/files/{file_id}/parent-exists [get]
(c *gin.Context)
| 92 | // @Success 200 {object} model.CommonResponse{data=map[string]interface{}} |
| 93 | // @Router /api/files/{file_id}/parent-exists [get] |
| 94 | func ParentExists(c *gin.Context) { |
| 95 | eid := config.GetEID(c) |
| 96 | // userID := config.GetUserId(c) |
| 97 | |
| 98 | id := c.Param("file_id") |
| 99 | if id == "" { |
| 100 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("文件ID不能为空"))) |
| 101 | return |
| 102 | } |
| 103 | |
| 104 | fileID, err := parseMCPStyleID(id) |
| 105 | if err != nil { |
| 106 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("无效的文件ID"))) |
| 107 | return |
| 108 | } |
| 109 | |
| 110 | // 获取文件信息 |
| 111 | file, err := model.GetFileByID(eid, fileID) |
| 112 | if err != nil { |
| 113 | c.JSON(http.StatusNotFound, model.NotFound.ToResponse(err)) |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | // 计算父级路径 |
| 118 | parentPath := "/" |
| 119 | if file.Path != "/" { |
| 120 | pp := filepath.Dir(file.Path) |
| 121 | if pp == "." || pp == "" { |
| 122 | parentPath = "/" |
| 123 | } else { |
| 124 | parentPath = pp |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // 根目录视为存在 |
| 129 | if parentPath == "/" { |
| 130 | c.JSON(http.StatusOK, model.Success.ToResponse(map[string]interface{}{ |
| 131 | "exists": true, |
| 132 | "parent_path": "/", |
| 133 | "parent_id": nil, |
| 134 | })) |
| 135 | return |
| 136 | } |
| 137 | |
| 138 | // 查询父级 |
| 139 | parent, _ := model.GetFileByPathAndLibrary(eid, file.LibraryID, parentPath) |
| 140 | if parent == nil || parent.IsDeleted { |
| 141 | c.JSON(http.StatusOK, model.Success.ToResponse(map[string]interface{}{ |
| 142 | "exists": false, |
| 143 | "parent_path": parentPath, |
| 144 | "parent_id": nil, |
| 145 | })) |
| 146 | return |
| 147 | } |
| 148 | |
| 149 | c.JSON(http.StatusOK, model.Success.ToResponse(map[string]interface{}{ |
| 150 | "exists": true, |
| 151 | "parent_path": parentPath, |
nothing calls this directly
no test coverage detected