Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,33 @@
|
|
| 1 |
-
from flask import Flask,
|
| 2 |
import os
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
| 6 |
-
# 指定要浏览的目录
|
| 7 |
-
base_directory = '/app'
|
| 8 |
-
|
| 9 |
@app.route('/')
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
full_path = os.path.join(base_directory, entry)
|
| 18 |
-
if os.path.isdir(full_path):
|
| 19 |
-
# 如果是子目录,创建链接
|
| 20 |
-
file_list += f"<li><a href='{entry}/'>{entry}/</a></li>"
|
| 21 |
-
else:
|
| 22 |
-
# 如果是文件,直接显示文件名
|
| 23 |
-
file_list += f"<li>{entry}</li>"
|
| 24 |
-
file_list += "</ul>"
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
if __name__ == '__main__':
|
| 34 |
app.run(debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, send_from_directory, render_template_string
|
| 2 |
import os
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
@app.route('/')
|
| 7 |
+
@app.route('/<path:req_path>')
|
| 8 |
+
def dir_listing(req_path=''):
|
| 9 |
+
base_dir = os.path.abspath(os.getcwd()) # 使用绝对路径
|
| 10 |
+
abs_path = os.path.join(base_dir, req_path)
|
| 11 |
|
| 12 |
+
# 检查路径是否存在
|
| 13 |
+
if not os.path.exists(abs_path):
|
| 14 |
+
return render_template_string('<h1>404 Not Found</h1>'), 404
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# 检查路径是否是文件
|
| 17 |
+
if os.path.isfile(abs_path):
|
| 18 |
+
# 如果是文件,则下载文件
|
| 19 |
+
return send_from_directory(base_dir, req_path, as_attachment=True)
|
| 20 |
|
| 21 |
+
# 显示目录内容
|
| 22 |
+
files = os.listdir(abs_path)
|
| 23 |
+
return render_template_string('''
|
| 24 |
+
<h1>Directory listing for {{req_path}}</h1>
|
| 25 |
+
<ul>
|
| 26 |
+
{% for file in files %}
|
| 27 |
+
<li><a href="{{ req_path|safe }}/{{ file|safe }}">{{ file }}</a></li>
|
| 28 |
+
{% endfor %}
|
| 29 |
+
</ul>
|
| 30 |
+
''', files=files, req_path=req_path)
|
| 31 |
|
| 32 |
if __name__ == '__main__':
|
| 33 |
app.run(debug=True)
|