You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
4.1 KiB

from flask import Flask, request, jsonify
import os
# 配置上传目录和文件限制
UPLOAD_FOLDER = '/mnt/sese'
ALLOWED_EXTENSIONS = {
'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'md', 'doc', 'docx','mp4','mov','wmv','avi','m4v','mpg',
'xlsx', 'xls', 'rar', 'zip', 'java', 'sql', 'py', 'css','conf','sql','properties','yaml','html','htm','jsp','js','json','yml'
}
MAX_FILE_SIZE = 88 * 1024 * 1024 # 88MB
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = MAX_FILE_SIZE
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
return jsonify({'status': 'error', 'message': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'status': 'error', 'message': 'No selected file'}), 400
if file and allowed_file(file.filename):
filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(filepath)
return jsonify({'status': 'success', 'message': 'File uploaded successfully'}), 200
return jsonify({'status': 'error', 'message': 'File type not allowed'}), 400
# GET 请求返回 HTML 页面
files = os.listdir(app.config['UPLOAD_FOLDER'])
files_list_html = ''.join(
f'<li><a href="/temp/{file}" target="_blank">{file}</a></li>'
for file in files
)
html_template = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
<script>
function uploadFile(event) {{
event.preventDefault();
const formData = new FormData(document.getElementById('uploadForm'));
const uploadButton = document.getElementById('uploadButton');
const progressIndicator = document.getElementById('progressIndicator');
const messageBox = document.getElementById('messageBox');
// 禁用按钮,显示上传中提示
uploadButton.disabled = true;
progressIndicator.style.display = 'inline-block';
fetch('/upload', {{
method: 'POST',
body: formData
}})
.then(response => response.json())
.then(data => {{
progressIndicator.style.display = 'none'; // 隐藏进度指示
uploadButton.disabled = false; // 启用按钮
if (data.status === 'success') {{
messageBox.innerHTML = '<p style="color:green;">' + data.message + '</p>';
location.reload(); // 刷新页面更新文件列表
}} else {{
messageBox.innerHTML = '<p style="color:red;">' + data.message + '</p>';
}}
}})
.catch(error => {{
progressIndicator.style.display = 'none'; // 隐藏进度指示
uploadButton.disabled = false; // 启用按钮
messageBox.innerHTML = '<p style="color:red;">Upload failed</p>';
}});
}}
</script>
</head>
<body>
<h1>Upload a file</h1>
<form id="uploadForm" onsubmit="uploadFile(event)">
<input type="file" name="file" required>
<button id="uploadButton" type="submit">Upload</button>
<span id="progressIndicator" style="display:none;">Uploading...</span>
</form>
<div id="messageBox"></div>
<h2>Uploaded list(注意:每日零晨清空列表):</h2>
<ul>
{files_list_html}
</ul>
</body>
</html>
"""
return html_template
if __name__ == '__main__':
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
app.run(host='0.0.0.0', port=8066)