textai-v2 / modules /utils.py
rbt2025's picture
Deploy TextAI v2 - Clean architecture
02daacc verified
"""
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")