File size: 1,183 Bytes
92fd1a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""File size value object."""
from dataclasses import dataclass

from ..exceptions.domain_exceptions import ValidationError

@dataclass(frozen=True)
class FileSize:
    """Value object representing file size."""
    bytes: int
    
    def __post_init__(self):
        if self.bytes < 0:
            raise ValidationError("File size cannot be negative")
    
    @property
    def megabytes(self) -> float:
        """Get size in megabytes."""
        return self.bytes / (1024 * 1024)
    
    @property
    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