Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,18 @@ import os
|
|
3 |
import zipfile
|
4 |
from pathlib import Path
|
5 |
import tempfile
|
6 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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'
|
11 |
return extension.lower() not in dangerous_extensions
|
12 |
|
13 |
def change_file_extension(file_content, original_name, new_extension):
|
@@ -26,27 +33,16 @@ def change_file_extension(file_content, original_name, new_extension):
|
|
26 |
|
27 |
def create_download_zip(files_data):
|
28 |
"""Create a zip file containing all converted files"""
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
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 |
-
|
|
|
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 |
|
@@ -100,7 +96,6 @@ def main():
|
|
100 |
|
101 |
# Process files
|
102 |
converted_files = []
|
103 |
-
results_data = []
|
104 |
|
105 |
st.header("π Conversion Results")
|
106 |
|
@@ -140,15 +135,18 @@ def main():
|
|
140 |
)
|
141 |
else:
|
142 |
# Multiple files - create zip
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
|
|
|
|
|
|
152 |
|
153 |
elif uploaded_files and not new_extension:
|
154 |
st.warning("β οΈ Please enter a target extension")
|
|
|
3 |
import zipfile
|
4 |
from pathlib import Path
|
5 |
import tempfile
|
6 |
+
import io
|
7 |
+
|
8 |
+
# Configure Streamlit to avoid permission issues
|
9 |
+
st.set_page_config(
|
10 |
+
page_title="File Extension Converter",
|
11 |
+
page_icon="π",
|
12 |
+
layout="wide"
|
13 |
+
)
|
14 |
|
15 |
def is_safe_extension(extension):
|
16 |
"""Check if the extension is safe to convert to"""
|
17 |
+
dangerous_extensions = ['.exe', '.bin', '.bat', '.cmd', '.com', '.scr', '.pif', '.vbs']
|
18 |
return extension.lower() not in dangerous_extensions
|
19 |
|
20 |
def change_file_extension(file_content, original_name, new_extension):
|
|
|
33 |
|
34 |
def create_download_zip(files_data):
|
35 |
"""Create a zip file containing all converted files"""
|
36 |
+
zip_buffer = io.BytesIO()
|
37 |
+
|
38 |
+
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
39 |
+
for filename, content in files_data:
|
40 |
+
zipf.writestr(filename, content)
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
zip_buffer.seek(0)
|
43 |
+
return zip_buffer.getvalue()
|
44 |
|
45 |
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
st.title("π File Extension Converter")
|
47 |
st.markdown("Convert any file extension to another (safely)")
|
48 |
|
|
|
96 |
|
97 |
# Process files
|
98 |
converted_files = []
|
|
|
99 |
|
100 |
st.header("π Conversion Results")
|
101 |
|
|
|
135 |
)
|
136 |
else:
|
137 |
# Multiple files - create zip
|
138 |
+
try:
|
139 |
+
zip_content = create_download_zip(converted_files)
|
140 |
+
st.download_button(
|
141 |
+
label=f"π₯ Download All Files ({len(converted_files)} files)",
|
142 |
+
data=zip_content,
|
143 |
+
file_name="converted_files.zip",
|
144 |
+
mime="application/zip"
|
145 |
+
)
|
146 |
+
|
147 |
+
st.info(f"π‘ {len(converted_files)} files will be downloaded as a ZIP archive")
|
148 |
+
except Exception as e:
|
149 |
+
st.error(f"Error creating ZIP file: {str(e)}")
|
150 |
|
151 |
elif uploaded_files and not new_extension:
|
152 |
st.warning("β οΈ Please enter a target extension")
|