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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -123
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
- # 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):
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
- def main():
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
- # Validate extension
86
- if new_extension.startswith('.'):
87
- new_extension = new_extension[1:]
88
-
89
- full_extension = f".{new_extension}"
90
-
91
- if not is_safe_extension(full_extension):
92
- st.error(f"❌ Extension '{full_extension}' is not allowed for security reasons")
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
- st.divider()
 
 
123
 
124
- # Download options
125
- st.header("πŸ’Ύ Download")
126
 
127
- if len(converted_files) == 1:
128
- # Single file download
129
- filename, content = converted_files[0]
130
- st.download_button(
131
- label=f"πŸ“₯ Download {filename}",
132
- data=content,
133
- file_name=filename,
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
- st.markdown("""
159
- <div style='text-align: center; color: #666; font-size: 0.8em;'>
160
- Built with Streamlit 🎈 | Safe file extension conversion
161
- </div>
162
- """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
- if __name__ == "__main__":
165
- main()
 
 
 
 
 
 
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)