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):

Returns: JSON response containing an array of uploaded file metadata (id, name, size, type).

Key Steps:

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:


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:

Returns: JSON response with total count, list of files, and parent folder metadata.

Additional Behavior:


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:

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:

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:


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:

Behavior:


9. get(tenant_id, file_id)

Route: /file/get/<file_id>
Method: GET
Description: Download a file by its ID.

Response:


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:


Important Implementation Details and Algorithms


Interaction with Other Modules


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.