mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-09-08 10:14:35 +08:00
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:
@@ -68,20 +68,26 @@ func (s *FileService) GetRootFolder(tenantID string) (map[string]interface{}, er
|
||||
return s.toFileResponse(file), nil
|
||||
}
|
||||
|
||||
// ListFiles lists files by parent folder ID
|
||||
// ListFiles lists files by parent folder ID (matching Python /files endpoint)
|
||||
// This method includes init_knowledgebase_docs initialization when parent_id is empty
|
||||
func (s *FileService) ListFiles(tenantID, pfID string, page, pageSize int, orderby string, desc bool, keywords string) (*ListFilesResponse, error) {
|
||||
// If pfID is empty, get root folder
|
||||
// If pfID is empty, get root folder and initialize knowledgebase docs
|
||||
if pfID == "" {
|
||||
rootFolder, err := s.fileDAO.GetRootFolder(tenantID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("failed to get root folder: %w", err)
|
||||
}
|
||||
pfID = rootFolder.ID
|
||||
|
||||
// Initialize knowledgebase docs (matching Python init_knowledgebase_docs logic)
|
||||
if err := s.initKnowledgebaseDocs(pfID, tenantID); err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize knowledgebase docs: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Check if parent folder exists
|
||||
if _, err := s.fileDAO.GetByID(pfID); err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Folder not found!")
|
||||
}
|
||||
|
||||
// Get files by parent folder ID
|
||||
@@ -93,16 +99,16 @@ func (s *FileService) ListFiles(tenantID, pfID string, page, pageSize int, order
|
||||
// Get parent folder
|
||||
parentFolder, err := s.fileDAO.GetParentFolder(pfID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("File not found!")
|
||||
}
|
||||
|
||||
// Process files to add additional info
|
||||
fileResponses := make([]map[string]interface{}, len(files))
|
||||
for i, file := range files {
|
||||
fileResponses := make([]map[string]interface{}, 0, len(files))
|
||||
for _, file := range files {
|
||||
fileInfo := s.toFileInfo(file)
|
||||
|
||||
// If folder, calculate size and check for child folders
|
||||
if file.Type == "folder" {
|
||||
if file.Type == FileTypeFolder {
|
||||
folderSize, err := s.fileDAO.GetFolderSize(file.ID)
|
||||
if err == nil {
|
||||
fileInfo.Size = folderSize
|
||||
@@ -121,7 +127,7 @@ func (s *FileService) ListFiles(tenantID, pfID string, page, pageSize int, order
|
||||
fileInfo.KbsInfo = kbsInfo
|
||||
}
|
||||
|
||||
fileResponses[i] = s.fileInfoToResponse(fileInfo)
|
||||
fileResponses = append(fileResponses, s.fileInfoToResponse(fileInfo))
|
||||
}
|
||||
|
||||
return &ListFilesResponse{
|
||||
@@ -131,6 +137,18 @@ func (s *FileService) ListFiles(tenantID, pfID string, page, pageSize int, order
|
||||
}, nil
|
||||
}
|
||||
|
||||
// initKnowledgebaseDocs initializes knowledgebase documents for tenant
|
||||
// This matches Python's FileService.init_knowledgebase_docs method
|
||||
func (s *FileService) initKnowledgebaseDocs(rootID, tenantID string) error {
|
||||
return s.fileDAO.InitKnowledgebaseDocs(rootID, tenantID, s.file2DocumentDAO)
|
||||
}
|
||||
|
||||
// KnowledgebaseFolderName is the folder name for knowledgebase
|
||||
const KnowledgebaseFolderName = ".knowledgebase"
|
||||
|
||||
// FileSourceKnowledgebase represents knowledgebase as file source
|
||||
const FileSourceKnowledgebase = "knowledgebase"
|
||||
|
||||
// toFileResponse converts file model to response format
|
||||
func (s *FileService) toFileResponse(file *entity.File) map[string]interface{} {
|
||||
result := map[string]interface{}{
|
||||
|
||||
Reference in New Issue
Block a user