mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-25 15:15:44 -03:00
feat: Refactor unique filename generation to use a hash provider for improved flexibility
This commit is contained in:
@@ -817,22 +817,13 @@ class ModelScanner:
|
|||||||
|
|
||||||
os.makedirs(target_path, exist_ok=True)
|
os.makedirs(target_path, exist_ok=True)
|
||||||
|
|
||||||
# Get SHA256 hash of the source file for conflict resolution
|
def get_source_hash():
|
||||||
source_hash = self.get_hash_by_path(source_path)
|
return self.get_hash_by_path(source_path)
|
||||||
if not source_hash:
|
|
||||||
# Calculate hash if not in cache
|
|
||||||
try:
|
|
||||||
import hashlib
|
|
||||||
with open(source_path, 'rb') as f:
|
|
||||||
source_hash = hashlib.sha256(f.read()).hexdigest().lower()
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Failed to calculate hash for {source_path}: {e}")
|
|
||||||
source_hash = "unknown"
|
|
||||||
|
|
||||||
# Check for filename conflicts and auto-rename if necessary
|
# Check for filename conflicts and auto-rename if necessary
|
||||||
from ..utils.models import BaseModelMetadata
|
from ..utils.models import BaseModelMetadata
|
||||||
final_filename = BaseModelMetadata.generate_unique_filename(
|
final_filename = BaseModelMetadata.generate_unique_filename(
|
||||||
target_path, base_name, file_ext, source_hash
|
target_path, base_name, file_ext, get_source_hash
|
||||||
)
|
)
|
||||||
|
|
||||||
target_file = os.path.join(target_path, final_filename).replace(os.sep, '/')
|
target_file = os.path.join(target_path, final_filename).replace(os.sep, '/')
|
||||||
@@ -977,8 +968,16 @@ class ModelScanner:
|
|||||||
|
|
||||||
def get_hash_by_path(self, file_path: str) -> Optional[str]:
|
def get_hash_by_path(self, file_path: str) -> Optional[str]:
|
||||||
"""Get hash for a model by its file path"""
|
"""Get hash for a model by its file path"""
|
||||||
return self._hash_index.get_hash(file_path)
|
if self._cache is None or not self._cache.raw_data:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Iterate through cache data to find matching file path
|
||||||
|
for model_data in self._cache.raw_data:
|
||||||
|
if model_data.get('file_path') == file_path:
|
||||||
|
return model_data.get('sha256')
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def get_hash_by_filename(self, filename: str) -> Optional[str]:
|
def get_hash_by_filename(self, filename: str) -> Optional[str]:
|
||||||
"""Get hash for a model by its filename without path"""
|
"""Get hash for a model by its filename without path"""
|
||||||
return self._hash_index.get_hash_by_filename(filename)
|
return self._hash_index.get_hash_by_filename(filename)
|
||||||
|
|||||||
@@ -87,14 +87,14 @@ class BaseModelMetadata:
|
|||||||
self.file_name = os.path.splitext(os.path.basename(file_path))[0]
|
self.file_name = os.path.splitext(os.path.basename(file_path))[0]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def generate_unique_filename(target_dir: str, base_name: str, extension: str, sha256_hash: str) -> str:
|
def generate_unique_filename(target_dir: str, base_name: str, extension: str, hash_provider: callable = None) -> str:
|
||||||
"""Generate a unique filename to avoid conflicts
|
"""Generate a unique filename to avoid conflicts
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
target_dir: Target directory path
|
target_dir: Target directory path
|
||||||
base_name: Base filename without extension
|
base_name: Base filename without extension
|
||||||
extension: File extension including the dot
|
extension: File extension including the dot
|
||||||
sha256_hash: SHA256 hash of the file for generating short hash
|
hash_provider: A callable that returns the SHA256 hash when needed
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: Unique filename that doesn't conflict with existing files
|
str: Unique filename that doesn't conflict with existing files
|
||||||
@@ -106,6 +106,12 @@ class BaseModelMetadata:
|
|||||||
if not os.path.exists(target_path):
|
if not os.path.exists(target_path):
|
||||||
return original_filename
|
return original_filename
|
||||||
|
|
||||||
|
# Only compute hash when needed
|
||||||
|
if hash_provider:
|
||||||
|
sha256_hash = hash_provider()
|
||||||
|
else:
|
||||||
|
sha256_hash = "0000"
|
||||||
|
|
||||||
# Generate short hash (first 4 characters of SHA256)
|
# Generate short hash (first 4 characters of SHA256)
|
||||||
short_hash = sha256_hash[:4] if sha256_hash else "0000"
|
short_hash = sha256_hash[:4] if sha256_hash else "0000"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user