Refactor metadata handling by introducing MetadataManager for centralized operations and improving error handling

This commit is contained in:
Will Miao
2025-06-17 21:01:48 +08:00
parent 71df8ba3e2
commit afa5a42f5a
9 changed files with 349 additions and 77 deletions

View File

@@ -13,6 +13,7 @@ from ..services.settings_manager import settings
from ..services.service_registry import ServiceRegistry
from ..utils.constants import SUPPORTED_MEDIA_EXTENSIONS
from ..utils.routes_common import ModelRouteUtils
from ..utils.metadata_manager import MetadataManager
logger = logging.getLogger(__name__)
@@ -446,8 +447,7 @@ class ExampleImagesRoutes:
model_copy.pop('folder', None)
# Write the metadata to file without the folder field
with open(metadata_path, 'w', encoding='utf-8') as f:
json.dump(model_copy, f, indent=2, ensure_ascii=False)
await MetadataManager.save_metadata(file_path, model_copy)
logger.info(f"Saved metadata to {metadata_path}")
except Exception as e:
logger.error(f"Failed to save metadata to {metadata_path}: {str(e)}")
@@ -1231,8 +1231,7 @@ class ExampleImagesRoutes:
model_copy.pop('folder', None)
# Write the metadata to file
with open(metadata_path, 'w', encoding='utf-8') as f:
json.dump(model_copy, f, indent=2, ensure_ascii=False)
await MetadataManager.save_metadata(file_path, model_copy)
logger.info(f"Saved metadata to {metadata_path}")
except Exception as e:
logger.error(f"Failed to save metadata to {metadata_path}: {str(e)}")