This commit is contained in:
咕谷酱
2025-08-22 14:03:00 +08:00
11 changed files with 532 additions and 63 deletions

View File

@@ -1,6 +1,10 @@
from __future__ import annotations
from datetime import datetime
from io import BytesIO
from fastapi import HTTPException
from PIL import Image
def unix_timestamp_to_windows(timestamp: int) -> int:
@@ -126,6 +130,7 @@ def truncate(text: str, limit: int = 100, ellipsis: str = "...") -> str:
return text
def parse_user_agent(user_agent: str | None, max_length: int = 255) -> str | None:
"""
解析用户代理字符串,提取关键信息:设备、系统、浏览器
@@ -181,3 +186,20 @@ def parse_user_agent(user_agent: str | None, max_length: int = 255) -> str | Non
# 如果无法解析,则截断原始字符串
return truncate(user_agent, max_length - 3, "...")
def check_image(content: bytes, size: int, width: int, height: int) -> None:
if len(content) > size: # 10MB limit
raise HTTPException(status_code=400, detail="File size exceeds 10MB limit")
elif len(content) == 0:
raise HTTPException(status_code=400, detail="File cannot be empty")
try:
with Image.open(BytesIO(content)) as img:
if img.format not in ["PNG", "JPEG", "GIF"]:
raise HTTPException(status_code=400, detail="Invalid image format")
if img.size[0] > width or img.size[1] > height:
raise HTTPException(
status_code=400,
detail=f"Image size exceeds {width}x{height} pixels",
)
except Exception as e:
raise HTTPException(status_code=400, detail=f"Error processing image: {e}")