Fix symbolic links

This commit is contained in:
Will Miao
2025-02-23 14:03:55 +08:00
parent 97d14dea39
commit 3871557868
4 changed files with 22 additions and 15 deletions

View File

@@ -82,7 +82,6 @@ class Config:
if normalized_path.startswith(target_path): if normalized_path.startswith(target_path):
# 如果路径以目标路径开头,则替换为链接路径 # 如果路径以目标路径开头,则替换为链接路径
mapped_path = normalized_path.replace(target_path, link_path, 1) mapped_path = normalized_path.replace(target_path, link_path, 1)
logger.info(f"Mapped path {normalized_path} to {mapped_path}")
return mapped_path return mapped_path
return path return path
@@ -100,7 +99,7 @@ class Config:
for path in paths: for path in paths:
real_path = os.path.normpath(os.path.realpath(path)).replace(os.sep, '/') real_path = os.path.normpath(os.path.realpath(path)).replace(os.sep, '/')
if real_path != path: if real_path != path:
self.add_path_mapping(real_path, path) self.add_path_mapping(path, real_path)
return paths return paths

View File

@@ -25,13 +25,19 @@ class LoraManager:
for idx, root in enumerate(config.loras_roots, start=1): for idx, root in enumerate(config.loras_roots, start=1):
preview_path = f'/loras_static/root{idx}/preview' 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) app.router.add_static(preview_path, real_root)
logger.info(f"Added static route {preview_path} -> {root}") logger.info(f"Added static route {preview_path} -> {real_root}")
# 记录路由映射 # 记录路由映射
config.add_route_mapping(root, preview_path) config.add_route_mapping(real_root, preview_path)
added_targets.add(root) added_targets.add(real_root)
# 为符号链接的目标路径添加额外的静态路由 # 为符号链接的目标路径添加额外的静态路由
link_idx = 1 link_idx = 1
@@ -42,7 +48,6 @@ class LoraManager:
app.router.add_static(route_path, target_path) app.router.add_static(route_path, target_path)
logger.info(f"Added static route for link target {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(target_path, route_path)
config.add_route_mapping(link_path, route_path) # 也为符号链接路径添加路由映射
added_targets.add(target_path) added_targets.add(target_path)
link_idx += 1 link_idx += 1

View File

@@ -217,12 +217,12 @@ class ApiRoutes:
main_file = patterns[0] main_file = patterns[0]
main_path = os.path.join(target_dir, main_file) main_path = os.path.join(target_dir, main_file)
if not os.path.exists(main_path): if os.path.exists(main_path):
raise web.HTTPNotFound(text=f"Model file not found: {main_file}") # Delete main file first
os.remove(main_path)
# Delete main file first deleted.append(main_file)
os.remove(main_path) else:
deleted.append(main_file) logger.warning(f"Model file not found: {main_file}")
# Delete optional files # Delete optional files
for pattern in patterns[1:]: for pattern in patterns[1:]:

View File

@@ -27,6 +27,8 @@ class LoraFileHandler(FileSystemEventHandler):
def _should_ignore(self, path: str) -> bool: def _should_ignore(self, path: str) -> bool:
"""Check if path should be ignored""" """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 real_path = os.path.realpath(path) # Resolve any symbolic links
return real_path.replace(os.sep, '/') in self._ignore_paths 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}") logger.info(f"LoRA file deleted: {event.src_path}")
self._schedule_update('remove', 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""" """Schedule a cache update"""
with self.lock: with self.lock:
# 使用 config 中的方法映射路径 # 使用 config 中的方法映射路径
@@ -112,6 +114,7 @@ class LoraFileHandler(FileSystemEventHandler):
elif action == 'remove': elif action == 'remove':
# 从缓存中移除 # 从缓存中移除
logger.info(f"Removing {file_path} from cache")
cache.raw_data = [ cache.raw_data = [
item for item in cache.raw_data item for item in cache.raw_data
if item['file_path'] != file_path if item['file_path'] != file_path
@@ -145,7 +148,7 @@ class LoraFileMonitor:
# 使用已存在的路径映射 # 使用已存在的路径映射
self.monitor_paths = set() self.monitor_paths = set()
for root in roots: 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(): for target_path in config._path_mappings.keys():