# SPDX-License-Identifier: Apache-2.0 # Copyright 2025-2026 Raykosan (RaykoStudio) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import folder_paths import json import os import struct import hashlib import re import asyncio import urllib.request import urllib.error from nodes import LoraLoader from server import PromptServer from datetime import datetime import aiohttp CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) PRESETS_DIR = os.path.join(CURRENT_DIR, "preset_models") LORA_PRESETS_DIR = os.path.join(PRESETS_DIR, "loras") RAYKO_LORA_DATA_DIR = os.path.join(CURRENT_DIR, "rayko_lora_data") os.makedirs(PRESETS_DIR, exist_ok=True) os.makedirs(LORA_PRESETS_DIR, exist_ok=True) os.makedirs(RAYKO_LORA_DATA_DIR, exist_ok=True) def clean_html(text): if not text: return "" clean = re.sub(r'<[^>]+>', ' ', str(text)) clean = re.sub(r'\s+', ' ', clean).strip() return clean def sanitize_preset_name(name): if not isinstance(name, str): return "" return "".join(c for c in name.strip() if c.isalnum() or c in " _-").strip() def _safe_preset_path(name): safe_name = sanitize_preset_name(name) if not safe_name: return None base = os.path.realpath(LORA_PRESETS_DIR) candidate = os.path.realpath(os.path.join(base, f"{safe_name}.json")) try: if os.path.commonpath([base, candidate]) != base: return None except ValueError: return None return candidate def _http_get_json(url, headers=None, timeout=30): req = urllib.request.Request(url, headers=headers or {}) opener = urllib.request.build_opener() try: with opener.open(req, timeout=timeout) as resp: raw = resp.read().decode("utf-8", errors="replace") try: return resp.status, json.loads(raw) except json.JSONDecodeError: return resp.status, None except urllib.error.HTTPError as e: return e.code, None def extract_metadata_from_safetensors(lora_full_path): try: with open(lora_full_path, "rb") as f: header_size = struct.unpack("