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")