import os from pathlib import Path import uuid from docx import Document from pdfminer.high_level import extract_text class FileHandler: @staticmethod def save_uploaded_file(uploaded_file, directory="temp_uploads"): """Save Streamlit uploaded file to temporary directory""" try: # Create directory if needed Path(directory).mkdir(exist_ok=True) # Generate unique filename file_ext = Path(uploaded_file.name).suffix unique_id = uuid.uuid4().hex temp_file = Path(directory) / f"{unique_id}{file_ext}" # Save file with open(temp_file, "wb") as f: f.write(uploaded_file.getbuffer()) return str(temp_file) except Exception as e: print(f"Error saving file: {e}") return None @staticmethod def save_resume(resume_data, output_path): """Save resume content to file""" try: if output_path.endswith('.docx'): doc = Document() for paragraph in resume_data.split('\n'): doc.add_paragraph(paragraph) doc.save(output_path) else: with open(output_path, 'w') as f: f.write(resume_data) return True except Exception as e: print(f"Error saving resume: {e}") return False @staticmethod def cleanup_temp_files(directory="temp_uploads"): """Remove temporary files""" try: for file in Path(directory).glob("*"): file.unlink() return True except Exception as e: print(f"Error cleaning files: {e}") return False