Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 140 additions & 1 deletion app/model_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import json
import time
import logging
import asyncio
import requests
from threading import Lock
from tqdm.auto import tqdm
from urllib.parse import unquote, urlparse
from typing import Callable
import folder_paths
import glob
import comfy.utils
Expand All @@ -13,8 +19,11 @@


class ModelFileManager:
def __init__(self) -> None:
def __init__(self, is_download_model_enabled: Callable[[], bool] = lambda: True) -> None:
self.cache: dict[str, tuple[list[dict], dict[str, float], float]] = {}
self.is_download_model_enabled = is_download_model_enabled
self._download_progress: dict[str, dict] = {}
self._download_lock = Lock()

def get_cache(self, key: str, default=None) -> tuple[list[dict], dict[str, float], float] | None:
return self.cache.get(key, default)
Expand All @@ -25,6 +34,39 @@ def set_cache(self, key: str, value: tuple[list[dict], dict[str, float], float])
def clear_cache(self):
self.cache.clear()


def _download_progress_key(self, save_dir: str, filename: str) -> str:
return f"{save_dir}/{filename}"

def _set_download_progress(self, key: str, **fields) -> None:
with self._download_lock:
entry = dict(self._download_progress.get(key, {}))
entry.update(fields)
self._download_progress[key] = entry

def _download_file_sync(self, url: str, headers: dict, tmp_path: str, save_path: str, key: str) -> None:
with requests.get(url, headers=headers, stream=True, timeout=(30, 3600)) as r:
r.raise_for_status()
total_size = int(r.headers.get("content-length", 0) or 0)
os.makedirs(os.path.dirname(save_path), exist_ok=True)
self._set_download_progress(
key,
status="running",
bytes_downloaded=0,
bytes_total=total_size,
filename=os.path.basename(save_path),
)
with open(tmp_path, "wb") as f:
downloaded = 0
for chunk in r.iter_content(chunk_size=1024 * 1024):
if not chunk:
continue
f.write(chunk)
downloaded += len(chunk)
self._set_download_progress(key, bytes_downloaded=downloaded, bytes_total=total_size or downloaded)
os.replace(tmp_path, save_path)


def add_routes(self, routes):
# NOTE: This is an experiment to replace `/models`
@routes.get("/experiment/models")
Expand All @@ -47,6 +89,103 @@ async def get_all_models(request):
files = self.get_model_file_list(folder)
return web.json_response(files)


@routes.post("/download_model")
async def post_download_model(request):
if not self.is_download_model_enabled():
logging.error("Download Model endpoint is disabled")
return web.Response(status=403)

json_data = await request.json()
url = json_data.get("url")
if not url:
return web.json_response({"error": "url required"}, status=400)

save_dir = json_data.get("save_dir")
if save_dir not in folder_paths.folder_names_and_paths:
return web.json_response({"error": "invalid save_dir"}, status=400)

default_filename = unquote(urlparse(url).path.split("/")[-1].split("?")[0])
filename = json_data.get("filename") or default_filename
if not filename or filename in (".", "..") or "/" in filename or "\\" in filename:
return web.json_response({"error": "invalid filename"}, status=400)

allowed_sources = (
"https://civitai.com/",
"https://civitai.red/",
"https://huggingface.co/",
"https://github.com/",
"http://localhost:",
)
if not any(url.startswith(src) for src in allowed_sources):
return web.json_response({"error": "url not allowed"}, status=400)

save_root = folder_paths.folder_names_and_paths[save_dir][0][0]
save_path = os.path.join(save_root, filename)
save_real = os.path.realpath(save_path)
root_real = os.path.realpath(save_root)
if not save_real.startswith(root_real + os.sep) and save_real != root_real:
return web.json_response({"error": "invalid path"}, status=400)

tmp_path = save_path + ".tmp"
token = json_data.get("token")
headers = {"Authorization": f"Bearer {token}"} if token else {}

