mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-05-06 16:36:45 -03:00
feat(example-images): add remote open mode support
This commit is contained in:
@@ -81,6 +81,9 @@ DEFAULT_SETTINGS: Dict[str, Any] = {
|
||||
"folder_paths": {},
|
||||
"extra_folder_paths": {},
|
||||
"example_images_path": "",
|
||||
"example_images_open_mode": "system",
|
||||
"example_images_local_root": "",
|
||||
"example_images_open_uri_template": "",
|
||||
"optimize_example_images": True,
|
||||
"auto_download_example_images": False,
|
||||
"blur_mature_content": True,
|
||||
|
||||
@@ -1,17 +1,81 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from urllib.parse import quote
|
||||
|
||||
from aiohttp import web
|
||||
from ..services.settings_manager import get_settings_manager
|
||||
from ..utils.example_images_paths import (
|
||||
get_model_folder,
|
||||
get_model_relative_path,
|
||||
)
|
||||
from ..utils.constants import SUPPORTED_MEDIA_EXTENSIONS
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
_WINDOWS_DRIVE_PATTERN = re.compile(r"^[A-Za-z]:/")
|
||||
|
||||
|
||||
def _is_within_root(path: str, root: str) -> bool:
|
||||
try:
|
||||
return os.path.commonpath([os.path.abspath(path), os.path.abspath(root)]) == os.path.abspath(root)
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
|
||||
def _join_local_example_path(local_root: str, relative_path: str) -> str:
|
||||
separator = "\\" if "\\" in local_root and "/" not in local_root else "/"
|
||||
normalized_root = local_root.rstrip("\\/")
|
||||
normalized_relative = relative_path.replace("/", separator)
|
||||
if not normalized_root:
|
||||
return normalized_relative
|
||||
return f"{normalized_root}{separator}{normalized_relative}"
|
||||
|
||||
|
||||
def _build_file_uri(path: str) -> str:
|
||||
normalized = path.replace("\\", "/")
|
||||
if _WINDOWS_DRIVE_PATTERN.match(normalized):
|
||||
return f"file:///{quote(normalized, safe='/:')}"
|
||||
if normalized.startswith("/"):
|
||||
return f"file://{quote(normalized, safe='/:')}"
|
||||
return f"file:///{quote(normalized.lstrip('/'), safe='/:')}"
|
||||
|
||||
|
||||
def _render_open_uri_template(template: str, local_path: str, relative_path: str) -> str:
|
||||
file_uri = _build_file_uri(local_path)
|
||||
replacements = {
|
||||
"{{local_path}}": local_path,
|
||||
"{{encoded_local_path}}": quote(local_path, safe=""),
|
||||
"{{relative_path}}": relative_path,
|
||||
"{{encoded_relative_path}}": quote(relative_path, safe=""),
|
||||
"{{file_uri}}": file_uri,
|
||||
"{{encoded_file_uri}}": quote(file_uri, safe=""),
|
||||
}
|
||||
|
||||
rendered = template
|
||||
for placeholder, value in replacements.items():
|
||||
rendered = rendered.replace(placeholder, value)
|
||||
return rendered
|
||||
|
||||
|
||||
def _open_system_folder(model_folder: str) -> dict[str, object]:
|
||||
if os.name == "nt": # Windows
|
||||
os.startfile(model_folder)
|
||||
elif os.name == "posix": # macOS and Linux
|
||||
if sys.platform == "darwin": # macOS
|
||||
subprocess.Popen(["open", model_folder])
|
||||
else: # Linux
|
||||
subprocess.Popen(["xdg-open", model_folder])
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"Opened example images folder for {model_folder}",
|
||||
"path": model_folder,
|
||||
}
|
||||
|
||||
|
||||
class ExampleImagesFileManager:
|
||||
"""Manages access and operations for example image files"""
|
||||
|
||||
@@ -54,7 +118,7 @@ class ExampleImagesFileManager:
|
||||
}, status=500)
|
||||
|
||||
# Path validation: ensure model_folder is under example_images_path
|
||||
if not model_folder.startswith(os.path.abspath(example_images_path)):
|
||||
if not _is_within_root(model_folder, example_images_path):
|
||||
return web.json_response({
|
||||
'success': False,
|
||||
'error': 'Invalid model folder path'
|
||||
@@ -66,20 +130,40 @@ class ExampleImagesFileManager:
|
||||
'success': False,
|
||||
'error': 'No example images found for this model. Download example images first.'
|
||||
}, status=404)
|
||||
|
||||
# Open folder in file explorer
|
||||
if os.name == 'nt': # Windows
|
||||
os.startfile(model_folder)
|
||||
elif os.name == 'posix': # macOS and Linux
|
||||
if sys.platform == 'darwin': # macOS
|
||||
subprocess.Popen(['open', model_folder])
|
||||
else: # Linux
|
||||
subprocess.Popen(['xdg-open', model_folder])
|
||||
|
||||
return web.json_response({
|
||||
'success': True,
|
||||
'message': f'Opened example images folder for model {model_hash}'
|
||||
})
|
||||
|
||||
root_path = os.path.abspath(example_images_path)
|
||||
relative_path = os.path.relpath(model_folder, root_path).replace("\\", "/")
|
||||
open_mode = settings_manager.get("example_images_open_mode") or "system"
|
||||
|
||||
if open_mode == "clipboard":
|
||||
local_root = settings_manager.get("example_images_local_root") or root_path
|
||||
local_path = _join_local_example_path(local_root, relative_path)
|
||||
return web.json_response({
|
||||
'success': True,
|
||||
'mode': 'clipboard',
|
||||
'path': local_path,
|
||||
'relative_path': relative_path,
|
||||
})
|
||||
|
||||
if open_mode == "uri_template":
|
||||
local_root = settings_manager.get("example_images_local_root") or root_path
|
||||
uri_template = settings_manager.get("example_images_open_uri_template") or ""
|
||||
if not uri_template.strip():
|
||||
return web.json_response({
|
||||
'success': False,
|
||||
'error': 'No example image open URI template configured.'
|
||||
}, status=400)
|
||||
|
||||
local_path = _join_local_example_path(local_root, relative_path)
|
||||
return web.json_response({
|
||||
'success': True,
|
||||
'mode': 'uri',
|
||||
'path': local_path,
|
||||
'relative_path': relative_path,
|
||||
'uri': _render_open_uri_template(uri_template, local_path, relative_path),
|
||||
})
|
||||
|
||||
return web.json_response(_open_system_folder(model_folder))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to open example images folder: {e}", exc_info=True)
|
||||
@@ -143,7 +227,7 @@ class ExampleImagesFileManager:
|
||||
file_ext = os.path.splitext(file)[1].lower()
|
||||
if (file_ext in SUPPORTED_MEDIA_EXTENSIONS['images'] or
|
||||
file_ext in SUPPORTED_MEDIA_EXTENSIONS['videos']):
|
||||
relative_path = get_model_relative_path(model_hash)
|
||||
relative_path = os.path.relpath(model_folder, os.path.abspath(example_images_path)).replace("\\", "/")
|
||||
files.append({
|
||||
'name': file,
|
||||
'path': f'/example_images_static/{relative_path}/{file}',
|
||||
@@ -227,4 +311,4 @@ class ExampleImagesFileManager:
|
||||
return web.json_response({
|
||||
'has_images': False,
|
||||
'error': str(e)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user