Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,139 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import zipfile
|
|
|
|
| 4 |
from pathlib import Path
|
| 5 |
-
import io
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
""
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
""
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
accept_multiple_files=True,
|
| 73 |
-
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 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=
|
| 126 |
-
data=
|
| 127 |
-
file_name=
|
| 128 |
-
mime="application/
|
| 129 |
)
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 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)
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Streamlit File Extension Renamer
|
| 3 |
+
--------------------------------
|
| 4 |
+
A lightweight Streamlit app intended for deployment on Hugging Face Spaces.
|
| 5 |
+
The app lets users upload one or more files and rename (not convert) their
|
| 6 |
+
extensions to any common file-type extension the user selects. Executable
|
| 7 |
+
and binary files are blocked for security reasons.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
import io
|
| 13 |
import zipfile
|
| 14 |
+
from datetime import datetime
|
| 15 |
from pathlib import Path
|
|
|
|
| 16 |
|
| 17 |
+
import streamlit as st
|
| 18 |
+
|
| 19 |
+
# -----------------------------------------------------------------------------
|
| 20 |
+
# Configuration
|
| 21 |
+
# -----------------------------------------------------------------------------
|
| 22 |
+
# List of target extensions displayed to the user. Extend this list as needed.
|
| 23 |
+
ALLOWED_TARGET_EXTS: list[str] = [
|
| 24 |
+
".txt",
|
| 25 |
+
".pdf",
|
| 26 |
+
".doc",
|
| 27 |
+
".docx",
|
| 28 |
+
".json",
|
| 29 |
+
".csv",
|
| 30 |
+
".xml",
|
| 31 |
+
".html",
|
| 32 |
+
".css",
|
| 33 |
+
".js",
|
| 34 |
+
".md",
|
| 35 |
+
".jpg",
|
| 36 |
+
".jpeg",
|
| 37 |
+
".png",
|
| 38 |
+
".gif",
|
| 39 |
+
".bmp",
|
| 40 |
+
".tiff",
|
| 41 |
+
".svg",
|
| 42 |
+
".ico",
|
| 43 |
+
".mp3",
|
| 44 |
+
".wav",
|
| 45 |
+
".mp4",
|
| 46 |
+
".avi",
|
| 47 |
+
".mkv",
|
| 48 |
+
".mov",
|
| 49 |
+
".zip",
|
| 50 |
+
".tar",
|
| 51 |
+
".gz",
|
| 52 |
+
".7z",
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
# Sourceβfile extensions that are disallowed (will be skipped if uploaded).
|
| 56 |
+
DISALLOWED_SOURCE_EXTS: set[str] = {".exe", ".bin"}
|
| 57 |
+
|
| 58 |
+
# -----------------------------------------------------------------------------
|
| 59 |
+
# UI helpers
|
| 60 |
+
# -----------------------------------------------------------------------------
|
| 61 |
+
|
| 62 |
+
def make_sidebar() -> str:
|
| 63 |
+
"""Return the user-chosen target extension from sidebar controls."""
|
| 64 |
+
st.sidebar.header("Settings")
|
| 65 |
+
target_ext = st.sidebar.selectbox(
|
| 66 |
+
label="Target extension (applied to **all** uploaded files)",
|
| 67 |
+
options=ALLOWED_TARGET_EXTS,
|
| 68 |
+
index=ALLOWED_TARGET_EXTS.index(".pdf"),
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
st.sidebar.markdown(
|
| 72 |
+
"""β **Note**: This tool only renames the file extension. It does **not**
|
| 73 |
+
convert the underlying file format. Opening a renamed file with an
|
| 74 |
+
incompatible program may fail or lead to data corruption."""
|
| 75 |
+
)
|
| 76 |
+
return target_ext
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def make_uploader():
|
| 80 |
+
"""Return the list of files uploaded by the user."""
|
| 81 |
+
return st.file_uploader(
|
| 82 |
+
"Upload one or more files",
|
| 83 |
accept_multiple_files=True,
|
| 84 |
+
type=[ext.lstrip(".") for ext in ALLOWED_TARGET_EXTS],
|
| 85 |
+
help="Drag-and-drop or browse for files to rename.",
|
| 86 |
)
|
| 87 |
|
| 88 |
+
|
| 89 |
+
# -----------------------------------------------------------------------------
|
| 90 |
+
# Core logic
|
| 91 |
+
# -----------------------------------------------------------------------------
|
| 92 |
+
|
| 93 |
+
def write_zip(uploaded_files: list[st.runtime.uploaded_file_manager.UploadedFile], target_ext: str) -> io.BytesIO:
|
| 94 |
+
"""Return an in-memory ZIP archive of the uploaded files with the new extension."""
|
| 95 |
+
buffer = io.BytesIO()
|
| 96 |
+
with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 97 |
+
for file in uploaded_files:
|
| 98 |
+
orig_path = Path(file.name)
|
| 99 |
+
if orig_path.suffix.lower() in DISALLOWED_SOURCE_EXTS:
|
| 100 |
+
st.warning(f"βοΈ Skipping disallowed file: **{orig_path.name}**")
|
| 101 |
+
continue
|
| 102 |
+
|
| 103 |
+
renamed = orig_path.with_suffix(target_ext)
|
| 104 |
+
zf.writestr(renamed.name, file.read())
|
| 105 |
+
st.success(f"β
Renamed **{orig_path.name}** β **{renamed.name}**")
|
| 106 |
+
|
| 107 |
+
buffer.seek(0)
|
| 108 |
+
return buffer
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# -----------------------------------------------------------------------------
|
| 112 |
+
# Main app
|
| 113 |
+
# -----------------------------------------------------------------------------
|
| 114 |
+
|
| 115 |
+
def main() -> None:
|
| 116 |
+
st.set_page_config("Extension Renamer", page_icon="π", layout="centered")
|
| 117 |
+
st.title("π Universal File-Extension Renamer")
|
| 118 |
+
st.write(
|
| 119 |
+
"Upload files, choose a new extension, and download them renamed in a single ZIP archive."
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
target_ext = make_sidebar()
|
| 123 |
+
uploaded_files = make_uploader()
|
| 124 |
+
|
| 125 |
+
if uploaded_files and st.button("π Rename & Package"):
|
| 126 |
+
zip_buffer = write_zip(uploaded_files, target_ext)
|
| 127 |
+
timestamp = datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
st.download_button(
|
| 129 |
+
label="β¬οΈ Download ZIP",
|
| 130 |
+
data=zip_buffer,
|
| 131 |
+
file_name=f"renamed_{timestamp}.zip",
|
| 132 |
+
mime="application/zip",
|
| 133 |
)
|
| 134 |
+
|
| 135 |
+
st.caption("Β© 2025 File-Extension Renamer β’ Built with Streamlit β’ Deployed on Hugging Face Spaces")
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|