File size: 3,620 Bytes
4b36911
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
"""
Script to update all file upload handlers to use MemoryFileHandler
This will prevent 403 errors on restricted environments like Hugging Face Spaces
"""

import os
import re
from pathlib import Path

def update_file(filepath):
    """Update a single file to use MemoryFileHandler."""
    
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
    
    original_content = content
    
    # Update import statements
    content = re.sub(
        r'from web_app\.utils import FileUploadHandler',
        'from web_app.utils import MemoryFileHandler',
        content
    )
    
    # Update FileUploadHandler calls to MemoryFileHandler
    content = re.sub(r'FileUploadHandler\.', 'MemoryFileHandler.', content)
    
    # Update save_to_temp pattern
    # Old pattern: temp_path = FileUploadHandler.save_to_temp(uploaded_file, prefix="...")
    # New pattern: content = MemoryFileHandler.process_uploaded_file(uploaded_file)
    
    # Replace save_to_temp and read_from_temp patterns
    content = re.sub(
        r'temp_path = MemoryFileHandler\.save_to_temp\(([^,]+), prefix="[^"]+"\)\s*'
        r'if temp_path:\s*'
        r'.*?= MemoryFileHandler\.read_from_temp\(temp_path\)',
        r'content = MemoryFileHandler.process_uploaded_file(\1)',
        content,
        flags=re.DOTALL
    )
    
    # Replace validate_file_size (MemoryFileHandler doesn't need this as it checks inline)
    content = re.sub(
        r'if not MemoryFileHandler\.validate_file_size\([^)]+\):\s*return',
        'if uploaded_file.size > 300 * 1024 * 1024:\n'
        '                        st.error(f"File too large ({uploaded_file.size / 1024 / 1024:.1f} MB). Maximum allowed: 300MB")\n'
        '                        return',
        content
    )
    
    # Remove cleanup_temp_file calls
    content = re.sub(
        r'MemoryFileHandler\.cleanup_temp_file\([^)]+\)\s*',
        '',
        content
    )
    
    # Remove cleanup_old_temp_files calls
    content = re.sub(
        r'MemoryFileHandler\.cleanup_old_temp_files\([^)]+\)\s*',
        '',
        content
    )
    
    if content != original_content:
        with open(filepath, 'w', encoding='utf-8') as f:
            f.write(content)
        print(f"βœ… Updated: {filepath}")
        return True
    else:
        print(f"⏭️  No changes needed: {filepath}")
        return False

def main():
    """Main function to update all relevant files."""
    
    # Files to update
    files_to_update = [
        'web_app/handlers/analysis_handlers.py',
        'web_app/handlers/pos_handlers.py',
        'web_app/handlers/frequency_handlers.py',
        'web_app/reference_manager.py',
        'web_app/config_manager.py',
        'web_app/debug_utils.py'
    ]
    
    updated_count = 0
    
    print("πŸ”„ Updating file handlers to use MemoryFileHandler...")
    print("=" * 60)
    
    for file_path in files_to_update:
        if os.path.exists(file_path):
            if update_file(file_path):
                updated_count += 1
        else:
            print(f"❌ File not found: {file_path}")
    
    print("=" * 60)
    print(f"βœ… Updated {updated_count} files")
    
    # Create a backup of the old FileUploadHandler
    old_handler_path = 'web_app/utils/file_upload_handler.py'
    backup_path = 'web_app/utils/file_upload_handler.py.backup'
    
    if os.path.exists(old_handler_path) and not os.path.exists(backup_path):
        import shutil
        shutil.copy2(old_handler_path, backup_path)
        print(f"πŸ“‹ Created backup: {backup_path}")

if __name__ == "__main__":
    main()