mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-25 15:15:44 -03:00
Enhance file handling in LoraScanner and file_utils: resolve symlinks and improve error logging
This commit is contained in:
@@ -294,7 +294,7 @@ class LoraScanner:
|
|||||||
async def scan_single_lora(self, file_path: str) -> Optional[Dict]:
|
async def scan_single_lora(self, file_path: str) -> Optional[Dict]:
|
||||||
"""Scan a single LoRA file and return its metadata"""
|
"""Scan a single LoRA file and return its metadata"""
|
||||||
try:
|
try:
|
||||||
if not os.path.exists(file_path):
|
if not os.path.exists(os.path.realpath(file_path)):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# 获取基本文件信息
|
# 获取基本文件信息
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
@@ -6,6 +7,8 @@ from typing import Dict, Optional
|
|||||||
from .lora_metadata import extract_lora_metadata
|
from .lora_metadata import extract_lora_metadata
|
||||||
from .models import LoraMetadata
|
from .models import LoraMetadata
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
async def calculate_sha256(file_path: str) -> str:
|
async def calculate_sha256(file_path: str) -> str:
|
||||||
"""Calculate SHA256 hash of a file"""
|
"""Calculate SHA256 hash of a file"""
|
||||||
sha256_hash = hashlib.sha256()
|
sha256_hash = hashlib.sha256()
|
||||||
@@ -37,33 +40,46 @@ def normalize_path(path: str) -> str:
|
|||||||
"""Normalize file path to use forward slashes"""
|
"""Normalize file path to use forward slashes"""
|
||||||
return path.replace(os.sep, "/") if path else path
|
return path.replace(os.sep, "/") if path else path
|
||||||
|
|
||||||
async def get_file_info(file_path: str) -> LoraMetadata:
|
async def get_file_info(file_path: str) -> Optional[LoraMetadata]:
|
||||||
"""Get basic file information as LoraMetadata object"""
|
"""Get basic file information as LoraMetadata object"""
|
||||||
|
# First check if file actually exists and resolve symlinks
|
||||||
|
try:
|
||||||
|
real_path = os.path.realpath(file_path)
|
||||||
|
if not os.path.exists(real_path):
|
||||||
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error checking file existence for {file_path}: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
base_name = os.path.splitext(os.path.basename(file_path))[0]
|
base_name = os.path.splitext(os.path.basename(file_path))[0]
|
||||||
dir_path = os.path.dirname(file_path)
|
dir_path = os.path.dirname(file_path)
|
||||||
|
|
||||||
preview_url = _find_preview_file(base_name, dir_path)
|
preview_url = _find_preview_file(base_name, dir_path)
|
||||||
|
|
||||||
metadata = LoraMetadata(
|
try:
|
||||||
file_name=base_name,
|
metadata = LoraMetadata(
|
||||||
model_name=base_name,
|
file_name=base_name,
|
||||||
file_path=normalize_path(file_path),
|
model_name=base_name,
|
||||||
size=os.path.getsize(file_path),
|
file_path=normalize_path(file_path),
|
||||||
modified=os.path.getmtime(file_path),
|
size=os.path.getsize(real_path),
|
||||||
sha256=await calculate_sha256(file_path),
|
modified=os.path.getmtime(real_path),
|
||||||
base_model="Unknown", # Will be updated later
|
sha256=await calculate_sha256(real_path),
|
||||||
usage_tips="",
|
base_model="Unknown", # Will be updated later
|
||||||
notes="",
|
usage_tips="",
|
||||||
from_civitai=True,
|
notes="",
|
||||||
preview_url=normalize_path(preview_url),
|
from_civitai=True,
|
||||||
)
|
preview_url=normalize_path(preview_url),
|
||||||
|
)
|
||||||
|
|
||||||
# create metadata file
|
# create metadata file
|
||||||
base_model_info = await extract_lora_metadata(file_path)
|
base_model_info = await extract_lora_metadata(real_path)
|
||||||
metadata.base_model = base_model_info['base_model']
|
metadata.base_model = base_model_info['base_model']
|
||||||
await save_metadata(file_path, metadata)
|
await save_metadata(file_path, metadata)
|
||||||
|
|
||||||
return metadata
|
return metadata
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error getting file info for {file_path}: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
async def save_metadata(file_path: str, metadata: LoraMetadata) -> None:
|
async def save_metadata(file_path: str, metadata: LoraMetadata) -> None:
|
||||||
"""Save metadata to .metadata.json file"""
|
"""Save metadata to .metadata.json file"""
|
||||||
|
|||||||
Reference in New Issue
Block a user