Medien: library-Pfade nach Medientyp (image/video/pdf/other) unterteilen

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-05-08 10:03:00 +02:00
1 parent 8d85649cf8
commit 3e4fa0dc39
6 files changed
+89 -20

No files matched your search

+47 -6
View File
@@ -2,7 +2,6 @@
from __future__ import annotations
import os
import re
import shutil
from pathlib import Path
from typing import Any, Optional
@@ -45,6 +44,44 @@ def get_effective_media_root(cur: Any) -> Path:
return (base / rel).resolve()
def library_media_kind_dir(mime_type: Optional[str], ext: str) -> str:
"""
Unterordner unter library/* — an API-Filter media_kind angelehnt (image/video/pdf/other).
Bei fehlendem MIME wird anhand der Dateiendung geraten (letzte Instanz: other).
"""
m = (mime_type or "").strip().lower()
x = (ext or "").strip().lower()
if x and not x.startswith("."):
x = "." + x
if m.startswith("image/"):
return "image"
if m.startswith("video/"):
return "video"
if m == "application/pdf" or (m and "pdf" in m):
return "pdf"
if x in (
".jpg",
".jpeg",
".png",
".gif",
".webp",
".heic",
".heif",
".bmp",
".svg",
".avif",
):
return "image"
if x in (".mp4", ".mov", ".webm", ".mkv", ".avi", ".m4v"):
return "video"
if x == ".pdf":
return "pdf"
return "other"
def path_under_media_root(media_root: Path, storage_key: str) -> Optional[Path]:
"""Gibt absoluten Pfad zurück oder None bei Path-Traversal."""
key = (storage_key or "").strip().replace("\\", "/").lstrip("/")
@@ -65,13 +102,16 @@ def library_storage_key(
ext: str,
*,
uploader_profile_id: Optional[int] = None,
mime_type: Optional[str] = None,
) -> str:
"""
Relativer Speicherpfad unter MEDIA_ROOT für lokale media_assets.
- official → library/official/{sha256}{ext}
- club (vereinsgeteilt) → library/club/c{club_id}/{sha256}{ext}
- private (nur Hochlader, Governance unverändert) → library/club/c{club_id}/u{profile}/{sha256}{ext}
- official → library/official/{kind}/{sha256}{ext}
- club (vereinsgeteilt) → library/club/c{club_id}/{kind}/{sha256}{ext}
- private (nur Hochlader, Governance unverändert) → library/club/c{club_id}/u{profile}/{kind}/{sha256}{ext}
kind ∈ {image, video, pdf, other} — siehe library_media_kind_dir.
Kein Ordnername „private“ auf der Platte: Zuordnung erfolgt über Uploader unter dem Verein.
"""
@@ -88,11 +128,12 @@ def library_storage_key(
e = ".bin"
if not e.startswith("."):
e = "." + e
e = e[:16]
if ".." in e or "/" in e or "\\" in e or "\x00" in e:
raise ValueError("Ungültige Dateiendung")
blob = f"{sha}{e}"
kind = library_media_kind_dir(mime_type, e)
e = e[:16]
blob = f"{kind}/{sha}{e}"
if vis == "official":
return f"library/official/{blob}"
if club_id is None: