| """ | |
| Utility Functions Module | |
| Common helper functions used across the application. | |
| """ | |
| from pathlib import Path | |
| from datetime import datetime | |
| def format_size(size: int) -> str: | |
| """Format bytes to human readable string""" | |
| for unit in ['B', 'KB', 'MB', 'GB', 'TB']: | |
| if size < 1024: | |
| return f"{size:.1f} {unit}" | |
| size /= 1024 | |
| return f"{size:.1f} PB" | |
| def get_file_icon(name: str, is_dir: bool = False) -> str: | |
| """Get emoji icon for file type""" | |
| if is_dir: | |
| return "π" | |
| ext = Path(name).suffix.lower() | |
| icons = { | |
| '.txt': 'π', '.md': 'π', '.pdf': 'π', '.doc': 'π', '.csv': 'π', | |
| '.py': 'π', '.js': 'π', '.json': 'π', '.html': 'π', '.css': 'π¨', | |
| '.jpg': 'πΌοΈ', '.jpeg': 'πΌοΈ', '.png': 'πΌοΈ', '.gif': 'πΌοΈ', '.svg': 'πΌοΈ', | |
| '.mp3': 'π΅', '.wav': 'π΅', '.mp4': 'π¬', '.avi': 'π¬', | |
| '.zip': 'π¦', '.tar': 'π¦', '.gz': 'π¦', | |
| '.pt': 'π§ ', '.pth': 'π§ ', '.safetensors': 'π§ ', '.ckpt': 'π§ ', '.bin': 'π§ ', | |
| '.env': 'π', '.log': 'π', '.sh': 'π₯οΈ', | |
| } | |
| return icons.get(ext, 'π') | |
| def get_mime_type(name: str) -> str: | |
| """Get mime type category for file""" | |
| ext = Path(name).suffix.lower() | |
| if ext in ['.jpg', '.jpeg', '.png', '.gif', '.svg', '.webp']: | |
| return 'image' | |
| if ext in ['.txt', '.md', '.py', '.js', '.json', '.html', '.css', '.yaml', '.yml', '.sh', '.log', '.csv']: | |
| return 'text' | |
| return 'binary' | |
| def format_timestamp(ts: float) -> str: | |
| """Format unix timestamp to string""" | |
| return datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M") | |