Spaces:
Runtime error
Runtime error
Commit ·
3a43322
1
Parent(s): f0286e6
网盘环节测试
Browse files- app.py +99 -4
- templates/index.html +154 -0
app.py
CHANGED
|
@@ -1,10 +1,105 @@
|
|
| 1 |
-
from flask import Flask, render_template
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
@app.route('/')
|
| 6 |
-
def
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
if __name__ == '__main__':
|
| 10 |
-
app.run(debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory, jsonify
|
| 2 |
+
import os
|
| 3 |
+
from werkzeug.utils import secure_filename
|
| 4 |
+
import json
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
+
app.secret_key = 'your_secret_key_here'
|
| 9 |
+
|
| 10 |
+
# 配置上传文件夹
|
| 11 |
+
UPLOAD_FOLDER = 'uploads'
|
| 12 |
+
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'doc', 'docx', 'zip', 'rar'}
|
| 13 |
+
|
| 14 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 15 |
+
|
| 16 |
+
# 确保上传目录存在
|
| 17 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
def allowed_file(filename):
|
| 20 |
+
return '.' in filename and \
|
| 21 |
+
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 22 |
+
|
| 23 |
+
def get_file_info(filepath):
|
| 24 |
+
"""获取文件信息"""
|
| 25 |
+
stat = os.stat(filepath)
|
| 26 |
+
return {
|
| 27 |
+
'name': os.path.basename(filepath),
|
| 28 |
+
'size': stat.st_size,
|
| 29 |
+
'modified': datetime.fromtimestamp(stat.st_mtime).strftime('%Y-%m-%d %H:%M:%S'),
|
| 30 |
+
'path': filepath
|
| 31 |
+
}
|
| 32 |
|
| 33 |
@app.route('/')
|
| 34 |
+
def index():
|
| 35 |
+
# 获取所有上传的文件
|
| 36 |
+
files = []
|
| 37 |
+
for filename in os.listdir(app.config['UPLOAD_FOLDER']):
|
| 38 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 39 |
+
if os.path.isfile(filepath):
|
| 40 |
+
files.append(get_file_info(filepath))
|
| 41 |
+
|
| 42 |
+
return render_template('index.html', files=files)
|
| 43 |
+
|
| 44 |
+
@app.route('/upload', methods=['POST'])
|
| 45 |
+
def upload_file():
|
| 46 |
+
if 'file' not in request.files:
|
| 47 |
+
flash('没有选择文件')
|
| 48 |
+
return redirect(request.url)
|
| 49 |
+
|
| 50 |
+
file = request.files['file']
|
| 51 |
+
|
| 52 |
+
if file.filename == '':
|
| 53 |
+
flash('没有选择文件')
|
| 54 |
+
return redirect(url_for('index'))
|
| 55 |
+
|
| 56 |
+
if file and allowed_file(file.filename):
|
| 57 |
+
filename = secure_filename(file.filename)
|
| 58 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 59 |
+
|
| 60 |
+
# 如果文件已存在,则添加时间戳避免覆盖
|
| 61 |
+
if os.path.exists(file_path):
|
| 62 |
+
base, ext = os.path.splitext(filename)
|
| 63 |
+
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 64 |
+
filename = f"{base}_{timestamp}{ext}"
|
| 65 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 66 |
+
|
| 67 |
+
file.save(file_path)
|
| 68 |
+
flash(f'文件 {filename} 上传成功!')
|
| 69 |
+
return redirect(url_for('index'))
|
| 70 |
+
else:
|
| 71 |
+
flash('不支持的文件类型')
|
| 72 |
+
return redirect(url_for('index'))
|
| 73 |
+
|
| 74 |
+
@app.route('/download/<filename>')
|
| 75 |
+
def download_file(filename):
|
| 76 |
+
try:
|
| 77 |
+
return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
|
| 78 |
+
except FileNotFoundError:
|
| 79 |
+
flash('文件不存在')
|
| 80 |
+
return redirect(url_for('index'))
|
| 81 |
+
|
| 82 |
+
@app.route('/delete/<filename>')
|
| 83 |
+
def delete_file(filename):
|
| 84 |
+
try:
|
| 85 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 86 |
+
os.remove(file_path)
|
| 87 |
+
flash(f'文件 {filename} 删除成功!')
|
| 88 |
+
except FileNotFoundError:
|
| 89 |
+
flash('文件不存在')
|
| 90 |
+
|
| 91 |
+
return redirect(url_for('index'))
|
| 92 |
+
|
| 93 |
+
@app.route('/list_files')
|
| 94 |
+
def list_files():
|
| 95 |
+
"""API接口:返回所有文件列表"""
|
| 96 |
+
files = []
|
| 97 |
+
for filename in os.listdir(app.config['UPLOAD_FOLDER']):
|
| 98 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 99 |
+
if os.path.isfile(filepath):
|
| 100 |
+
files.append(get_file_info(filepath))
|
| 101 |
+
|
| 102 |
+
return jsonify(files)
|
| 103 |
|
| 104 |
if __name__ == '__main__':
|
| 105 |
+
app.run(debug=True, host='0.0.0.0', port=5000)
|
templates/index.html
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="zh-CN">
|
| 3 |
+
|
| 4 |
+
<head>
|
| 5 |
+
<meta charset="utf-8">
|
| 6 |
+
<title>测试网盘</title>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
font-family: Arial, sans-serif;
|
| 10 |
+
margin: 0;
|
| 11 |
+
padding: 20px;
|
| 12 |
+
background-color: #f5f5f5;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
.container {
|
| 16 |
+
max-width: 1000px;
|
| 17 |
+
margin: 0 auto;
|
| 18 |
+
background-color: white;
|
| 19 |
+
padding: 20px;
|
| 20 |
+
border-radius: 8px;
|
| 21 |
+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
h1 {
|
| 25 |
+
color: #333;
|
| 26 |
+
text-align: center;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
.upload-section {
|
| 30 |
+
background-color: #e8f4fd;
|
| 31 |
+
padding: 20px;
|
| 32 |
+
border-radius: 5px;
|
| 33 |
+
margin-bottom: 20px;
|
| 34 |
+
text-align: center;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
.file-list {
|
| 38 |
+
margin-top: 20px;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
table {
|
| 42 |
+
width: 100%;
|
| 43 |
+
border-collapse: collapse;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
th,
|
| 47 |
+
td {
|
| 48 |
+
padding: 12px;
|
| 49 |
+
text-align: left;
|
| 50 |
+
border-bottom: 1px solid #ddd;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
th {
|
| 54 |
+
background-color: #f2f2f2;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
tr:hover {
|
| 58 |
+
background-color: #f5f5f5;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
.btn {
|
| 62 |
+
padding: 6px 12px;
|
| 63 |
+
margin: 0 5px;
|
| 64 |
+
border: none;
|
| 65 |
+
border-radius: 4px;
|
| 66 |
+
cursor: pointer;
|
| 67 |
+
text-decoration: none;
|
| 68 |
+
display: inline-block;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
.btn-download {
|
| 72 |
+
background-color: #4CAF50;
|
| 73 |
+
color: white;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
.btn-delete {
|
| 77 |
+
background-color: #f44336;
|
| 78 |
+
color: white;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
.flash-messages {
|
| 82 |
+
margin-bottom: 20px;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
.flash-message {
|
| 86 |
+
padding: 10px;
|
| 87 |
+
background-color: #d4edda;
|
| 88 |
+
border: 1px solid #c3e6cb;
|
| 89 |
+
border-radius: 4px;
|
| 90 |
+
color: #155724;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
.file-info {
|
| 94 |
+
color: #666;
|
| 95 |
+
font-size: 0.9em;
|
| 96 |
+
}
|
| 97 |
+
</style>
|
| 98 |
+
</head>
|
| 99 |
+
|
| 100 |
+
<body>
|
| 101 |
+
<div class="container">
|
| 102 |
+
<h1>Flask网盘系统</h1>
|
| 103 |
+
|
| 104 |
+
{% with messages = get_flashed_messages() %} {% if messages %}
|
| 105 |
+
<div class="flash-messages">
|
| 106 |
+
{% for message in messages %}
|
| 107 |
+
<div class="flash-message">{{ message }}</div>
|
| 108 |
+
{% endfor %}
|
| 109 |
+
</div>
|
| 110 |
+
{% endif %} {% endwith %}
|
| 111 |
+
|
| 112 |
+
<div class="upload-section">
|
| 113 |
+
<h2>上传文件</h2>
|
| 114 |
+
<form action="{{ url_for('upload_file') }}" method="post" enctype="multipart/form-data">
|
| 115 |
+
<input type="file" name="file" required>
|
| 116 |
+
<button type="submit" class="btn" style="background-color: #007bff; color: white;">上传文件</button>
|
| 117 |
+
</form>
|
| 118 |
+
<p class="file-info">支持的文件类型: txt, pdf, png, jpg, jpeg, gif, doc, docx, zip, rar</p>
|
| 119 |
+
</div>
|
| 120 |
+
|
| 121 |
+
<div class="file-list">
|
| 122 |
+
<h2>文件列表</h2>
|
| 123 |
+
{% if files %}
|
| 124 |
+
<table>
|
| 125 |
+
<thead>
|
| 126 |
+
<tr>
|
| 127 |
+
<th>文件名</th>
|
| 128 |
+
<th>大小</th>
|
| 129 |
+
<th>修改时间</th>
|
| 130 |
+
<th>操作</th>
|
| 131 |
+
</tr>
|
| 132 |
+
</thead>
|
| 133 |
+
<tbody>
|
| 134 |
+
{% for file in files %}
|
| 135 |
+
<tr>
|
| 136 |
+
<td>{{ file.name }}</td>
|
| 137 |
+
<td>{{ "%.2f"|format(file.size/1024) }} KB</td>
|
| 138 |
+
<td>{{ file.modified }}</td>
|
| 139 |
+
<td>
|
| 140 |
+
<a href="{{ url_for('download_file', filename=file.name) }}" class="btn btn-download">下载</a>
|
| 141 |
+
<a href="{{ url_for('delete_file', filename=file.name) }}" class="btn btn-delete" onclick="return confirm('确定要删除这个文件吗?')">删除</a>
|
| 142 |
+
</td>
|
| 143 |
+
</tr>
|
| 144 |
+
{% endfor %}
|
| 145 |
+
</tbody>
|
| 146 |
+
</table>
|
| 147 |
+
{% else %}
|
| 148 |
+
<p>暂无文件</p>
|
| 149 |
+
{% endif %}
|
| 150 |
+
</div>
|
| 151 |
+
</div>
|
| 152 |
+
</body>
|
| 153 |
+
|
| 154 |
+
</html>
|