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