File size: 1,681 Bytes
02daacc |
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 |
"""
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")
|