euler314 commited on
Commit
d0d9535
Β·
verified Β·
1 Parent(s): 23f3683

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +167 -0
app.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import zipfile
4
+ from pathlib import Path
5
+ import tempfile
6
+ import shutil
7
+
8
+ def is_safe_extension(extension):
9
+ """Check if the extension is safe to convert to"""
10
+ dangerous_extensions = ['.exe', '.bin', '.bat', '.cmd', '.com', '.scr', '.pif', '.vbs', '.js']
11
+ return extension.lower() not in dangerous_extensions
12
+
13
+ def change_file_extension(file_content, original_name, new_extension):
14
+ """Change file extension and return new filename"""
15
+ # Remove the dot if user includes it
16
+ if new_extension.startswith('.'):
17
+ new_extension = new_extension[1:]
18
+
19
+ # Get the base name without extension
20
+ base_name = Path(original_name).stem
21
+
22
+ # Create new filename
23
+ new_filename = f"{base_name}.{new_extension}"
24
+
25
+ return new_filename, file_content
26
+
27
+ def create_download_zip(files_data):
28
+ """Create a zip file containing all converted files"""
29
+ # Create a temporary directory
30
+ with tempfile.TemporaryDirectory() as temp_dir:
31
+ zip_path = os.path.join(temp_dir, "converted_files.zip")
32
+
33
+ with zipfile.ZipFile(zip_path, 'w') as zipf:
34
+ for filename, content in files_data:
35
+ zipf.writestr(filename, content)
36
+
37
+ # Read the zip file content
38
+ with open(zip_path, 'rb') as f:
39
+ zip_content = f.read()
40
+
41
+ return zip_content
42
+
43
+ def main():
44
+ st.set_page_config(
45
+ page_title="File Extension Converter",
46
+ page_icon="πŸ”„",
47
+ layout="wide"
48
+ )
49
+
50
+ st.title("πŸ”„ File Extension Converter")
51
+ st.markdown("Convert any file extension to another (safely)")
52
+
53
+ # Sidebar with instructions
54
+ with st.sidebar:
55
+ st.header("πŸ“‹ Instructions")
56
+ st.markdown("""
57
+ 1. Upload one or more files
58
+ 2. Enter the target extension
59
+ 3. Download converted files
60
+
61
+ **Safety Note:**
62
+ - .exe and .bin files are blocked for security
63
+ - Only the extension is changed, not the file content
64
+ """)
65
+
66
+ st.header("⚠️ Blocked Extensions")
67
+ st.code(".exe, .bin, .bat, .cmd, .com, .scr, .pif, .vbs, .js")
68
+
69
+ # Main interface
70
+ col1, col2 = st.columns([2, 1])
71
+
72
+ with col1:
73
+ st.header("Upload Files")
74
+ uploaded_files = st.file_uploader(
75
+ "Choose files to convert",
76
+ accept_multiple_files=True,
77
+ help="Upload any files you want to change the extension for"
78
+ )
79
+
80
+ with col2:
81
+ st.header("Target Extension")
82
+ new_extension = st.text_input(
83
+ "New extension",
84
+ placeholder="e.g., txt, pdf, jpg",
85
+ help="Enter the extension you want to convert to (without the dot)"
86
+ ).strip()
87
+
88
+ if uploaded_files and new_extension:
89
+ # Validate extension
90
+ if new_extension.startswith('.'):
91
+ new_extension = new_extension[1:]
92
+
93
+ full_extension = f".{new_extension}"
94
+
95
+ if not is_safe_extension(full_extension):
96
+ st.error(f"❌ Extension '{full_extension}' is not allowed for security reasons")
97
+ return
98
+
99
+ st.success(f"βœ… Converting to: **{full_extension}**")
100
+
101
+ # Process files
102
+ converted_files = []
103
+ results_data = []
104
+
105
+ st.header("πŸ“ Conversion Results")
106
+
107
+ for uploaded_file in uploaded_files:
108
+ original_name = uploaded_file.name
109
+ file_content = uploaded_file.read()
110
+
111
+ # Convert extension
112
+ new_filename, new_content = change_file_extension(
113
+ file_content, original_name, new_extension
114
+ )
115
+
116
+ converted_files.append((new_filename, new_content))
117
+
118
+ # Display result
119
+ col_orig, col_arrow, col_new = st.columns([3, 1, 3])
120
+ with col_orig:
121
+ st.text(f"πŸ“„ {original_name}")
122
+ with col_arrow:
123
+ st.text("➑️")
124
+ with col_new:
125
+ st.text(f"πŸ“„ {new_filename}")
126
+
127
+ st.divider()
128
+
129
+ # Download options
130
+ st.header("πŸ’Ύ Download")
131
+
132
+ if len(converted_files) == 1:
133
+ # Single file download
134
+ filename, content = converted_files[0]
135
+ st.download_button(
136
+ label=f"πŸ“₯ Download {filename}",
137
+ data=content,
138
+ file_name=filename,
139
+ mime="application/octet-stream"
140
+ )
141
+ else:
142
+ # Multiple files - create zip
143
+ zip_content = create_download_zip(converted_files)
144
+ st.download_button(
145
+ label=f"πŸ“₯ Download All Files ({len(converted_files)} files)",
146
+ data=zip_content,
147
+ file_name="converted_files.zip",
148
+ mime="application/zip"
149
+ )
150
+
151
+ st.info(f"πŸ’‘ {len(converted_files)} files will be downloaded as a ZIP archive")
152
+
153
+ elif uploaded_files and not new_extension:
154
+ st.warning("⚠️ Please enter a target extension")
155
+ elif new_extension and not uploaded_files:
156
+ st.warning("⚠️ Please upload files to convert")
157
+
158
+ # Footer
159
+ st.divider()
160
+ st.markdown("""
161
+ <div style='text-align: center; color: #666; font-size: 0.8em;'>
162
+ Built with Streamlit 🎈 | Safe file extension conversion
163
+ </div>
164
+ """, unsafe_allow_html=True)
165
+
166
+ if __name__ == "__main__":
167
+ main()