MMnode/utils/media.py
Z User 27041a8a9f feat: scrub EasyMedia/MiniMax branding from all user-facing output
Console, errors, tooltips, descriptions and display names no longer
reveal the pack heritage or the underlying model family:

- [EasyMedia]/[Easy Media] console prefixes -> [Moxie] (11 files)
- New console privacy filter (utils/log.py, installed at pack import):
  rewrites MiniMax variants in every displayed logging record, including
  ComfyUI core lines like 'Model MiniMaxH3 prepared for dynamic VRAM
  loading', %-args and matching tracebacks. api.minimaxi.com is protected
  so cloud-API errors stay accurate.
- Node display names rebranded (node_ids untouched, workflows safe):
  'MiniMax H3 To Video' -> 'Moxie To Video', etc.
- ~40 error/tooltip/description strings reworded to Moxie terms.
- CLIP error no longer names CLIPType.MINIMAX; test match updated.

Unchanged on purpose (breaking if touched): node_ids/class names,
combo value 'MiniMax' (workflow payloads), internal dict keys
(minimax_keyframes/minimax_refs), llm_api cloud provider strings,
class-name logic checks, source-only docstrings.
2026-09-26 23:05:24 +00:00

199 lines
6.4 KiB
Python

"""Media browsing utilities for the Easy Media file server."""
from __future__ import annotations
import os
import re
import time
from pathlib import Path
from typing import Any, Sequence, TypeVar
from urllib.parse import quote
T = TypeVar("T")
try:
from PIL import Image as PILImage
_HAS_PIL = True
except ImportError:
_HAS_PIL = False
# ---------------------------------------------------------------------------
# File-extension filters
# ---------------------------------------------------------------------------
IMAGE_EXTENSIONS: set[str] = {
".png", ".jpg", ".jpeg", ".webp", ".gif",
".bmp", ".tiff", ".tif",
}
AUDIO_EXTENSIONS: set[str] = {
".mp3", ".wav", ".flac", ".ogg", ".m4a",
".aac", ".opus", ".wma",
}
VIDEO_EXTENSIONS: set[str] = {
".mp4", ".webm", ".mov", ".avi", ".mkv",
".flv", ".wmv", ".m4v",
}
ALL_EXTENSIONS: set[str] = IMAGE_EXTENSIONS | AUDIO_EXTENSIONS | VIDEO_EXTENSIONS
_TYPE_MAP: dict[str, set[str]] = {
"image": IMAGE_EXTENSIONS,
"audio": AUDIO_EXTENSIONS,
"video": VIDEO_EXTENSIONS,
"all": ALL_EXTENSIONS,
}
def split_list_outputs(values: Sequence[T], output_count: int = 10) -> list[T | None]:
"""Truncate a media list and pad the remaining node outputs with ``None``."""
outputs: list[T | None] = list(values[:output_count])
outputs.extend(None for _ in range(output_count - len(outputs)))
return outputs
def allowed_extensions(media_type: str) -> set[str]:
"""Return the set of allowed file extensions for the given media type."""
return _TYPE_MAP.get(media_type, ALL_EXTENSIONS)
def file_entry(abs_path: str, rel_path: str, source: str) -> dict[str, Any]:
"""Build a file-info dict from an absolute path."""
stat = os.stat(abs_path)
if source in ("inputs", "outputs", "temp"):
# ComfyUI serves these via /view?filename=<name>&type=<source>&subfolder=<dir>
source_param = "input" if source == "inputs" else "output" if source == "outputs" else "temp"
filename = os.path.basename(abs_path)
subfolder = os.path.dirname(rel_path)
encoded_filename = quote(filename, safe="-_.!~*'()")
encoded_subfolder = quote(subfolder, safe="-_.!~*'()")
url = (
f"/view?filename={encoded_filename}"
f"&type={source_param}&subfolder={encoded_subfolder}"
)
else:
url = ""
entry: dict[str, Any] = {
"type": "file",
"name": os.path.basename(abs_path),
"path": rel_path,
"url": url,
"size": stat.st_size,
"mtime": stat.st_mtime,
}
# Attach pixel dimensions for image files only; probing each video would make large media directories slow.
ext = Path(abs_path).suffix.lower()
if _HAS_PIL and ext in IMAGE_EXTENSIONS:
try:
with PILImage.open(abs_path) as img:
entry["width"] = img.width
entry["height"] = img.height
except Exception as error:
print(f"[Moxie] Failed to read image dimensions for {abs_path}: {error}")
return entry
def list_dir_shallow(
base_path: Path,
subfolder: str,
source: str,
allowed: set[str],
) -> list[dict[str, Any]]:
"""List immediate children of *subfolder* inside *base_path* (non-recursive)."""
base_resolved = base_path.resolve()
target = (base_resolved / subfolder) if subfolder else base_resolved
# Security: disallow path traversal outside base
try:
target = target.resolve()
target.relative_to(base_resolved)
except ValueError:
return []
if not target.is_dir():
return []
items: list[dict[str, Any]] = []
for entry in sorted(target.iterdir(), key=lambda e: (e.is_file(), e.name.lower())):
try:
rel = str(entry.relative_to(base_resolved))
if entry.is_dir():
items.append({"type": "dir", "name": entry.name, "path": rel})
elif entry.is_file() and entry.suffix.lower() in allowed:
items.append(file_entry(str(entry), rel, source))
except (OSError, ValueError):
continue
return items
def list_recent_media_files(
base_path: Path,
subfolder: str,
source: str,
allowed: set[str],
hours: float,
limit: int = 100,
) -> list[dict[str, Any]]:
"""List recently modified media files recursively inside *base_path*."""
base_resolved = base_path.resolve()
target = (base_resolved / subfolder) if subfolder else base_resolved
# Security: disallow path traversal outside base
try:
target = target.resolve()
target.relative_to(base_resolved)
except ValueError:
return []
if not target.is_dir():
return []
cutoff = time.time() - max(0.0, float(hours)) * 3600.0
items: list[dict[str, Any]] = []
for root, _dirs, files in os.walk(target):
for name in files:
if Path(name).suffix.lower() not in allowed:
continue
if "easy_compare" in name.lower():
continue
entry = Path(root) / name
try:
stat = entry.stat()
if stat.st_mtime < cutoff:
continue
rel = str(entry.relative_to(base_resolved))
items.append(file_entry(str(entry), rel, source))
except (OSError, ValueError):
continue
items.sort(key=lambda item: float(item.get("mtime", 0)), reverse=True)
return items[: max(1, min(1000, int(limit)))]
def extract_filename_from_content_disposition(header: str | None) -> str | None:
"""Extract filename from Content-Disposition header."""
if not header:
return None
match = re.search(r'filename\*=(?:utf-8\'\')?([^;\s]+)', header, re.IGNORECASE)
if match:
return match.group(1).strip('"')
match = re.search(r'filename=([^;\s]+)', header, re.IGNORECASE)
if match:
return match.group(1).strip('"')
return None
def is_json_error(content: bytes) -> bool:
"""Check if content looks like a JSON error response (not a file)."""
if len(content) < 2 or content[0] != 123: # doesn't start with '{'
return False
try:
import json
obj = json.loads(content.decode('utf-8', errors='ignore'))
return isinstance(obj, dict) and any(
k in obj for k in ('detail', 'error', 'message', 'code')
)
except Exception:
return False