File size: 5,417 Bytes
d0d9535
 
 
 
 
7de3bfe
 
 
 
 
 
 
 
d0d9535
 
 
7de3bfe
d0d9535
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7de3bfe
 
 
 
 
d0d9535
7de3bfe
 
d0d9535
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7de3bfe
 
 
 
 
 
 
 
 
 
 
 
d0d9535
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import streamlit as st
import os
import zipfile
from pathlib import Path
import tempfile
import io

# Configure Streamlit to avoid permission issues
st.set_page_config(
    page_title="File Extension Converter",
    page_icon="πŸ”„",
    layout="wide"
)

def is_safe_extension(extension):
    """Check if the extension is safe to convert to"""
    dangerous_extensions = ['.exe', '.bin', '.bat', '.cmd', '.com', '.scr', '.pif', '.vbs']
    return extension.lower() not in dangerous_extensions

def change_file_extension(file_content, original_name, new_extension):
    """Change file extension and return new filename"""
    # Remove the dot if user includes it
    if new_extension.startswith('.'):
        new_extension = new_extension[1:]
    
    # Get the base name without extension
    base_name = Path(original_name).stem
    
    # Create new filename
    new_filename = f"{base_name}.{new_extension}"
    
    return new_filename, file_content

def create_download_zip(files_data):
    """Create a zip file containing all converted files"""
    zip_buffer = io.BytesIO()
    
    with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zipf:
        for filename, content in files_data:
            zipf.writestr(filename, content)
    
    zip_buffer.seek(0)
    return zip_buffer.getvalue()

def main():
    st.title("πŸ”„ File Extension Converter")
    st.markdown("Convert any file extension to another (safely)")
    
    # Sidebar with instructions
    with st.sidebar:
        st.header("πŸ“‹ Instructions")
        st.markdown("""
        1. Upload one or more files
        2. Enter the target extension
        3. Download converted files
        
        **Safety Note:** 
        - .exe and .bin files are blocked for security
        - Only the extension is changed, not the file content
        """)
        
        st.header("⚠️ Blocked Extensions")
        st.code(".exe, .bin, .bat, .cmd, .com, .scr, .pif, .vbs, .js")
    
    # Main interface
    col1, col2 = st.columns([2, 1])
    
    with col1:
        st.header("Upload Files")
        uploaded_files = st.file_uploader(
            "Choose files to convert",
            accept_multiple_files=True,
            help="Upload any files you want to change the extension for"
        )
    
    with col2:
        st.header("Target Extension")
        new_extension = st.text_input(
            "New extension",
            placeholder="e.g., txt, pdf, jpg",
            help="Enter the extension you want to convert to (without the dot)"
        ).strip()
    
    if uploaded_files and new_extension:
        # Validate extension
        if new_extension.startswith('.'):
            new_extension = new_extension[1:]
        
        full_extension = f".{new_extension}"
        
        if not is_safe_extension(full_extension):
            st.error(f"❌ Extension '{full_extension}' is not allowed for security reasons")
            return
        
        st.success(f"βœ… Converting to: **{full_extension}**")
        
        # Process files
        converted_files = []
        
        st.header("πŸ“ Conversion Results")
        
        for uploaded_file in uploaded_files:
            original_name = uploaded_file.name
            file_content = uploaded_file.read()
            
            # Convert extension
            new_filename, new_content = change_file_extension(
                file_content, original_name, new_extension
            )
            
            converted_files.append((new_filename, new_content))
            
            # Display result
            col_orig, col_arrow, col_new = st.columns([3, 1, 3])
            with col_orig:
                st.text(f"πŸ“„ {original_name}")
            with col_arrow:
                st.text("➑️")
            with col_new:
                st.text(f"πŸ“„ {new_filename}")
        
        st.divider()
        
        # Download options
        st.header("πŸ’Ύ Download")
        
        if len(converted_files) == 1:
            # Single file download
            filename, content = converted_files[0]
            st.download_button(
                label=f"πŸ“₯ Download {filename}",
                data=content,
                file_name=filename,
                mime="application/octet-stream"
            )
        else:
            # Multiple files - create zip
            try:
                zip_content = create_download_zip(converted_files)
                st.download_button(
                    label=f"πŸ“₯ Download All Files ({len(converted_files)} files)",
                    data=zip_content,
                    file_name="converted_files.zip",
                    mime="application/zip"
                )
                
                st.info(f"πŸ’‘ {len(converted_files)} files will be downloaded as a ZIP archive")
            except Exception as e:
                st.error(f"Error creating ZIP file: {str(e)}")
    
    elif uploaded_files and not new_extension:
        st.warning("⚠️ Please enter a target extension")
    elif new_extension and not uploaded_files:
        st.warning("⚠️ Please upload files to convert")
    
    # Footer
    st.divider()
    st.markdown("""
    <div style='text-align: center; color: #666; font-size: 0.8em;'>
        Built with Streamlit 🎈 | Safe file extension conversion
    </div>
    """, unsafe_allow_html=True)

if __name__ == "__main__":
    main()