diff --git a/py/services/model_scanner.py b/py/services/model_scanner.py index 523d18c1..06b0f2b3 100644 --- a/py/services/model_scanner.py +++ b/py/services/model_scanner.py @@ -817,22 +817,13 @@ class ModelScanner: os.makedirs(target_path, exist_ok=True) - # Get SHA256 hash of the source file for conflict resolution - source_hash = 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" + def get_source_hash(): + return self.get_hash_by_path(source_path) # Check for filename conflicts and auto-rename if necessary from ..utils.models import BaseModelMetadata 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, '/') @@ -977,8 +968,16 @@ class ModelScanner: def get_hash_by_path(self, file_path: str) -> Optional[str]: """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]: """Get hash for a model by its filename without path""" return self._hash_index.get_hash_by_filename(filename) diff --git a/py/utils/models.py b/py/utils/models.py index 6b13f013..9e1dc737 100644 --- a/py/utils/models.py +++ b/py/utils/models.py @@ -87,14 +87,14 @@ class BaseModelMetadata: self.file_name = os.path.splitext(os.path.basename(file_path))[0] @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 Args: target_dir: Target directory path base_name: Base filename without extension 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: str: Unique filename that doesn't conflict with existing files @@ -106,6 +106,12 @@ class BaseModelMetadata: if not os.path.exists(target_path): 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) short_hash = sha256_hash[:4] if sha256_hash else "0000"