# 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 from nodes import LoraLoader from server import PromptServer 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): """Sanitize a preset name to a safe filename component. Strips all characters except alphanumerics, spaces, underscores and dashes. Returns an empty string if the result is empty. """ 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): """Return an absolute path inside LORA_PRESETS_DIR for the given preset name. Returns None if the name is invalid or if the resulting path escapes LORA_PRESETS_DIR (defense in depth against path traversal). """ 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: # Different drives on Windows / incompatible paths return None return candidate def extract_metadata_from_safetensors(lora_full_path): try: with open(lora_full_path, "rb") as f: header_size = struct.unpack("