From f2b7df94d7b5baac6918d270074388084051d2c6 Mon Sep 17 00:00:00 2001 From: kurokobo Date: Mon, 10 Mar 2025 14:15:06 +0900 Subject: [PATCH] fix: return absolute path as the icon url if CONSOLE_API_URL is empty (#15279) --- api/core/tools/tool_manager.py | 17 +++++++++++------ api/services/tools/tools_transform_service.py | 8 ++++++-- api/tests/unit_tests/libs/test_yarl.py | 6 ++++++ docker/nginx/conf.d/default.conf.template | 13 ------------- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/api/core/tools/tool_manager.py b/api/core/tools/tool_manager.py index af6541dfe0..1caf02192d 100644 --- a/api/core/tools/tool_manager.py +++ b/api/core/tools/tool_manager.py @@ -765,17 +765,22 @@ class ToolManager: @classmethod def generate_builtin_tool_icon_url(cls, provider_id: str) -> str: - return ( - dify_config.CONSOLE_API_URL - + "/console/api/workspaces/current/tool-provider/builtin/" - + provider_id - + "/icon" + return str( + URL(dify_config.CONSOLE_API_URL or "/") + / "console" + / "api" + / "workspaces" + / "current" + / "tool-provider" + / "builtin" + / provider_id + / "icon" ) @classmethod def generate_plugin_tool_icon_url(cls, tenant_id: str, filename: str) -> str: return str( - URL(dify_config.CONSOLE_API_URL) + URL(dify_config.CONSOLE_API_URL or "/") / "console" / "api" / "workspaces" diff --git a/api/services/tools/tools_transform_service.py b/api/services/tools/tools_transform_service.py index 83a42ddfcb..d44151befa 100644 --- a/api/services/tools/tools_transform_service.py +++ b/api/services/tools/tools_transform_service.py @@ -29,7 +29,9 @@ logger = logging.getLogger(__name__) class ToolTransformService: @classmethod def get_plugin_icon_url(cls, tenant_id: str, filename: str) -> str: - url_prefix = URL(dify_config.CONSOLE_API_URL) / "console" / "api" / "workspaces" / "current" / "plugin" / "icon" + url_prefix = ( + URL(dify_config.CONSOLE_API_URL or "/") / "console" / "api" / "workspaces" / "current" / "plugin" / "icon" + ) return str(url_prefix % {"tenant_id": tenant_id, "filename": filename}) @classmethod @@ -37,7 +39,9 @@ class ToolTransformService: """ get tool provider icon url """ - url_prefix = URL(dify_config.CONSOLE_API_URL) / "console" / "api" / "workspaces" / "current" / "tool-provider" + url_prefix = ( + URL(dify_config.CONSOLE_API_URL or "/") / "console" / "api" / "workspaces" / "current" / "tool-provider" + ) if provider_type == ToolProviderType.BUILT_IN.value: return str(url_prefix / "builtin" / provider_name / "icon") diff --git a/api/tests/unit_tests/libs/test_yarl.py b/api/tests/unit_tests/libs/test_yarl.py index b9aee4af5f..ff1a3e80d0 100644 --- a/api/tests/unit_tests/libs/test_yarl.py +++ b/api/tests/unit_tests/libs/test_yarl.py @@ -18,6 +18,12 @@ def test_yarl_urls(): assert str(URL("https://dify.ai/api") / "v1") == expected_3 assert str(URL("https://dify.ai/api/") / "v1") == expected_3 + expected_4 = "api" + assert str(URL("") / "api") == expected_4 + + expected_5 = "/api" + assert str(URL("/") / "api") == expected_5 + with pytest.raises(ValueError) as e1: str(URL("https://dify.ai") / "/api") assert str(e1.value) == "Appending path '/api' starting from slash is forbidden" diff --git a/docker/nginx/conf.d/default.conf.template b/docker/nginx/conf.d/default.conf.template index 44191ca1d0..a458412d1e 100644 --- a/docker/nginx/conf.d/default.conf.template +++ b/docker/nginx/conf.d/default.conf.template @@ -4,19 +4,6 @@ server { listen ${NGINX_PORT}; server_name ${NGINX_SERVER_NAME}; - # Rule 1: Handle application entry points (preserve /app/{id}) - location ~ ^/app/[a-f0-9-]+$ { - proxy_pass http://api:5001; - include proxy.conf; - } - - # Rule 2: Handle static resource requests (remove /app/{id} prefix) - location ~ ^/app/[a-f0-9-]+/(console/api/.*)$ { - rewrite ^/app/[a-f0-9-]+/(.*)$ /$1 break; - proxy_pass http://api:5001; - include proxy.conf; - } - location /console/api { proxy_pass http://api:5001; include proxy.conf;