| """File size value object.""" | |
| from dataclasses import dataclass | |
| from ..exceptions.domain_exceptions import ValidationError | |
| class FileSize: | |
| """Value object representing file size.""" | |
| bytes: int | |
| def __post_init__(self): | |
| if self.bytes < 0: | |
| raise ValidationError("File size cannot be negative") | |
| def megabytes(self) -> float: | |
| """Get size in megabytes.""" | |
| return self.bytes / (1024 * 1024) | |
| def gigabytes(self) -> float: | |
| """Get size in gigabytes.""" | |
| return self.bytes / (1024 * 1024 * 1024) | |
| def is_within_limit(self, max_mb: float) -> bool: | |
| """Check if file size is within specified limit in MB.""" | |
| return self.megabytes <= max_mb | |
| def __str__(self) -> str: | |
| """Human-readable string representation.""" | |
| if self.gigabytes >= 1: | |
| return f"{self.gigabytes:.2f} GB" | |
| elif self.megabytes >= 1: | |
| return f"{self.megabytes:.1f} MB" | |
| else: | |
| return f"{self.bytes} bytes" | |
| def __lt__(self, other: 'FileSize') -> bool: | |
| return self.bytes < other.bytes |