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.
115 lines
4.5 KiB
115 lines
4.5 KiB
from flask import Flask, request, jsonify, render_template, send_from_directory
|
|
import os
|
|
|
|
# 默认配置
|
|
DEFAULT_UPLOAD_FOLDER = "/Users/zgz/Documents/images"
|
|
#DEFAULT_UPLOAD_FOLDER = "/mnt/sese"
|
|
|
|
ALLOWED_EXTENSIONS = {
|
|
'png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'webp','heic', # 图片类型
|
|
'mp4', 'mov', 'wmv', 'avi', 'm4v', 'mpg', 'mpeg', 'flv', 'mkv', '3gp', 'webm' # 视频类型
|
|
}
|
|
MAX_FILE_SIZE = 88 * 1024 * 1024 # 88MB
|
|
app = Flask(__name__, static_folder=None)
|
|
#app = Flask(__name__, static_folder=DEFAULT_UPLOAD_FOLDER, static_url_path="/files")
|
|
app.config['UPLOAD_FOLDER'] = DEFAULT_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
|
|
|
|
|
|
def list_files(directory):
|
|
"""列出指定目录下的所有文件并按时间排序"""
|
|
if not os.path.exists(directory):
|
|
return []
|
|
files = []
|
|
for root, _, filenames in os.walk(directory):
|
|
for filename in filenames:
|
|
files.append(os.path.relpath(os.path.join(root, filename), directory))
|
|
files.sort(key=lambda x: os.path.getmtime(os.path.join(directory, x)), reverse=True) # 按时间降序排序
|
|
return files
|
|
|
|
@app.route('/delete', methods=['DELETE'])
|
|
def delete_file():
|
|
# 获取当前选择的路径,默认路径为 DEFAULT_UPLOAD_FOLDER
|
|
upload_path = request.args.get('path', DEFAULT_UPLOAD_FOLDER)
|
|
|
|
file_name = request.args.get('file')
|
|
if not file_name:
|
|
return jsonify({'status': 'error', 'message': 'File name is required'}), 400
|
|
|
|
file_path = os.path.join(upload_path, file_name)
|
|
if not os.path.exists(file_path):
|
|
return jsonify({'status': 'error', 'message': 'File not found'}), 404
|
|
|
|
try:
|
|
os.remove(file_path)
|
|
return jsonify({'status': 'success', 'message': f'File {file_name} deleted successfully'})
|
|
except Exception as e:
|
|
return jsonify({'status': 'error', 'message': f'Failed to delete file: {str(e)}'}), 500
|
|
|
|
@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
|
|
|
|
# 获取用户选择的上传路径,默认为 DEFAULT_UPLOAD_FOLDER
|
|
upload_path = request.form.get('uploadPath', DEFAULT_UPLOAD_FOLDER)
|
|
|
|
# 检查路径是否合法
|
|
allowed_paths = ['/mnt/sese', '/mnt/self', '/mnt/temp', '/Users/zgz/Documents/images', '/Users/zgz/Documents/images/share']
|
|
if upload_path not in allowed_paths:
|
|
return jsonify({'status': 'error', 'message': 'Invalid upload path'}), 400
|
|
|
|
# 检查文件类型并保存
|
|
if file and allowed_file(file.filename):
|
|
save_dir = os.path.abspath(upload_path)
|
|
if not os.path.exists(save_dir):
|
|
os.makedirs(save_dir)
|
|
|
|
filepath = os.path.join(save_dir, file.filename)
|
|
file.save(filepath)
|
|
|
|
uploaded_files = list_files(save_dir) # 列举上传路径中的文件
|
|
return jsonify({'status': 'success', 'message': f'File uploaded to {filepath}',
|
|
'files': uploaded_files}), 200
|
|
|
|
return jsonify({'status': 'error', 'message': 'File type not allowed'}), 400
|
|
|
|
# GET 请求返回 HTML 页面
|
|
files = list_files(DEFAULT_UPLOAD_FOLDER)
|
|
return render_template('index.html', files=files[:10], total_files=len(files))
|
|
|
|
|
|
@app.route('/files', methods=['GET'])
|
|
def get_files():
|
|
# 获取当前选择的路径,默认路径为 DEFAULT_UPLOAD_FOLDER
|
|
upload_path = request.args.get('path', DEFAULT_UPLOAD_FOLDER)
|
|
|
|
# 获取该路径下的文件列表
|
|
files = list_files(upload_path)
|
|
return jsonify(files=files)
|
|
|
|
@app.route('/static/<path:filename>')
|
|
def serve_dynamic_static(filename):
|
|
directory = request.args.get('path', '/mnt/sese')
|
|
full_path = os.path.join(directory, filename)
|
|
print(f"Serving file from directory: {directory}, file: {filename}")
|
|
print(f"Full path: {full_path}")
|
|
try:
|
|
return send_from_directory(directory, filename)
|
|
except FileNotFoundError:
|
|
return jsonify({"error": "File not found", "directory": directory, "filename": filename}), 404
|
|
|
|
if __name__ == '__main__':
|
|
if not os.path.exists(DEFAULT_UPLOAD_FOLDER):
|
|
os.makedirs(DEFAULT_UPLOAD_FOLDER)
|
|
app.run(host='0.0.0.0', port=8066) |