feat(File Management): Refactor File List API and Add Knowledge Base Document Initialization (#13914)

### What problem does this PR solve?

feat(File Management): Refactor File List API and Add Knowledge Base
Document Initialization

- Migrate the file list API endpoint from `/v1/file/list` to
`/api/v1/files` to align with the Python implementation.
- Add logic for initializing knowledge base documents; automatically
create the `.knowledgebase` folder and associated documents when
retrieving the root directory.
- Enhance parameter validation and error handling, including the
introduction of a new `CodeParamError` error code.
- Optimize the file list response structure to match the implementation
on the Python side.
- Update the Vite configuration to support proxying the new
`/api/v1/files` endpoint.

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
chanx
2026-04-03 15:08:43 +08:00
committed by GitHub
parent 6263857c1e
commit 21af67f6f9
6 changed files with 217 additions and 103 deletions

View File

@@ -41,20 +41,20 @@ func NewFileHandler(fileService *service.FileService, userService *service.UserS
}
}
// ListFiles list files
// ListFiles list files (new endpoint at /api/v1/files matching Python /files)
// @Summary List Files
// @Description Get list of files for the current user with filtering, pagination and sorting
// @Description Get list of files under a folder with filtering, pagination and sorting (matches Python /files endpoint)
// @Tags file
// @Accept json
// @Produce json
// @Param parent_id query string false "parent folder ID"
// @Param keywords query string false "search keywords"
// @Param page query int false "page number (default: 1)"
// @Param page_size query int false "items per page (default: 15)"
// @Param parent_id query string false "parent folder ID (empty means root folder)"
// @Param keywords query string false "search keywords (case-insensitive)"
// @Param page query int false "page number (default: 1, min: 1)"
// @Param page_size query int false "items per page (default: 15, min: 1, max: 100)"
// @Param orderby query string false "order by field (default: create_time)"
// @Param desc query bool false "descending order (default: true)"
// @Success 200 {object} service.ListFilesResponse
// @Router /v1/file/list [get]
// @Router /api/v1/files [get]
func (h *FileHandler) ListFiles(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
@@ -63,49 +63,52 @@ func (h *FileHandler) ListFiles(c *gin.Context) {
}
userID := user.ID
// Parse query parameters
parentID := c.Query("parent_id")
keywords := c.Query("keywords")
// Parse page (default: 1)
page := 1
if pageStr := c.Query("page"); pageStr != "" {
if p, err := strconv.Atoi(pageStr); err == nil && p > 0 {
if p, err := strconv.Atoi(pageStr); err == nil && p >= 1 {
page = p
} else if err != nil {
jsonError(c, common.CodeParamError, "Invalid page parameter: must be a positive integer")
return
}
}
// Parse page_size (default: 15)
pageSize := 15
if pageSizeStr := c.Query("page_size"); pageSizeStr != "" {
if ps, err := strconv.Atoi(pageSizeStr); err == nil && ps > 0 {
if ps, err := strconv.Atoi(pageSizeStr); err == nil {
if ps < 1 {
jsonError(c, common.CodeParamError, "Invalid page_size parameter: must be at least 1")
return
}
if ps > 100 {
ps = 100
}
pageSize = ps
} else {
jsonError(c, common.CodeParamError, "Invalid page_size parameter: must be a positive integer")
return
}
}
// Parse orderby (default: create_time)
orderby := c.DefaultQuery("orderby", "create_time")
// Parse desc (default: true)
desc := true
if descStr := c.Query("desc"); descStr != "" {
desc = descStr != "false"
}
// List files
result, err := h.fileService.ListFiles(userID, parentID, page, pageSize, orderby, desc, keywords)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": err.Error(),
})
jsonError(c, common.CodeServerError, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"code": common.CodeSuccess,
"data": result,
"message": "success",
"message": common.CodeSuccess.Message(),
})
}
@@ -128,17 +131,14 @@ func (h *FileHandler) GetRootFolder(c *gin.Context) {
// Get root folder
rootFolder, err := h.fileService.GetRootFolder(userID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": err.Error(),
})
jsonError(c, common.CodeServerError, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"code": common.CodeSuccess,
"data": gin.H{"root_folder": rootFolder},
"message": "success",
"message": common.CodeSuccess.Message(),
})
}
@@ -161,27 +161,21 @@ func (h *FileHandler) GetParentFolder(c *gin.Context) {
// Get file_id from query
fileID := c.Query("file_id")
if fileID == "" {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "file_id is required",
})
jsonError(c, common.CodeBadRequest, "file_id is required")
return
}
// Get parent folder
parentFolder, err := h.fileService.GetParentFolder(fileID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": err.Error(),
})
jsonError(c, common.CodeServerError, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"code": common.CodeSuccess,
"data": gin.H{"parent_folder": parentFolder},
"message": "success",
"message": common.CodeSuccess.Message(),
})
}
@@ -204,27 +198,21 @@ func (h *FileHandler) GetAllParentFolders(c *gin.Context) {
// Get file_id from query
fileID := c.Query("file_id")
if fileID == "" {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "file_id is required",
})
jsonError(c, common.CodeBadRequest, "file_id is required")
return
}
// Get all parent folders
parentFolders, err := h.fileService.GetAllParentFolders(fileID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": err.Error(),
})
jsonError(c, common.CodeServerError, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"code": common.CodeSuccess,
"data": gin.H{"parent_folders": parentFolders},
"message": "success",
"message": common.CodeSuccess.Message(),
})
}
@@ -248,10 +236,7 @@ type CreateFolderRequest struct {
func (h *FileHandler) UploadFile(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
c.JSON(http.StatusBadRequest, gin.H{
"code": errorCode,
"message": errorMessage,
})
jsonError(c, errorCode, errorMessage)
return
}
@@ -261,29 +246,20 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
if strings.Contains(contentType, "multipart/form-data") {
if err := c.Request.ParseMultipartForm(32 << 20); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "Failed to parse multipart form: " + err.Error(),
})
jsonError(c, common.CodeBadRequest, "Failed to parse multipart form: "+err.Error())
return
}
form := c.Request.MultipartForm
if form == nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "No file part!",
})
jsonError(c, common.CodeBadRequest, "No file part!")
return
}
parentID := c.PostForm("parent_id")
if parentID == "" {
rootFolder, err := h.fileService.GetRootFolder(userID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": err.Error(),
})
jsonError(c, common.CodeServerError, err.Error())
return
}
parentID = rootFolder["id"].(string)
@@ -291,36 +267,27 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
files := form.File["file"]
if len(files) == 0 {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "No file selected!",
})
jsonError(c, common.CodeBadRequest, "No file selected!")
return
}
for _, fileHeader := range files {
if fileHeader.Filename == "" {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "No file selected!",
})
jsonError(c, common.CodeBadRequest, "No file selected!")
return
}
}
result, err := h.fileService.UploadFile(userID, parentID, files)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": err.Error(),
})
jsonError(c, common.CodeBadRequest, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"code": common.CodeSuccess,
"data": result,
"message": "success",
"message": common.CodeSuccess.Message(),
})
return
}
@@ -339,10 +306,7 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
if parentID == "" {
rootFolder, err := h.fileService.GetRootFolder(userID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": err.Error(),
})
jsonError(c, common.CodeServerError, err.Error())
return
}
parentID = rootFolder["id"].(string)
@@ -350,24 +314,18 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
result, err := h.fileService.CreateFolder(userID, req.Name, parentID, req.Type)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": err.Error(),
})
jsonError(c, common.CodeBadRequest, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"code": common.CodeSuccess,
"data": result,
"message": "success",
"message": common.CodeSuccess.Message(),
})
return
}
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "Unsupported content type",
})
jsonError(c, common.CodeBadRequest, "Unsupported content type")
return
}