euler314 commited on
Commit
7de3bfe
Β·
verified Β·
1 Parent(s): 98493df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -30
app.py CHANGED
@@ -3,11 +3,18 @@ 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):
@@ -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
- # 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
 
@@ -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
- 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")
 
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")