Spaces:
Runtime error
Runtime error
| from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory, jsonify | |
| import os | |
| from werkzeug.utils import secure_filename | |
| import json | |
| from datetime import datetime | |
| app = Flask(__name__) | |
| app.secret_key = 'your_secret_key_here' | |
| # 配置上传文件夹 | |
| UPLOAD_FOLDER = 'uploads' | |
| ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'doc', 'docx', 'zip', 'rar'} | |
| app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
| # 确保上传目录存在 | |
| os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
| def allowed_file(filename): | |
| return '.' in filename and \ | |
| filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS | |
| def get_file_info(filepath): | |
| """获取文件信息""" | |
| stat = os.stat(filepath) | |
| return { | |
| 'name': os.path.basename(filepath), | |
| 'size': stat.st_size, | |
| 'modified': datetime.fromtimestamp(stat.st_mtime).strftime('%Y-%m-%d %H:%M:%S'), | |
| 'path': filepath | |
| } | |
| def index(): | |
| # 获取所有上传的文件 | |
| files = [] | |
| for filename in os.listdir(app.config['UPLOAD_FOLDER']): | |
| filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
| if os.path.isfile(filepath): | |
| files.append(get_file_info(filepath)) | |
| return render_template('index.html', files=files) | |
| def upload_file(): | |
| if 'file' not in request.files: | |
| flash('没有选择文件') | |
| return redirect(request.url) | |
| file = request.files['file'] | |
| if file.filename == '': | |
| flash('没有选择文件') | |
| return redirect(url_for('index')) | |
| if file and allowed_file(file.filename): | |
| filename = secure_filename(file.filename) | |
| file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
| # 如果文件已存在,则添加时间戳避免覆盖 | |
| if os.path.exists(file_path): | |
| base, ext = os.path.splitext(filename) | |
| timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') | |
| filename = f"{base}_{timestamp}{ext}" | |
| file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
| file.save(file_path) | |
| flash(f'文件 {filename} 上传成功!') | |
| return redirect(url_for('index')) | |
| else: | |
| flash('不支持的文件类型') | |
| return redirect(url_for('index')) | |
| def download_file(filename): | |
| try: | |
| return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True) | |
| except FileNotFoundError: | |
| flash('文件不存在') | |
| return redirect(url_for('index')) | |
| def delete_file(filename): | |
| try: | |
| file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
| os.remove(file_path) | |
| flash(f'文件 {filename} 删除成功!') | |
| except FileNotFoundError: | |
| flash('文件不存在') | |
| return redirect(url_for('index')) | |
| def list_files(): | |
| """API接口:返回所有文件列表""" | |
| files = [] | |
| for filename in os.listdir(app.config['UPLOAD_FOLDER']): | |
| filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
| if os.path.isfile(filepath): | |
| files.append(get_file_info(filepath)) | |
| return jsonify(files) | |
| # 创建一个应用实例供gunicorn使用 | |
| application = app | |
| if __name__ == '__main__': | |
| app.run(debug=True, host='0.0.0.0', port=5000) |