Fix: HEIC/MOV-Konvertierung bei Partner-Uploads
Logo-Pfad akzeptierte .heic, öffnete aber direkt mit Pillow (kein HEIF-Opener) — iPhone-Fotos schlugen fehl. Jetzt convert_media-Vorstufe wie im Foto-Pfad. Fehlgeschlagene Konvertierungen (HEIC→JPEG, MOV→MP4) brechen mit klarer Meldung ab statt rohe Dateien zu speichern (MOV wäre als <img> kaputt gerendert). Test: echter HEIC-Roundtrip (pillow-heif) für Logo + Foto.
This commit is contained in:
parent
21f54f478b
commit
8a614eef1a
2 changed files with 36 additions and 2 deletions
|
|
@ -329,17 +329,22 @@ async def upload_partner_logo(file: UploadFile = File(...), user=Depends(require
|
||||||
new_name = f"logo_{uuid.uuid4().hex[:8]}.webp"
|
new_name = f"logo_{uuid.uuid4().hex[:8]}.webp"
|
||||||
new_path = os.path.join(save_dir, new_name)
|
new_path = os.path.join(save_dir, new_name)
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
# HEIC/HEIF (iPhone) zuerst nach JPEG wandeln — Pillow kann HEIC nicht ohne Opener
|
||||||
|
data, ext = await loop.run_in_executor(None, lambda: convert_media(raw, filename))
|
||||||
|
if ext in (".heic", ".heif"):
|
||||||
|
raise HTTPException(400, "HEIC-Bild konnte nicht konvertiert werden. Bitte als JPG/PNG exportieren.")
|
||||||
|
|
||||||
def _save():
|
def _save():
|
||||||
import io
|
import io
|
||||||
from PIL import Image, ImageOps
|
from PIL import Image, ImageOps
|
||||||
img = Image.open(io.BytesIO(raw))
|
img = Image.open(io.BytesIO(data))
|
||||||
img = ImageOps.exif_transpose(img)
|
img = ImageOps.exif_transpose(img)
|
||||||
# Transparenz erhalten (Logos sind oft PNG mit Alpha)
|
# Transparenz erhalten (Logos sind oft PNG mit Alpha)
|
||||||
img = img.convert("RGBA" if "A" in (img.mode or "") or img.mode == "P" else "RGB")
|
img = img.convert("RGBA" if "A" in (img.mode or "") or img.mode == "P" else "RGB")
|
||||||
img.thumbnail((512, 512))
|
img.thumbnail((512, 512))
|
||||||
img.save(new_path, format="WEBP", quality=85)
|
img.save(new_path, format="WEBP", quality=85)
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
try:
|
try:
|
||||||
await loop.run_in_executor(None, _save)
|
await loop.run_in_executor(None, _save)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|
@ -394,6 +399,11 @@ async def upload_partner_photo(file: UploadFile = File(...), user=Depends(requir
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
# HEIC→JPEG bzw. MOV/AVI→MP4 (ffmpeg, komprimiert) — blockierend, daher Threadpool
|
# HEIC→JPEG bzw. MOV/AVI→MP4 (ffmpeg, komprimiert) — blockierend, daher Threadpool
|
||||||
data, ext = await loop.run_in_executor(None, lambda: convert_media(raw, filename))
|
data, ext = await loop.run_in_executor(None, lambda: convert_media(raw, filename))
|
||||||
|
if ext in (".heic", ".heif"):
|
||||||
|
raise HTTPException(400, "HEIC-Bild konnte nicht konvertiert werden. Bitte als JPG/PNG exportieren.")
|
||||||
|
if ext in (".mov", ".avi", ".m4v"):
|
||||||
|
# ffmpeg-Konvertierung fehlgeschlagen — unkonvertiert wäre es im Browser nicht abspielbar
|
||||||
|
raise HTTPException(400, "Video konnte nicht konvertiert werden. Bitte als MP4 hochladen.")
|
||||||
|
|
||||||
save_dir = _pp_dir(user["id"])
|
save_dir = _pp_dir(user["id"])
|
||||||
file_id = uuid.uuid4().hex[:12]
|
file_id = uuid.uuid4().hex[:12]
|
||||||
|
|
|
||||||
|
|
@ -112,6 +112,30 @@ def test_logo_and_photo_upload(client, user):
|
||||||
assert r.json()["photos"] == []
|
assert r.json()["photos"] == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_heic_uploads_convert(client, user):
|
||||||
|
"""HEIC (iPhone-Format) wird bei Logo UND Foto nach WebP konvertiert."""
|
||||||
|
import pillow_heif
|
||||||
|
from PIL import Image
|
||||||
|
_make_partner(user["email"])
|
||||||
|
|
||||||
|
pillow_heif.register_heif_opener()
|
||||||
|
buf = io.BytesIO()
|
||||||
|
Image.new("RGB", (64, 64), "green").save(buf, format="HEIF")
|
||||||
|
heic_bytes = buf.getvalue()
|
||||||
|
|
||||||
|
# Logo als HEIC
|
||||||
|
r = client.post("/api/partner/my-profile/logo", headers=user["headers"],
|
||||||
|
files={"file": ("IMG_0001.HEIC", io.BytesIO(heic_bytes), "image/heic")})
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
assert r.json()["logo_url"].endswith(".webp")
|
||||||
|
|
||||||
|
# Foto als HEIC
|
||||||
|
r = client.post("/api/partner/my-profile/photos", headers=user["headers"],
|
||||||
|
files={"file": ("IMG_0002.heic", io.BytesIO(heic_bytes), "image/heic")})
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
assert r.json()["photos"][0].endswith(".webp")
|
||||||
|
|
||||||
|
|
||||||
def test_partner_has_pro_access(client, user):
|
def test_partner_has_pro_access(client, user):
|
||||||
"""is_partner=1 -> has_pro_access True (Pro gratis fuer Partner)."""
|
"""is_partner=1 -> has_pro_access True (Pro gratis fuer Partner)."""
|
||||||
from auth import has_pro_access
|
from auth import has_pro_access
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue