""" Test if the memory-based file handler fixes the 403 error """ import streamlit as st import sys import os sys.path.append(os.path.dirname(__file__)) # Test both handlers from web_app.utils import FileUploadHandler, MemoryFileHandler st.set_page_config(page_title="403 Error Fix Test", layout="wide") st.title("Test 403 Error Fix") st.write("This test compares the old FileUploadHandler with the new MemoryFileHandler") # File upload uploaded_file = st.file_uploader( "Upload a test file to check for 403 errors", type=['txt', 'csv', 'tsv'], help="We'll test both handlers with this file" ) if uploaded_file: col1, col2 = st.columns(2) with col1: st.subheader("❌ Old Method (FileUploadHandler)") st.write("This may cause 403 errors on restricted environments") try: # Try the old method temp_path = FileUploadHandler.save_to_temp(uploaded_file, prefix="test") if temp_path: st.success(f"✅ Saved to: {temp_path}") content = FileUploadHandler.read_from_temp(temp_path) if content: st.success(f"✅ Read {len(content)} bytes") FileUploadHandler.cleanup_temp_file(temp_path) else: st.error("❌ Failed to save to temp") except Exception as e: st.error(f"❌ Error: {str(e)}") if "403" in str(e): st.error("**403 ERROR DETECTED!**") with col2: st.subheader("✅ New Method (MemoryFileHandler)") st.write("This keeps files in memory, avoiding filesystem") try: # Reset file pointer uploaded_file.seek(0) # Try the new method content = MemoryFileHandler.process_uploaded_file(uploaded_file, as_text=False) if content: st.success(f"✅ Successfully read {len(content)} bytes") st.write("No filesystem access needed!") # Also test text mode uploaded_file.seek(0) text_content = MemoryFileHandler.process_uploaded_file(uploaded_file, as_text=True) if text_content: st.success(f"✅ Text mode: {len(text_content)} characters") else: st.error("❌ Failed to read file") except Exception as e: st.error(f"❌ Error: {str(e)}") st.info(""" **Summary:** - The old FileUploadHandler saves files to /tmp which can trigger 403 errors - The new MemoryFileHandler processes files entirely in memory - To fix your app, replace all FileUploadHandler usage with MemoryFileHandler """) # Quick implementation guide with st.expander("📝 How to implement the fix in your app"): st.code(""" # Replace this: from web_app.utils import FileUploadHandler temp_path = FileUploadHandler.save_to_temp(uploaded_file) content = FileUploadHandler.read_from_temp(temp_path) # With this: from web_app.utils import MemoryFileHandler content = MemoryFileHandler.process_uploaded_file(uploaded_file) """, language="python")