You've already forked simplefilehub
50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
import streamlit as st
|
|
from pathlib import Path
|
|
|
|
STORAGE_DIR = Path("storage")
|
|
STORAGE_DIR.mkdir(exist_ok=True)
|
|
|
|
st.title("Simple File Hub")
|
|
st.text("Demo app")
|
|
|
|
st.header("Upload a file")
|
|
with st.form("upload", clear_on_submit=True):
|
|
uploaded_file = st.file_uploader("Choose a file to upload")
|
|
file_submitted = st.form_submit_button("Upload")
|
|
|
|
if file_submitted and (uploaded_file is not None):
|
|
dest = STORAGE_DIR / uploaded_file.name
|
|
try:
|
|
with open(dest, "wb") as f:
|
|
f.write(uploaded_file.getbuffer())
|
|
except Exception as ex:
|
|
st.error(f"Failed to upload: {ex}")
|
|
st.stop()
|
|
|
|
st.success("Uploaded successfully!")
|
|
|
|
st.divider()
|
|
|
|
st.header("Hosted Files")
|
|
files = tuple(STORAGE_DIR.iterdir())
|
|
|
|
if not files:
|
|
st.info("No uploads yet.")
|
|
else:
|
|
for file in files:
|
|
col1, col2, col3 = st.columns([4, 1, 1])
|
|
size_kb = file.stat().st_size / 1024
|
|
col1.write(f"**{file.name}** ({size_kb:.1f} KB)")
|
|
|
|
with open(file, "rb") as f:
|
|
col2.download_button(
|
|
label="Download",
|
|
data=f,
|
|
file_name=file.name,
|
|
key=f"dl_{file.name}"
|
|
)
|
|
|
|
if col3.button("Delete", key=f"del_{file.name}"):
|
|
file.unlink()
|
|
st.rerun()
|