From 12c66a9eb48b509078e50a73071a38061fb06517 Mon Sep 17 00:00:00 2001 From: Franklin-F Date: Fri, 6 Jun 2025 23:00:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89404=E5=92=8C403=E9=94=99=E8=AF=AF=E5=93=8D=E5=BA=94?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E4=BC=98=E5=8C=96=E5=9B=BE=E5=83=8F=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=8E=A5=E5=8F=A3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- screenshots_flask.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/screenshots_flask.py b/screenshots_flask.py index 88adc77..2e0ee83 100644 --- a/screenshots_flask.py +++ b/screenshots_flask.py @@ -1,35 +1,35 @@ -from flask import Flask, send_file, abort +from flask import Flask, send_file, abort, request, jsonify from pathlib import Path app = Flask(__name__) -# 固定 screenshots 目录 PROJECT_ROOT = Path(__file__).parent.resolve() SCREENSHOTS_DIR = PROJECT_ROOT / "screenshots" - @app.route('/image/') def serve_image(filename): - app = Flask(__name__) + file_path = SCREENSHOTS_DIR / filename - PROJECT_ROOT = Path(__file__).parent.resolve() - SCREENSHOTS_DIR = PROJECT_ROOT / "screenshots" + # 防止路径越界访问 + try: + file_path.resolve().relative_to(SCREENSHOTS_DIR.resolve()) + except ValueError: + abort(403, description=f"禁止访问目录外文件: {file_path.resolve()}") - @app.route('/image/') - def serve_image(filename): - file_path = SCREENSHOTS_DIR / filename + if not file_path.exists(): + abort(404, description=f"文件不存在: {file_path.resolve()}") - # 防止路径越界访问 - try: - file_path.resolve().relative_to(SCREENSHOTS_DIR.resolve()) - except ValueError: - abort(403, description=f"禁止访问目录外文件: {file_path.resolve()}") + return send_file(file_path, as_attachment=False) - if not file_path.exists(): - abort(404, description=f"文件不存在: {file_path.resolve()}") - - return send_file(file_path, as_attachment=False) +# 自定义 404 错误响应 +@app.errorhandler(404) +def handle_404(e): + return f"404 错误:{e.description}", 404 +# 自定义 403 错误响应 +@app.errorhandler(403) +def handle_403(e): + return f"403 错误:{e.description}", 403 if __name__ == '__main__': - app.run(host='0.0.0.0', debug=True, port=5000) + app.run(host='0.0.0.0', debug=False, port=5000) \ No newline at end of file