File size: 2,285 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
from docx import Document
from pdfminer.high_level import extract_text
from pathlib import Path
from tempfile import NamedTemporaryFile
import uuid

class FileHandler:
    @staticmethod
    def read_file(file_path: str) -> str:
        if file_path.endswith('.docx'):
            return FileHandler._read_docx(file_path)
        elif file_path.endswith('.pdf'):
            return extract_text(file_path)
        else:
            with open(file_path, 'r') as f:
                return f.read()
    #--
    
    @staticmethod
    def save_uploaded_file(uploaded_file, directory="temp_uploads"):
        """Save Streamlit uploaded file to a temporary directory"""
        try:
            # Create directory if it doesn't exist
            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 cleanup_temp_files(directory="temp_uploads"):
        """Remove temporary files"""
        try:
            for file in Path(directory).glob("*"):
                file.unlink()
        except Exception as e:
            print(f"Error cleaning files: {e}")
   #--
   
    @staticmethod
    def _read_docx(file_path: str) -> str:
        doc = Document(file_path)
        return '\n'.join([para.text for para in doc.paragraphs])
    
    @staticmethod
    def save_resume(resume_data: dict, output_path: str):
        if output_path.endswith('.docx'):
            FileHandler._save_as_docx(resume_data, output_path)
        else:
            with open(output_path, 'w') as f:
                f.write(resume_data['content'])

    @staticmethod
    def _save_as_docx(resume_data: dict, output_path: str):
        doc = Document()
        # Add formatting preservation logic here
        doc.save(output_path)