File size: 1,485 Bytes
c7e434a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
File storage utilities
"""

import os
import shutil
from pathlib import Path
from app.config import settings


def ensure_upload_dir():
    """Ensure upload directory exists"""
    Path(settings.UPLOAD_DIR).mkdir(parents=True, exist_ok=True)


def save_uploaded_file(file_content: bytes, filename: str) -> str:
    """
    Save uploaded file
    
    Returns:
        str: Path to saved file
    """
    ensure_upload_dir()
    
    file_path = os.path.join(settings.UPLOAD_DIR, filename)
    
    with open(file_path, "wb") as f:
        f.write(file_content)
    
    return file_path


def delete_file(file_path: str):
    """Delete file if exists"""
    if os.path.exists(file_path):
        os.remove(file_path)


def cleanup_old_files(max_age_hours: int = 24):
    """Cleanup old uploaded files"""
    import time
    
    if not os.path.exists(settings.UPLOAD_DIR):
        return
    
    current_time = time.time()
    max_age_seconds = max_age_hours * 3600
    
    for filename in os.listdir(settings.UPLOAD_DIR):
        file_path = os.path.join(settings.UPLOAD_DIR, filename)
        
        if os.path.isfile(file_path):
            file_age = current_time - os.path.getmtime(file_path)
            
            if file_age > max_age_seconds:
                try:
                    delete_file(file_path)
                    print(f"Deleted old file: {filename}")
                except Exception as e:
                    print(f"Error deleting {filename}: {e}")