From 36603467971d5ad4cc431f5fbf5298c10d571e85 Mon Sep 17 00:00:00 2001 From: LeonspaceX Date: Sat, 31 Jan 2026 17:31:53 +0800 Subject: [PATCH] Replace imghdr with header check --- back/main.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/back/main.py b/back/main.py index c33b4ee..5d81a00 100644 --- a/back/main.py +++ b/back/main.py @@ -7,7 +7,6 @@ from sqlalchemy.orm import foreign import os import uuid import json -import imghdr from datetime import datetime app = Flask(__name__) @@ -211,6 +210,17 @@ def save_hashtags(tag_type, target_id, hashtopic): name=tag ) db.session.add(new_tag) + +def detect_image_type(header: bytes): + if header.startswith(b"\xFF\xD8\xFF"): + return "jpeg" + if header.startswith(b"\x89PNG\r\n\x1a\n"): + return "png" + if header.startswith(b"GIF87a") or header.startswith(b"GIF89a"): + return "gif" + if header.startswith(b"RIFF") and len(header) >= 12 and header[8:12] == b"WEBP": + return "webp" + return None # --- 用户普通api端点 --- @app.route('/api/settings', methods=['GET']) @@ -507,7 +517,7 @@ def upload_pic(): header = file.read(512) file.seek(0) - detected = imghdr.what(None, header) + detected = detect_image_type(header) ext_map = { 'jpg': 'jpeg', 'jpeg': 'jpeg',