From 083f4805b28da4a1869c2e21eba731863fdbd943 Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Tue, 26 Aug 2025 20:41:01 +0800 Subject: [PATCH] feat: Enhance get_preview_static_url to find the longest matching route for static URLs --- py/config.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/py/config.py b/py/config.py index d96c0c35..46393a50 100644 --- a/py/config.py +++ b/py/config.py @@ -268,19 +268,26 @@ class Config: return [] def get_preview_static_url(self, preview_path: str) -> str: - """Convert local preview path to static URL""" if not preview_path: return "" real_path = os.path.realpath(preview_path).replace(os.sep, '/') - + + # Find longest matching path (most specific match) + best_match = "" + best_route = "" + for path, route in self._route_mappings.items(): - if real_path.startswith(path): - relative_path = os.path.relpath(real_path, path).replace(os.sep, '/') - safe_parts = [urllib.parse.quote(part) for part in relative_path.split('/')] - safe_path = '/'.join(safe_parts) - return f'{route}/{safe_path}' - + if real_path.startswith(path) and len(path) > len(best_match): + best_match = path + best_route = route + + if best_match: + relative_path = os.path.relpath(real_path, best_match).replace(os.sep, '/') + safe_parts = [urllib.parse.quote(part) for part in relative_path.split('/')] + safe_path = '/'.join(safe_parts) + return f'{best_route}/{safe_path}' + return "" # Global config instance