Replace imghdr with header check
This commit is contained in:
14
back/main.py
14
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',
|
||||
|
||||
Reference in New Issue
Block a user