From 3871557868812b5c2a15d76bab6217087233d86f Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Sun, 23 Feb 2025 14:03:55 +0800 Subject: [PATCH] Fix symbolic links --- config.py | 3 +-- lora_manager.py | 15 ++++++++++----- routes/api_routes.py | 12 ++++++------ services/file_monitor.py | 7 +++++-- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/config.py b/config.py index 97a55324..946493b6 100644 --- a/config.py +++ b/config.py @@ -82,7 +82,6 @@ class Config: if normalized_path.startswith(target_path): # 如果路径以目标路径开头,则替换为链接路径 mapped_path = normalized_path.replace(target_path, link_path, 1) - logger.info(f"Mapped path {normalized_path} to {mapped_path}") return mapped_path return path @@ -100,7 +99,7 @@ class Config: for path in paths: real_path = os.path.normpath(os.path.realpath(path)).replace(os.sep, '/') if real_path != path: - self.add_path_mapping(real_path, path) + self.add_path_mapping(path, real_path) return paths diff --git a/lora_manager.py b/lora_manager.py index 3e32fc90..8e0208f4 100644 --- a/lora_manager.py +++ b/lora_manager.py @@ -25,13 +25,19 @@ class LoraManager: for idx, root in enumerate(config.loras_roots, start=1): preview_path = f'/loras_static/root{idx}/preview' + real_root = root + if root in config._path_mappings.values(): + for target, link in config._path_mappings.items(): + if link == root: + real_root = target + break # 为原始路径添加静态路由 - app.router.add_static(preview_path, root) - logger.info(f"Added static route {preview_path} -> {root}") + app.router.add_static(preview_path, real_root) + logger.info(f"Added static route {preview_path} -> {real_root}") # 记录路由映射 - config.add_route_mapping(root, preview_path) - added_targets.add(root) + config.add_route_mapping(real_root, preview_path) + added_targets.add(real_root) # 为符号链接的目标路径添加额外的静态路由 link_idx = 1 @@ -42,7 +48,6 @@ class LoraManager: app.router.add_static(route_path, target_path) logger.info(f"Added static route for link target {route_path} -> {target_path}") config.add_route_mapping(target_path, route_path) - config.add_route_mapping(link_path, route_path) # 也为符号链接路径添加路由映射 added_targets.add(target_path) link_idx += 1 diff --git a/routes/api_routes.py b/routes/api_routes.py index 40d4742b..dae82c9d 100644 --- a/routes/api_routes.py +++ b/routes/api_routes.py @@ -217,12 +217,12 @@ class ApiRoutes: main_file = patterns[0] main_path = os.path.join(target_dir, main_file) - if not os.path.exists(main_path): - raise web.HTTPNotFound(text=f"Model file not found: {main_file}") - - # Delete main file first - os.remove(main_path) - deleted.append(main_file) + if os.path.exists(main_path): + # Delete main file first + os.remove(main_path) + deleted.append(main_file) + else: + logger.warning(f"Model file not found: {main_file}") # Delete optional files for pattern in patterns[1:]: diff --git a/services/file_monitor.py b/services/file_monitor.py index 6fd7027f..9988520e 100644 --- a/services/file_monitor.py +++ b/services/file_monitor.py @@ -27,6 +27,8 @@ class LoraFileHandler(FileSystemEventHandler): def _should_ignore(self, path: str) -> bool: """Check if path should be ignored""" + logger.info(f"Checking ignore for {path}") + logger.info(f"Current ignore paths: {self._ignore_paths}") real_path = os.path.realpath(path) # Resolve any symbolic links return real_path.replace(os.sep, '/') in self._ignore_paths @@ -66,7 +68,7 @@ class LoraFileHandler(FileSystemEventHandler): logger.info(f"LoRA file deleted: {event.src_path}") self._schedule_update('remove', event.src_path) - def _schedule_update(self, action: str, file_path: str): + def _schedule_update(self, action: str, file_path: str): #file_path is a real path """Schedule a cache update""" with self.lock: # 使用 config 中的方法映射路径 @@ -112,6 +114,7 @@ class LoraFileHandler(FileSystemEventHandler): elif action == 'remove': # 从缓存中移除 + logger.info(f"Removing {file_path} from cache") cache.raw_data = [ item for item in cache.raw_data if item['file_path'] != file_path @@ -145,7 +148,7 @@ class LoraFileMonitor: # 使用已存在的路径映射 self.monitor_paths = set() for root in roots: - self.monitor_paths.add(os.path.realpath(root)) + self.monitor_paths.add(os.path.realpath(root).replace(os.sep, '/')) # 添加所有已映射的目标路径 for target_path in config._path_mappings.keys():