File size: 1,855 Bytes
5dec17e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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