key = self._download_progress_key(save_dir, filename)
loop = asyncio.get_running_loop()
try:
await loop.run_in_executor(
None,
self._download_file_sync,
url,
headers,
tmp_path,
save_path,
key,
)
self._set_download_progress(key, status="completed")
logging.info("Downloaded model to %s", save_path)
return web.json_response({"ok": True, "path": save_path, "save_dir": save_dir, "filename": filename})
except Exception as e:
logging.error("Failed to download model: %s", e)
self._set_download_progress(key, status="failed", error=str(e))
if os.path.exists(tmp_path):
os.remove(tmp_path)
return web.json_response({"error": str(e)}, status=500)



@routes.get("/download_model/progress")
async def get_download_model_progress(request):
save_dir = request.rel_url.query.get("save_dir")
filename = request.rel_url.query.get("filename")
if not save_dir or not filename:
return web.json_response({"error": "save_dir and filename required"}, status=400)
key = self._download_progress_key(save_dir, filename)
with self._download_lock:
entry = dict(self._download_progress.get(key, {}))
if not entry and save_dir in folder_paths.folder_names_and_paths:
root = folder_paths.folder_names_and_paths[save_dir][0][0]
tmp_path = os.path.join(root, filename + ".tmp")
final_path = os.path.join(root, filename)
if os.path.isfile(final_path):
size = os.path.getsize(final_path)
entry = {"status": "completed", "bytes_downloaded": size, "bytes_total": size}
elif os.path.isfile(tmp_path):
size = os.path.getsize(tmp_path)
entry = {"status": "running", "bytes_downloaded": size, "bytes_total": 0}
bytes_total = int(entry.get("bytes_total") or 0)
bytes_downloaded = int(entry.get("bytes_downloaded") or 0)
progress = (bytes_downloaded / bytes_total) if bytes_total > 0 else 0
return web.json_response({
"status": entry.get("status", "unknown"),
"bytes_downloaded": bytes_downloaded,
"bytes_total": bytes_total,
"progress": progress,
"error": entry.get("error"),
})


@routes.get("/experiment/models/preview/{folder}/{path_index}/{filename:.*}")
async def get_model_preview(request):
folder_name = request.match_info.get("folder", None)
Expand Down
13 changes: 12 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,18 @@ def __init__(self, loop):
PromptServer.instance = self

self.user_manager = UserManager()
self.model_file_manager = ModelFileManager()
def _is_model_download_enabled():
settings_path = os.path.join(folder_paths.get_user_directory(), "default", "comfy.settings.json")
try:
if os.path.isfile(settings_path):
with open(settings_path) as f:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Specify UTF-8 encoding when opening JSON file.

The file is opened without an explicit encoding parameter. JSON files should use UTF-8 encoding per RFC 8259, and Python's default encoding is platform-dependent (e.g., cp1252 on Windows). This could cause decoding issues if the settings file contains non-ASCII characters.

📝 Proposed fix
-                    with open(settings_path) as f:
+                    with open(settings_path, encoding='utf-8') as f:
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
with open(settings_path) as f:
with open(settings_path, encoding='utf-8') as f:
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@server.py` at line 213, The open() call that reads the settings file is
missing an explicit UTF-8 encoding parameter. Since JSON files should use UTF-8
encoding per RFC 8259 and Python's default encoding is platform-dependent, add
the encoding='utf-8' parameter to the open() function call to ensure consistent
UTF-8 decoding across all platforms and prevent issues with non-ASCII characters
in the settings file.

settings = json.load(f)
return settings.get("Comfy.ModelDownloadEnabled", True)
except Exception:
pass
return True

self.model_file_manager = ModelFileManager(is_download_model_enabled=_is_model_download_enabled)
self.custom_node_manager = CustomNodeManager()
self.subgraph_manager = SubgraphManager()
self.node_replace_manager = NodeReplaceManager()
Expand Down