from __future__ import annotations import hashlib import logging from pathlib import Path from typing import Any logger = logging.getLogger(__name__) CHUNK_SIZE = 10 * 1024 * 1024 class ChunkedMediaUploader: def __init__(self, api_client: Any = None): self._api_client = api_client def needs_chunked_upload(self, file_size: int, threshold: int = CHUNK_SIZE) -> bool: return file_size >= threshold async def upload_file_chunked( self, file_path: str, chat_type: str, target_id: str ) -> dict | None: path = Path(file_path) if not path.exists(): return None data = path.read_bytes() file_hash = hashlib.sha256(data).hexdigest()[:16] parts = [] offset = 0 part_num = 1 while offset < len(data): chunk = data[offset : offset + CHUNK_SIZE] chunk_hash = hashlib.sha256(chunk).hexdigest()[:8] parts.append( { "part_num": part_num, "data": chunk, "hash": chunk_hash, } ) offset += CHUNK_SIZE part_num += 1 return { "file_hash": file_hash, "total_parts": len(parts), "file_size": len(data), "filename": path.name, }