File size: 3,864 Bytes
05b45a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
import datetime
import os
from typing import List, Optional, Tuple

from .config import AUDIO_FORMATS, INPUTS_DIR, OUTPUTS_DIR


def list_input_files() -> List[str]:
    """List all input text files."""
    return [f for f in os.listdir(INPUTS_DIR) if f.endswith(".txt")]


def list_output_files() -> List[str]:
    """List all output audio files, sorted by most recent first."""
    files = [
        os.path.join(OUTPUTS_DIR, f)
        for f in os.listdir(OUTPUTS_DIR)
        if any(f.endswith(ext) for ext in AUDIO_FORMATS)
    ]
    # Sort files by modification time, most recent first
    return sorted(files, key=os.path.getmtime, reverse=True)


def read_text_file(filename: str) -> str:
    """Read content of a text file."""
    if not filename:
        return ""
    try:
        file_path = os.path.join(INPUTS_DIR, filename)
        with open(file_path, "r", encoding="utf-8") as f:
            return f.read()
    except:
        return ""


def save_text(text: str, filename: Optional[str] = None) -> Optional[str]:
    """Save text to a file. Returns the filename if successful."""
    if not text.strip():
        return None

    if filename is None:
        # Use input_1.txt, input_2.txt, etc.
        base = "input"
        counter = 1
        while True:
            filename = f"{base}_{counter}.txt"
            if not os.path.exists(os.path.join(INPUTS_DIR, filename)):
                break
            counter += 1
    else:
        # Handle duplicate filenames by adding _1, _2, etc.
        base = os.path.splitext(filename)[0]
        ext = os.path.splitext(filename)[1] or ".txt"
        counter = 1
        while os.path.exists(os.path.join(INPUTS_DIR, filename)):
            filename = f"{base}_{counter}{ext}"
            counter += 1

    filepath = os.path.join(INPUTS_DIR, filename)
    try:
        with open(filepath, "w", encoding="utf-8") as f:
            f.write(text)
        return filename
    except Exception as e:
        print(f"Error saving file: {e}")
        return None


def delete_all_input_files() -> bool:
    """Delete all files from the inputs directory. Returns True if successful."""
    try:
        for filename in os.listdir(INPUTS_DIR):
            if filename.endswith(".txt"):
                file_path = os.path.join(INPUTS_DIR, filename)
                os.remove(file_path)
        return True
    except Exception as e:
        print(f"Error deleting input files: {e}")
        return False


def delete_all_output_files() -> bool:
    """Delete all audio files from the outputs directory. Returns True if successful."""
    try:
        for filename in os.listdir(OUTPUTS_DIR):
            if any(filename.endswith(ext) for ext in AUDIO_FORMATS):
                file_path = os.path.join(OUTPUTS_DIR, filename)
                os.remove(file_path)
        return True
    except Exception as e:
        print(f"Error deleting output files: {e}")
        return False


def process_uploaded_file(file_path: str) -> bool:
    """Save uploaded file to inputs directory. Returns True if successful."""
    if not file_path:
        return False

    try:
        filename = os.path.basename(file_path)
        if not filename.endswith(".txt"):
            return False

        # Create target path in inputs directory
        target_path = os.path.join(INPUTS_DIR, filename)

        # If file exists, add number suffix
        base, ext = os.path.splitext(filename)
        counter = 1
        while os.path.exists(target_path):
            new_name = f"{base}_{counter}{ext}"
            target_path = os.path.join(INPUTS_DIR, new_name)
            counter += 1

        # Copy file to inputs directory
        import shutil

        shutil.copy2(file_path, target_path)
        return True

    except Exception as e:
        print(f"Error saving uploaded file: {e}")
        return False