From d625340b405cbffa459a7dc0ab9eba36fd0c8c9f Mon Sep 17 00:00:00 2001 From: Henry Ruhs Date: Tue, 10 Nov 2026 00:52:41 +0100 Subject: [PATCH] allow to config the upload limit (#1274) * allow to config the upload limit * allow to config the upload limit * allow to config the upload limit --- facefusion.ini | 1 + facefusion/apis/core.py | 5 ++++- facefusion/args_helper.py | 1 + facefusion/choices.py | 1 + facefusion/locales.py | 1 + facefusion/program.py | 8 ++++++++ facefusion/types.py | 2 ++ tests/apis/endpoints/test_assets.py | 1 + tests/apis/endpoints/test_capabilities.py | 5 ++++- tests/apis/endpoints/test_jobs.py | 1 + tests/apis/endpoints/test_metrics.py | 1 + tests/apis/endpoints/test_ping.py | 1 + tests/apis/endpoints/test_process.py | 1 + tests/apis/endpoints/test_root.py | 8 +++++++- tests/apis/endpoints/test_session.py | 1 + tests/apis/endpoints/test_state.py | 1 + tests/apis/endpoints/test_stream.py | 1 + tests/apis/test_core.py | 14 ++++++++++++++ 18 files changed, 51 insertions(+), 3 deletions(-) diff --git a/facefusion.ini b/facefusion.ini index 8cccad95..bb38bcc3 100644 --- a/facefusion.ini +++ b/facefusion.ini @@ -150,6 +150,7 @@ api_host = api_port = api_security_strategy = api_session_limit = +api_upload_limit = [cors] cors_origins = diff --git a/facefusion/apis/core.py b/facefusion/apis/core.py index 4d6b52eb..239d8341 100644 --- a/facefusion/apis/core.py +++ b/facefusion/apis/core.py @@ -44,6 +44,7 @@ def pre_check() -> bool: def create_api() -> Starlette: cors_origins = state_manager.get_item('cors_origins') + api_upload_limit = state_manager.get_item('api_upload_limit') session_guard = Middleware(create_session_guard) routes =\ [ @@ -82,7 +83,9 @@ def create_api() -> Starlette: api = Starlette(routes = routes) api.add_middleware(create_exception_guard) api.add_middleware(create_security_guard) - api.add_middleware(RequestBodyLimitMiddleware, max_body_size = 512 * 1024 * 1024) + + if api_upload_limit > 0: + api.add_middleware(RequestBodyLimitMiddleware, max_body_size = api_upload_limit * 1024 * 1024) if cors_origins: api.add_middleware(CORSMiddleware, allow_origins = cors_origins, allow_methods = [ 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' ], allow_headers = [ 'Authorization', 'Content-Type' ], expose_headers = [ 'Location' ]) diff --git a/facefusion/args_helper.py b/facefusion/args_helper.py index 420e75d9..d7d5717a 100644 --- a/facefusion/args_helper.py +++ b/facefusion/args_helper.py @@ -83,6 +83,7 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None: apply_state_item('api_port', args.get('api_port')) apply_state_item('api_security_strategy', args.get('api_security_strategy')) apply_state_item('api_session_limit', args.get('api_session_limit')) + apply_state_item('api_upload_limit', args.get('api_upload_limit')) apply_state_item('cors_origins', args.get('cors_origins')) apply_state_item('video_memory_strategy', args.get('video_memory_strategy')) apply_state_item('log_level', args.get('log_level')) diff --git a/facefusion/choices.py b/facefusion/choices.py index 4278a303..06589ae9 100755 --- a/facefusion/choices.py +++ b/facefusion/choices.py @@ -162,6 +162,7 @@ progress_action_set : ProgressActionSet =\ job_statuses : List[JobStatus] = list(get_args(JobStatus)) api_session_limit_range : Sequence[int] = create_int_range(1, 32, 1) +api_upload_limit_range : Sequence[int] = create_int_range(32, 1024, 32) benchmark_cycle_count_range : Sequence[int] = create_int_range(1, 10, 1) execution_thread_count_range : Sequence[int] = create_int_range(1, 32, 1) face_detector_margin_range : Sequence[int] = create_int_range(0, 100, 1) diff --git a/facefusion/locales.py b/facefusion/locales.py index 0d376629..153088ca 100644 --- a/facefusion/locales.py +++ b/facefusion/locales.py @@ -160,6 +160,7 @@ LOCALES : Locales =\ 'api_port': 'specify the API port', 'api_security_strategy': 'specify the API security strategy used for sanitizing uploaded assets', 'api_session_limit': 'specify the maximum amount of concurrent sessions', + 'api_upload_limit': 'specify the maximum size of an upload', 'cors_origins': 'specify the CORS origins', 'execution_device_ids': 'specify the devices used for processing', 'execution_providers': 'inference using different providers (choices: {choices}, ...)', diff --git a/facefusion/program.py b/facefusion/program.py index f12582b4..ae28e14a 100755 --- a/facefusion/program.py +++ b/facefusion/program.py @@ -955,6 +955,14 @@ def create_api_program() -> ArgumentParser: choices = facefusion.choices.api_session_limit_range, metavar = create_int_metavar(facefusion.choices.api_session_limit_range) ) + group_api.add_argument( + '--api-upload-limit', + help = translator.get('help.api_upload_limit'), + type = int, + default = config.get_int_value('api', 'api_upload_limit', '512'), + choices = facefusion.choices.api_upload_limit_range, + metavar = create_int_metavar(facefusion.choices.api_upload_limit_range) + ) return program diff --git a/facefusion/types.py b/facefusion/types.py index 77c73569..9faecbac 100755 --- a/facefusion/types.py +++ b/facefusion/types.py @@ -662,6 +662,7 @@ StateKey = Literal\ 'api_key', 'api_security_strategy', 'api_session_limit', + 'api_upload_limit', 'cors_origins', 'job_id', 'job_status', @@ -738,6 +739,7 @@ State = TypedDict('State', 'api_key' : str, 'api_security_strategy' : ApiSecurityStrategy, 'api_session_limit' : int, + 'api_upload_limit' : int, 'cors_origins' : List[CorsOrigin], 'job_id' : JobId, 'job_status' : JobStatus, diff --git a/tests/apis/endpoints/test_assets.py b/tests/apis/endpoints/test_assets.py index e886566a..575d0beb 100644 --- a/tests/apis/endpoints/test_assets.py +++ b/tests/apis/endpoints/test_assets.py @@ -17,6 +17,7 @@ from tests.assert_helper import get_test_example_file, get_test_examples_directo @pytest.fixture(scope = 'module', autouse = True) def before_all() -> None: state_manager.init() + state_manager.init_item('api_upload_limit', 512) process_manager.start() conditional_download(get_test_examples_directory(), diff --git a/tests/apis/endpoints/test_capabilities.py b/tests/apis/endpoints/test_capabilities.py index 3a67badb..9387c1a3 100644 --- a/tests/apis/endpoints/test_capabilities.py +++ b/tests/apis/endpoints/test_capabilities.py @@ -4,7 +4,7 @@ from typing import Iterator import pytest from starlette.testclient import TestClient -from facefusion import capability_store, session_manager +from facefusion import capability_store, session_manager, state_manager from facefusion.apis.core import create_api @@ -33,6 +33,9 @@ def before_all() -> None: groups = [ 'paths' ] ) + state_manager.init() + state_manager.init_item('api_upload_limit', 512) + @pytest.fixture(scope = 'function', autouse = True) def before_each() -> None: diff --git a/tests/apis/endpoints/test_jobs.py b/tests/apis/endpoints/test_jobs.py index a134540e..a7dd8b88 100644 --- a/tests/apis/endpoints/test_jobs.py +++ b/tests/apis/endpoints/test_jobs.py @@ -17,6 +17,7 @@ from tests.assert_helper import get_test_example_file, get_test_examples_directo @pytest.fixture(scope = 'module', autouse = True) def before_all() -> None: state_manager.init() + state_manager.init_item('api_upload_limit', 512) create_program() diff --git a/tests/apis/endpoints/test_metrics.py b/tests/apis/endpoints/test_metrics.py index 5d3f9dea..b10a4a76 100644 --- a/tests/apis/endpoints/test_metrics.py +++ b/tests/apis/endpoints/test_metrics.py @@ -15,6 +15,7 @@ def before_all() -> None: state_manager.init() state_manager.init_item('jobs_path', get_test_jobs_directory()) state_manager.init_item('api_session_limit', 10) + state_manager.init_item('api_upload_limit', 512) @pytest.fixture(scope = 'function', autouse = True) diff --git a/tests/apis/endpoints/test_ping.py b/tests/apis/endpoints/test_ping.py index ad8f8326..aae749b7 100644 --- a/tests/apis/endpoints/test_ping.py +++ b/tests/apis/endpoints/test_ping.py @@ -14,6 +14,7 @@ def before_all() -> None: state_manager.init() state_manager.init_item('jobs_path', get_test_jobs_directory()) state_manager.init_item('api_session_limit', 10) + state_manager.init_item('api_upload_limit', 512) @pytest.fixture(scope = 'function', autouse = True) diff --git a/tests/apis/endpoints/test_process.py b/tests/apis/endpoints/test_process.py index 91d84057..c54f969b 100644 --- a/tests/apis/endpoints/test_process.py +++ b/tests/apis/endpoints/test_process.py @@ -15,6 +15,7 @@ def before_all() -> None: state_manager.init() state_manager.init_item('jobs_path', get_test_jobs_directory()) state_manager.init_item('api_session_limit', 10) + state_manager.init_item('api_upload_limit', 512) @pytest.fixture(scope = 'function', autouse = True) diff --git a/tests/apis/endpoints/test_root.py b/tests/apis/endpoints/test_root.py index 79b57b05..2e4d18c6 100644 --- a/tests/apis/endpoints/test_root.py +++ b/tests/apis/endpoints/test_root.py @@ -3,10 +3,16 @@ from typing import Iterator import pytest from starlette.testclient import TestClient -from facefusion import metadata, session_manager +from facefusion import metadata, session_manager, state_manager from facefusion.apis.core import create_api +@pytest.fixture(scope = 'module', autouse = True) +def before_all() -> None: + state_manager.init() + state_manager.init_item('api_upload_limit', 512) + + @pytest.fixture(scope = 'function', autouse = True) def before_each() -> None: session_manager.API_SESSIONS.clear() diff --git a/tests/apis/endpoints/test_session.py b/tests/apis/endpoints/test_session.py index eeac508b..7209ff65 100644 --- a/tests/apis/endpoints/test_session.py +++ b/tests/apis/endpoints/test_session.py @@ -20,6 +20,7 @@ from tests.assert_helper import get_test_example_file, get_test_examples_directo @pytest.fixture(scope = 'module', autouse = True) def before_all() -> None: state_manager.init() + state_manager.init_item('api_upload_limit', 512) datachannel_module.pre_check() diff --git a/tests/apis/endpoints/test_state.py b/tests/apis/endpoints/test_state.py index aad10b3f..fe4d9db1 100644 --- a/tests/apis/endpoints/test_state.py +++ b/tests/apis/endpoints/test_state.py @@ -16,6 +16,7 @@ def before_all() -> None: state_manager.init() state_manager.init_item('jobs_path', get_test_jobs_directory()) state_manager.init_item('api_session_limit', 10) + state_manager.init_item('api_upload_limit', 512) process_manager.start() program = ArgumentParser() diff --git a/tests/apis/endpoints/test_stream.py b/tests/apis/endpoints/test_stream.py index aa3d0b7e..47e599e9 100644 --- a/tests/apis/endpoints/test_stream.py +++ b/tests/apis/endpoints/test_stream.py @@ -26,6 +26,7 @@ def before_all() -> None: state_manager.init_item('temp_path', tempfile.gettempdir()) state_manager.init_item('jobs_path', get_test_jobs_directory()) state_manager.init_item('api_session_limit', 10) + state_manager.init_item('api_upload_limit', 512) state_manager.init_item('processors', []) pre_check() diff --git a/tests/apis/test_core.py b/tests/apis/test_core.py index 203e78b9..b167f448 100644 --- a/tests/apis/test_core.py +++ b/tests/apis/test_core.py @@ -16,6 +16,7 @@ def before_all() -> None: state_manager.init_item('api_host', '127.0.0.1') state_manager.init_item('api_port', 7000) state_manager.init_item('api_session_limit', 4) + state_manager.init_item('api_upload_limit', 512) local_id = session_context.resolve_local_id() session_context.set_session_id(local_id) @@ -57,6 +58,19 @@ def test_create_api(test_client : TestClient) -> None: assert root_response.headers.get('access-control-expose-headers') == 'Location' assert root_response.status_code == 200 + state_manager.set_item('api_upload_limit', 1) + + with TestClient(create_api()) as upload_limit_client: + session_response = upload_limit_client.post('/session', content = bytes(1024 * 1024)) + + assert not session_response.status_code == 413 + + session_response = upload_limit_client.post('/session', content = bytes(1024 * 1024 + 1)) + + assert session_response.status_code == 413 + + state_manager.set_item('api_upload_limit', 512) + def test_create_server() -> None: server = create_server()