Spaces:
Build error
Build error
File size: 2,614 Bytes
111538d |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
"""Audio Upload Data Transfer Object"""
from dataclasses import dataclass
from typing import Optional
import mimetypes
import os
@dataclass
class AudioUploadDto:
"""DTO for file upload data
Handles audio file upload information including filename,
content, and content type validation.
"""
filename: str
content: bytes
content_type: str
size: Optional[int] = None
def __post_init__(self):
"""Validate the DTO after initialization"""
self._validate()
if self.size is None:
self.size = len(self.content)
def _validate(self):
"""Validate audio upload data"""
if not self.filename:
raise ValueError("Filename cannot be empty")
if not self.content:
raise ValueError("Audio content cannot be empty")
if not self.content_type:
raise ValueError("Content type cannot be empty")
# Validate file extension
_, ext = os.path.splitext(self.filename.lower())
supported_extensions = ['.wav', '.mp3', '.m4a', '.flac', '.ogg']
if ext not in supported_extensions:
raise ValueError(f"Unsupported file extension: {ext}. Supported: {supported_extensions}")
# Validate content type
expected_content_type = mimetypes.guess_type(self.filename)[0]
if expected_content_type and not self.content_type.startswith('audio/'):
raise ValueError(f"Invalid content type: {self.content_type}. Expected audio/* type")
# Validate file size (max 100MB)
max_size = 100 * 1024 * 1024 # 100MB
if len(self.content) > max_size:
raise ValueError(f"File too large: {len(self.content)} bytes. Maximum: {max_size} bytes")
# Validate minimum file size (at least 1KB)
min_size = 1024 # 1KB
if len(self.content) < min_size:
raise ValueError(f"File too small: {len(self.content)} bytes. Minimum: {min_size} bytes")
@property
def file_extension(self) -> str:
"""Get the file extension"""
return os.path.splitext(self.filename.lower())[1]
@property
def base_filename(self) -> str:
"""Get filename without extension"""
return os.path.splitext(self.filename)[0]
def to_dict(self) -> dict:
"""Convert to dictionary representation"""
return {
'filename': self.filename,
'content_type': self.content_type,
'size': self.size,
'file_extension': self.file_extension
} |