46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
ALLOWED_MIME_TYPES = {
|
|
"application/pdf",
|
|
"text/plain",
|
|
"text/csv",
|
|
"image/jpeg",
|
|
"image/png",
|
|
"image/gif",
|
|
"image/webp",
|
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
"application/msword",
|
|
"application/vnd.ms-excel",
|
|
}
|
|
|
|
BLOCKED_EXTENSIONS = {
|
|
".exe",
|
|
".dll",
|
|
".bat",
|
|
".cmd",
|
|
".ps1",
|
|
".vbs",
|
|
".js",
|
|
".scr",
|
|
".pif",
|
|
".msi",
|
|
".com",
|
|
}
|
|
|
|
MAX_TOTAL_SIZE = 25 * 1024 * 1024
|
|
MAX_PER_ATTACHMENT = 10 * 1024 * 1024
|
|
MAX_TEXT_FOR_AI = 200 * 1024
|
|
|
|
|
|
def is_attachment_safe(filename: str, content_type: str, size: int) -> tuple[bool, str]:
|
|
ext = os.path.splitext(filename)[1].lower()
|
|
if ext in BLOCKED_EXTENSIONS:
|
|
return False, f"禁止的文件类型: {ext}"
|
|
if size > MAX_PER_ATTACHMENT:
|
|
return False, f"附件过大 ({size} bytes > {MAX_PER_ATTACHMENT})"
|
|
if content_type not in ALLOWED_MIME_TYPES:
|
|
return False, f"不支持的 MIME 类型: {content_type}"
|
|
return True, "ok" |