Initial Commit2
Browse files- app.py +89 -89
- requirements.txt +2 -5
app.py
CHANGED
|
@@ -1,108 +1,108 @@
|
|
| 1 |
-
import
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
from pathlib import Path
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
WORK_DIR = "/data"
|
| 8 |
os.makedirs(WORK_DIR, exist_ok=True)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
def list_files(path="."):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
return []
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
"size": size,
|
| 25 |
-
"path": os.path.join(path, item)
|
| 26 |
-
})
|
| 27 |
-
return items
|
| 28 |
-
|
| 29 |
-
def upload_file(files, current_path):
|
| 30 |
-
"""上传文件"""
|
| 31 |
-
target_dir = os.path.join(WORK_DIR, current_path)
|
| 32 |
-
os.makedirs(target_dir, exist_ok=True)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
for file in files:
|
| 36 |
-
filename = os.path.bas(file.name)
|
| 37 |
-
target_path = os.path.join(target_dir, filename)
|
| 38 |
-
shutil.copy(file.name, target_path)
|
| 39 |
-
saved_files.append(filename)
|
| 40 |
-
return f"已上传 {len(saved_files)} 个文件"
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
target_dir = os.path.join(WORK_DIR, current_path, folder_name)
|
| 45 |
-
os.makedirs(target_dir, exist_ok=True)
|
| 46 |
-
return "文件夹创建成功"
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
with gr.Group():
|
| 60 |
-
folder_name = gr.Textbox(label="新文件夹名称")
|
| 61 |
-
create_btn = gr.Button("创建文件夹")
|
| 62 |
-
|
| 63 |
-
refresh_btn = gr.Button("刷新列表")
|
| 64 |
-
|
| 65 |
-
with gr.Column(scale=3):
|
| 66 |
-
gr.Markdown("### 文件列表")
|
| 67 |
-
file_table = gr.Dataframe(
|
| 68 |
-
headers=["名称", "类型", "大小", "路径"],
|
| 69 |
-
datatype=["str", "str", "number", "str"],
|
| 70 |
-
interactive=False
|
| 71 |
)
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
outputs=None
|
| 88 |
-
).then(
|
| 89 |
-
fn=lambda p: list_files(p),
|
| 90 |
-
inputs=[current_path],
|
| 91 |
-
outputs=[file_table]
|
| 92 |
-
)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
from pathlib import Path
|
| 5 |
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="个人网盘",
|
| 8 |
+
page_icon="📁",
|
| 9 |
+
layout="wide"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# 初始化session state
|
| 13 |
+
if 'current_path' not in st.session_state:
|
| 14 |
+
st.session_state.current_path = "."
|
| 15 |
+
|
| 16 |
WORK_DIR = "/data"
|
| 17 |
os.makedirs(WORK_DIR, exist_ok=True)
|
| 18 |
|
| 19 |
+
def get_full_path(relative_path):
|
| 20 |
+
return os.path.join(WORK_DIR, relative_path)
|
| 21 |
+
|
| 22 |
def list_files(path="."):
|
| 23 |
+
files = []
|
| 24 |
+
dirs = []
|
| 25 |
+
full_path = get_full_path(path)
|
|
|
|
| 26 |
|
| 27 |
+
if os.path.exists(full_path):
|
| 28 |
+
for item in os.listdir(full_path):
|
| 29 |
+
item_path = os.path.join(full_path, item)
|
| 30 |
+
if os.path.isdir(item_path):
|
| 31 |
+
dirs.append((item, "📁", "-", os.path.join(path, item)))
|
| 32 |
+
else:
|
| 33 |
+
size = os.path.getsize(item_path)
|
| 34 |
+
files.append((item, "📄", f"{size:,} bytes", os.path.join(path, item)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
return dirs + files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# 界面布局
|
| 39 |
+
st.title("📁 个人网盘服务")
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
col1, col2 = st.columns([1, 3])
|
| 42 |
+
|
| 43 |
+
with col1:
|
| 44 |
+
st.header("操作")
|
| 45 |
|
| 46 |
+
# 上传文件
|
| 47 |
+
uploaded_files = st.file_uploader(
|
| 48 |
+
"选择文件上传",
|
| 49 |
+
accept_multiple_files=True
|
| 50 |
+
)
|
| 51 |
|
| 52 |
+
if uploaded_files:
|
| 53 |
+
for uploaded_file in uploaded_files:
|
| 54 |
+
bytes_data = uploaded_file.read()
|
| 55 |
+
save_path = get_full_path(
|
| 56 |
+
os.path.join(st.session_state.current_path, uploaded_file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
+
with open(save_path, "wb") as f:
|
| 59 |
+
f.write(bytes_data)
|
| 60 |
+
st.success(f"上传了 {len(uploaded_files)} 个文件")
|
| 61 |
|
| 62 |
+
# 创建文件夹
|
| 63 |
+
with st.form("create_folder"):
|
| 64 |
+
folder_name = st.text_input("文件夹名称")
|
| 65 |
+
submitted = st.form_submit_button("创建")
|
| 66 |
+
if submitted and folder_name:
|
| 67 |
+
new_dir = get_full_path(
|
| 68 |
+
os.path.join(st.session_state.current_path, folder_name)
|
| 69 |
+
)
|
| 70 |
+
os.makedirs(new_dir, exist_ok=True)
|
| 71 |
+
st.success("文件夹创建成功")
|
| 72 |
+
|
| 73 |
+
with col2:
|
| 74 |
+
st.header("文件列表")
|
| 75 |
|
| 76 |
+
# 面包屑导航
|
| 77 |
+
path_parts = st.session_state.current_path.split('/')
|
| 78 |
+
cols = st.columns(len(path_parts) + 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
with cols[0]:
|
| 81 |
+
if st.button("🏠 根目录"):
|
| 82 |
+
st.session_state.current_path = "."
|
| 83 |
+
st.rerun()
|
|
|
|
| 84 |
|
| 85 |
+
for i, part in enumerate(path_parts):
|
| 86 |
+
if part:
|
| 87 |
+
with cols[i + 1]:
|
| 88 |
+
if st.button(f"📁 {part}"):
|
| 89 |
+
st.session_state.current_path = '/'.join(path_parts[:i+1])
|
| 90 |
+
st.rerun()
|
| 91 |
+
|
| 92 |
+
# 文件列表
|
| 93 |
+
files = list_files(st.session_state.current_path)
|
| 94 |
+
|
| 95 |
+
if files:
|
| 96 |
+
for name, icon, size, full_path in files:
|
| 97 |
+
col_name, col_size, col_action = st.columns([3, 2, 1])
|
| 98 |
+
with col_name:
|
| 99 |
+
st.write(f"{icon} {name}")
|
| 100 |
+
with col_size:
|
| 101 |
+
st.write(size)
|
| 102 |
+
with col_action:
|
| 103 |
+
if icon == "📁":
|
| 104 |
+
if st.button("进入", key=f"enter_{name}"):
|
| 105 |
+
st.session_state.current_path = full_path
|
| 106 |
+
st.rerun()
|
| 107 |
+
else:
|
| 108 |
+
st.info("当前目录为空")
|
requirements.txt
CHANGED
|
@@ -1,5 +1,2 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
python-multipart
|
| 4 |
-
fastapi
|
| 5 |
-
aiofiles
|
|
|
|
| 1 |
+
streamlit>=1.28.0
|
| 2 |
+
python-multipart
|
|
|
|
|
|
|
|
|