aorus
This commit is contained in:
@@ -10,6 +10,7 @@ from sqlmodel import Session, select
|
||||
from app.config import UPLOAD_DIR
|
||||
from app.database import get_session
|
||||
from app.models.media import Attachment, Media
|
||||
from app.models.settings import UserSettings
|
||||
|
||||
|
||||
class MediaPatch(BaseModel):
|
||||
@@ -102,6 +103,20 @@ def _canonicalize_rows(rows: List[Media], session: Session) -> None:
|
||||
session.commit()
|
||||
|
||||
|
||||
try:
|
||||
import pillow_heif
|
||||
pillow_heif.register_heif_opener()
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
def _is_heic(content_type: str, filename: str) -> bool:
|
||||
if content_type.lower() in ("image/heic", "image/heif"):
|
||||
return True
|
||||
fn = (filename or "").lower()
|
||||
return fn.endswith(".heic") or fn.endswith(".heif")
|
||||
|
||||
|
||||
def _save_webp(data: bytes, max_px: int) -> str:
|
||||
try:
|
||||
from PIL import Image
|
||||
@@ -122,12 +137,28 @@ def _save_webp(data: bytes, max_px: int) -> str:
|
||||
|
||||
|
||||
@router.post("/upload")
|
||||
async def upload_file(file: UploadFile = File(...)):
|
||||
async def upload_file(
|
||||
file: UploadFile = File(...),
|
||||
session: Session = Depends(get_session),
|
||||
):
|
||||
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
||||
data = await file.read()
|
||||
ct = file.content_type or ""
|
||||
if ct.startswith("image/"):
|
||||
name = _save_webp(data, 1200)
|
||||
|
||||
# Lire la largeur max configurée (défaut 1200, 0 = taille originale)
|
||||
setting = session.exec(select(UserSettings).where(UserSettings.cle == "image_max_width")).first()
|
||||
max_px = 1200
|
||||
if setting:
|
||||
try:
|
||||
max_px = int(setting.valeur)
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
if max_px <= 0:
|
||||
max_px = 99999
|
||||
|
||||
heic = _is_heic(ct, file.filename or "")
|
||||
if heic or ct.startswith("image/"):
|
||||
name = _save_webp(data, max_px)
|
||||
thumb = _save_webp(data, 300)
|
||||
return {"url": f"/uploads/{name}", "thumbnail_url": f"/uploads/{thumb}"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user