132 lines
4.7 KiB
Python
Executable File
132 lines
4.7 KiB
Python
Executable File
"""
|
|
Image compression configuration loader
|
|
Loads compression levels from YAML configuration file
|
|
"""
|
|
import yaml
|
|
from pathlib import Path
|
|
from typing import Dict, Any, Optional
|
|
|
|
|
|
class ImageCompressionConfig:
|
|
"""Manages image compression configuration from YAML file"""
|
|
|
|
def __init__(self, config_path: Optional[str] = None):
|
|
"""
|
|
Initialize configuration loader
|
|
|
|
Args:
|
|
config_path: Path to YAML config file (optional)
|
|
"""
|
|
if config_path is None:
|
|
# Default path: config/image_compression.yaml (from project root)
|
|
# Path from backend/app/utils/ -> up 3 levels to project root
|
|
config_path = Path(__file__).parent.parent.parent.parent / "config" / "image_compression.yaml"
|
|
|
|
self.config_path = Path(config_path)
|
|
self.config = self._load_config()
|
|
|
|
def _load_config(self) -> Dict[str, Any]:
|
|
"""Load configuration from YAML file"""
|
|
if not self.config_path.exists():
|
|
print(f"Warning: Image compression config not found at {self.config_path}")
|
|
print("Using default configuration")
|
|
return self._get_default_config()
|
|
|
|
try:
|
|
with open(self.config_path, 'r', encoding='utf-8') as f:
|
|
config = yaml.safe_load(f)
|
|
return config
|
|
except Exception as e:
|
|
print(f"Error loading image compression config: {e}")
|
|
print("Using default configuration")
|
|
return self._get_default_config()
|
|
|
|
def _get_default_config(self) -> Dict[str, Any]:
|
|
"""Get default configuration if YAML file not found"""
|
|
return {
|
|
"default_level": "medium",
|
|
"levels": {
|
|
"medium": {
|
|
"enabled": True,
|
|
"quality": 85,
|
|
"max_width": 1920,
|
|
"max_height": 1080,
|
|
"thumbnail_size": 48,
|
|
"thumbnail_quality": 75,
|
|
"thumbnail_format": "webp",
|
|
"description": "Qualité moyenne - Usage général"
|
|
}
|
|
},
|
|
"supported_formats": ["jpg", "jpeg", "png", "webp", "gif", "bmp"],
|
|
"max_upload_size": 52428800,
|
|
"auto_convert_to_webp": True,
|
|
"keep_original": False,
|
|
"compressed_prefix": "compressed_",
|
|
"thumbnail_prefix": "thumb_"
|
|
}
|
|
|
|
def get_level(self, level_name: Optional[str] = None) -> Dict[str, Any]:
|
|
"""
|
|
Get compression settings for a specific level
|
|
|
|
Args:
|
|
level_name: Name of compression level (high, medium, low, minimal)
|
|
If None, uses default level
|
|
|
|
Returns:
|
|
Dictionary with compression settings
|
|
"""
|
|
if level_name is None:
|
|
level_name = self.config.get("default_level", "medium")
|
|
|
|
levels = self.config.get("levels", {})
|
|
if level_name not in levels:
|
|
print(f"Warning: Level '{level_name}' not found, using default")
|
|
level_name = self.config.get("default_level", "medium")
|
|
|
|
return levels.get(level_name, levels.get("medium", {}))
|
|
|
|
def get_all_levels(self) -> Dict[str, Dict[str, Any]]:
|
|
"""Get all available compression levels"""
|
|
return self.config.get("levels", {})
|
|
|
|
def get_default_level_name(self) -> str:
|
|
"""Get name of default compression level"""
|
|
return self.config.get("default_level", "medium")
|
|
|
|
def is_format_supported(self, format: str) -> bool:
|
|
"""Check if image format is supported for input"""
|
|
supported = self.config.get("supported_input_formats", ["jpg", "jpeg", "png", "webp"])
|
|
return format.lower() in supported
|
|
|
|
def get_output_format(self) -> str:
|
|
"""Get output format for resized images"""
|
|
return self.config.get("output_format", "png")
|
|
|
|
def get_folders(self) -> Dict[str, str]:
|
|
"""Get folder structure configuration"""
|
|
return self.config.get("folders", {
|
|
"original": "original",
|
|
"thumbnail": "thumbnail"
|
|
})
|
|
|
|
def get_max_upload_size(self) -> int:
|
|
"""Get maximum upload size in bytes"""
|
|
return self.config.get("max_upload_size", 52428800)
|
|
|
|
def should_keep_original(self) -> bool:
|
|
"""Check if original file should be kept"""
|
|
return self.config.get("keep_original", True)
|
|
|
|
def get_compressed_prefix(self) -> str:
|
|
"""Get prefix for compressed files"""
|
|
return self.config.get("compressed_prefix", "")
|
|
|
|
def get_thumbnail_prefix(self) -> str:
|
|
"""Get prefix for thumbnail files"""
|
|
return self.config.get("thumbnail_prefix", "thumb_")
|
|
|
|
|
|
# Global instance
|
|
image_compression_config = ImageCompressionConfig()
|