Spaces:
Configuration error

hustzhxy commited on
Commit
e60beaa
·
1 Parent(s): 2a8d384

Initial Commit2

Browse files
Files changed (2) hide show
  1. app.py +89 -89
  2. requirements.txt +2 -5
app.py CHANGED
@@ -1,108 +1,108 @@
1
- import gradio as gr
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
- full_path = os.path.join(WORK_DIR, path)
13
- if not os.path.exists(full_path):
14
- return []
15
 
16
- items = []
17
- for item in os.listdir(full_path):
18
- item_path = os.path.join(full_path, item)
19
- is_dir = os.path.isdir(item_path)
20
- size = os.path.getsize(item_path) if not is_dir else 0
21
- items.append({
22
- "name": item,
23
- "type": "directory" if is_dir else "file",
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
- saved_files = []
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
- def create_folder(folder_name, current_path):
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
- with gr.Blocks(title="个人网盘") as app:
49
- gr.Markdown("# 🗂️ 个人文件管理系统")
 
 
50
 
51
- current_path = gr.State(".")
 
 
 
 
52
 
53
- with gr.Row():
54
- with gr.Column(scale=1):
55
- gr.Markdown("### 操作面板")
56
- upload = gr.File(label="选择文件", file_count="multiple")
57
- upload_btn = gr.Button("上传文件")
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
- upload_btn.click(
75
- fn=upload_file,
76
- inputs=[upload, current_path],
77
- outputs=None
78
- ).then(
79
- fn=lambda p: list_files(p),
80
- inputs=[current_path],
81
- outputs=[file_table]
82
- )
 
 
 
83
 
84
- create_btn.click(
85
- fn=create_folder,
86
- inputs=[folder_name, current_path],
87
- outputs=None
88
- ).then(
89
- fn=lambda p: list_files(p),
90
- inputs=[current_path],
91
- outputs=[file_table]
92
- )
93
 
94
- refresh_btn.click(
95
- fn=lambda p: list_files(p),
96
- inputs=[current_path],
97
- outputs=[file_table]
98
- )
99
 
100
- # 初始化加载文件列表
101
- app.load(
102
- fn=lambda: list_files("."),
103
- inputs=None,
104
- outputs=[file_table]
105
- )
106
-
107
- if __name__ == "__main__":
108
- app.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- uvicorn[standard]
2
- gradio>=4.0
3
- python-multipart
4
- fastapi
5
- aiofiles
 
1
+ streamlit>=1.28.0
2
+ python-multipart