files.py
Overview
The files.py module provides a set of RESTful API endpoints for managing files and folders within a multi-tenant file management system. It is implemented using the Flask web framework and secured with token authentication. The functionalities include uploading files, creating folders, listing files, retrieving folder hierarchies, renaming, moving, deleting files/folders, and downloading files.
This module acts as a controller layer interfacing HTTP requests with the underlying services (FileService, File2DocumentService, DocumentService) and a storage backend (STORAGE_IMPL). It orchestrates file metadata management in the database and file content management in the storage system.
Classes and Functions
This file does not define new classes but contains multiple Flask route handler functions. Each function corresponds to a specific API endpoint related to file management.
1. upload(tenant_id)
Route: /file/upload
Method: POST
Description: Upload one or more files to a specified folder (or root folder by default) for a tenant.
Parameters (via form-data):
file: Required. One or more files to upload.parent_id: Optional. ID of the parent folder to upload into. Defaults to tenant root folder.
Returns: JSON response containing an array of uploaded file metadata (id, name, size, type).
Key Steps:
Validate presence of files.
Determine parent folder (default to root if not specified).
For each file:
Parse file path components and create missing folder structure if needed.
Generate unique file name to avoid duplicates.
Save file metadata in DB and file content in storage.
Return metadata for all uploaded files or error on failure.
Example Usage:
curl -X POST -H "Authorization: Bearer <token>" -F [email protected] -F parent_id=<folder_id> https://api.example.com/file/upload
2. create(tenant_id)
Route: /file/create
Method: POST
Description: Create a new folder or virtual file under a specified parent folder (or root folder by default).
Request JSON:
{
"name": "New Folder",
"parent_id": "<parent_folder_id>", // Optional
"type": "FOLDER" // or "VIRTUAL"
}
Returns: JSON response with the created file/folder metadata.
Key Points:
Validates parent folder existence.
Checks for duplicate names within the parent.
Inserts new file/folder metadata with generated UUID.
Supports two types:
FOLDERandVIRTUAL.
3. list_files(tenant_id)
Route: /file/list
Method: GET
Description: List files and folders within a specified folder, with optional filtering, pagination, and sorting.
Query Parameters:
parent_id: Folder ID to list from (defaults to root folder).keywords: Search filter string.page: Page number (default 1).page_size: Number of items per page (default 15).orderby: Field to sort by (default "create_time").desc: Boolean for descending order (default true).
Returns: JSON response with total count, list of files, and parent folder metadata.
Additional Behavior:
Initializes knowledgebase documents for root folder if listing from root.
4. get_root_folder(tenant_id)
Route: /file/root_folder
Method: GET
Description: Retrieve the root folder information for the authenticated tenant.
Returns: JSON with root folder metadata (id, name, type).
5. get_parent_folder()
Route: /file/parent_folder
Method: GET
Description: Get the immediate parent folder of a specified file.
Query Parameters:
file_id: The file ID to find the parent for (required).
Returns: JSON with parent folder metadata.
6. get_all_parent_folders(tenant_id)
Route: /file/all_parent_folder
Method: GET
Description: Retrieve all ancestor folders for a given file, effectively the full folder path hierarchy.
Query Parameters:
file_id: Target file ID (required).
Returns: JSON array of parent folders ordered from closest to root.
7. rm(tenant_id)
Route: /file/rm
Method: POST
Description: Delete one or multiple files or folders.
Request JSON:
{
"file_ids": ["id1", "id2", ...]
}
Behavior:
For folders, recursively deletes all innermost files and subfolders.
Removes file content from storage.
Deletes associated document records and links.
Returns success boolean or error on failure.
8. rename(tenant_id)
Route: /file/rename
Method: POST
Description: Rename a file or folder.
Request JSON:
{
"file_id": "<file_id>",
"name": "new_name"
}
Constraints:
File extensions cannot be changed for non-folder files.
No duplicate names allowed in the same folder.
Behavior:
Updates file metadata.
Updates linked document name if applicable.
9. get(tenant_id, file_id)
Route: /file/get/<file_id>
Method: GET
Description: Download a file by its ID.
Response:
Returns file stream with appropriate content-type header based on file extension and type.
Returns 404 if file or content not found.
10. move(tenant_id)
Route: /file/mv
Method: POST
Description: Move one or multiple files/folders to another folder.
Request JSON:
{
"src_file_ids": ["id1", "id2", ...],
"dest_file_id": "<destination_folder_id>"
}
Behavior:
Validates source files and destination folder existence.
Updates parent folder references in the database.
Important Implementation Details and Algorithms
Folder Structure Creation in Upload:
The upload function parses the file path and compares it against existing folder IDs. It creates missing folders dynamically to replicate subfolder structure present in the uploaded file paths.Duplicate Name Handling:
Both upload and create endpoints ensure no duplicate file/folder names exist within the same parent folder by checking existing entries and modifying names if necessary.Storage Abstraction:
File content is handled viaSTORAGE_IMPLwhich abstracts underlying storage implementation details. It provides methods likeput,get,rm, andobj_existfor managing file blobs.Tenant Isolation:
All operations are tenant-scoped using thetenant_idparameter obtained via the@token_requireddecorator, enforcing multi-tenancy security and data separation.File and Document Synchronization:
Many file operations (rename, delete) also synchronize changes to linked documents viaFile2DocumentServiceandDocumentServiceto maintain data consistency.Error Handling:
Most endpoints wrap logic in try-except blocks returning server error responses on exceptions, ensuring robustness.
Interaction with Other Modules
FileService: Core service managing file metadata CRUD, folder hierarchy, and queries.File2DocumentService&DocumentService: Manage relationships between files and documents and synchronize document data on file changes.STORAGE_IMPL: Storage backend interface for actual file content persistence.api.utils.api_utils: Utility functions for API responses, authentication (token_required), and JSON formatting.api.utils.file_utils: Utility for determining file types based on names/extensions.api.db: Database models and enums such asFileType.manager: Flask blueprint or app instance where routes are registered (assumed imported or defined elsewhere).
Visual Diagram
The following Mermaid class diagram illustrates the structure of the key services and API endpoint functions in files.py, showing their methods and interactions:
classDiagram
class FileService {
+get_root_folder(tenant_id)
+get_by_id(file_id)
+get_id_list_by_id(parent_id, path_names, start_idx, id_list)
+create_folder(...)
+insert(file_data)
+query(name, parent_id)
+is_parent_folder_exist(folder_id)
+get_by_pf_id(tenant_id, pf_id, page, size, orderby, desc, keywords)
+get_parent_folder(file_id)
+get_all_parent_folders(file_id)
+get_all_innermost_file_ids(folder_id, ids)
+delete_folder_by_pf_id(tenant_id, folder_id)
+delete(file)
+update_by_id(file_id, update_data)
+move_file(file_ids, dest_folder_id)
+get_by_ids(file_ids)
+init_knowledgebase_docs(root_id, tenant_id)
}
class File2DocumentService {
+get_by_file_id(file_id)
+delete_by_file_id(file_id)
+get_storage_address(file_id)
}
class DocumentService {
+get_by_id(doc_id)
+get_tenant_id(doc_id)
+remove_document(doc, tenant_id)
+update_by_id(doc_id, update_data)
}
class STORAGE_IMPL {
+obj_exist(parent_id, location)
+put(parent_id, location, blob)
+get(parent_id, location)
+rm(parent_id, location)
}
class files_py {
+upload(tenant_id)
+create(tenant_id)
+list_files(tenant_id)
+get_root_folder(tenant_id)
+get_parent_folder()
+get_all_parent_folders(tenant_id)
+rm(tenant_id)
+rename(tenant_id)
+get(tenant_id, file_id)
+move(tenant_id)
}
files_py --> FileService : uses
files_py --> File2DocumentService : uses
files_py --> DocumentService : uses
files_py --> STORAGE_IMPL : uses
Summary
The files.py module provides comprehensive file management REST APIs for a tenant-aware backend system. It leverages service layers for database operations and abstracted storage for file contents. The module ensures integrity and consistency between file metadata and linked documents and supports common file operations with security and error handling.
This module is central to the file management subsystem and interacts closely with database services, document management, and the storage layer, forming a critical component of the overall application infrastructure.