Eigenes Logo und Favicon je Installation
Beides ist optional und wird ueber Einstellungen -> Darstellung gesetzt. Ohne eigenes Bild bleibt alles wie bisher. Ablage bewusst in der Datenbank (Tabelle branding_assets) und nicht im Dateisystem: So liegt das Bild automatisch im Backup, und das Deployment braucht kein zusaetzliches Volume. Es geht um wenige Kilobyte; der Upload ist auf 512 KB und auf Bildformate begrenzt, die ein Browser auch wirklich darstellt. Das Abrufen ist ohne Anmeldung moeglich, weil der Browser das Favicon schon vor dem Login holt - hochladen und entfernen duerfen nur Administratoren. Das Logo ersetzt in der Seitenleiste und auf der Anmeldeseite Zeichen und Schriftzug zusammen. Damit ein zu grosses oder sehr breites Bild das Layout nicht auseinanderziehen kann, ist die Hoehe per CSS gedeckelt und die Breite auf den Container begrenzt; object-fit haelt das Seitenverhaeltnis. Faellt das Laden fehl, erscheint wieder das eingebaute Zeichen. Als Standard-Favicon dient derselbe Barcode-Glyph wie im App-Icon, damit Web und iOS-App zusammenpassen. Getestet: Die Endpunkte sind gegen die laufende API geprueft - hochladen, abrufen ohne Anmeldung, Abweisen von falschem Dateityp (400), zu grosser Datei (400), unbekannter Bildart (404) und fehlenden Rechten (401), entfernen und der Rueckfall auf 404 danach. Web-Build laeuft durch, 40 pytest-Tests gruen. Die Darstellung im Browser habe ich nicht selbst angesehen. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ from .database import Base, SessionLocal, engine
|
||||
from .routers import (
|
||||
api_tokens,
|
||||
auth,
|
||||
branding,
|
||||
groups,
|
||||
locations,
|
||||
products,
|
||||
@@ -98,3 +99,4 @@ app.include_router(views.router)
|
||||
app.include_router(transfer.router)
|
||||
app.include_router(api_tokens.router)
|
||||
app.include_router(settings_router.router)
|
||||
app.include_router(branding.router)
|
||||
|
||||
@@ -11,6 +11,7 @@ from sqlalchemy import (
|
||||
Float,
|
||||
ForeignKey,
|
||||
Integer,
|
||||
LargeBinary,
|
||||
String,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
@@ -248,6 +249,24 @@ class Movement(Base):
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now)
|
||||
|
||||
|
||||
class BrandingAsset(Base):
|
||||
"""Eigenes Logo bzw. Favicon der Installation.
|
||||
|
||||
Bewusst in der Datenbank und nicht im Dateisystem: So landet das Bild
|
||||
automatisch im Backup und das Deployment braucht kein zusätzliches Volume.
|
||||
Es geht um wenige Kilobyte, die Größe ist beim Upload begrenzt.
|
||||
"""
|
||||
|
||||
__tablename__ = "branding_assets"
|
||||
|
||||
kind: Mapped[str] = mapped_column(String(16), primary_key=True) # "logo" | "favicon"
|
||||
content_type: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
data: Mapped[bytes] = mapped_column(LargeBinary, nullable=False)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=_now, onupdate=_now
|
||||
)
|
||||
|
||||
|
||||
class Setting(Base):
|
||||
__tablename__ = "settings"
|
||||
|
||||
|
||||
113
backend/app/routers/branding.py
Normal file
113
backend/app/routers/branding.py
Normal file
@@ -0,0 +1,113 @@
|
||||
"""Eigenes Logo und Favicon der Installation.
|
||||
|
||||
Die Bilder liegen in der Datenbank (siehe models.BrandingAsset). Das Abrufen ist
|
||||
bewusst ohne Anmeldung möglich – das Favicon wird vom Browser schon vor dem
|
||||
Login geladen, und es handelt sich um nichts Schützenswertes. Hochladen und
|
||||
Entfernen dürfen nur Administratoren.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status
|
||||
from fastapi.responses import Response
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
from ..deps import require_admin
|
||||
from ..models import BrandingAsset, User
|
||||
|
||||
router = APIRouter(prefix="/branding", tags=["branding"])
|
||||
|
||||
KINDS = ("logo", "favicon")
|
||||
|
||||
# Bewusst eng: genau die Formate, die ein Browser als Logo/Favicon darstellt.
|
||||
ALLOWED_TYPES = {
|
||||
"image/png": "png",
|
||||
"image/jpeg": "jpg",
|
||||
"image/svg+xml": "svg",
|
||||
"image/webp": "webp",
|
||||
"image/x-icon": "ico",
|
||||
"image/vnd.microsoft.icon": "ico",
|
||||
"image/gif": "gif",
|
||||
}
|
||||
|
||||
MAX_BYTES = 512 * 1024 # 512 KB reichen für ein Logo deutlich aus
|
||||
|
||||
|
||||
def _check_kind(kind: str) -> str:
|
||||
if kind not in KINDS:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Unbekannte Bildart")
|
||||
return kind
|
||||
|
||||
|
||||
@router.get("/{kind}")
|
||||
def get_branding(kind: str, db: Session = Depends(get_db)) -> Response:
|
||||
"""Liefert das hinterlegte Bild – oder 404, wenn keines gesetzt ist.
|
||||
|
||||
Die Oberfläche fällt bei 404 auf ihr eingebautes Zeichen zurück.
|
||||
"""
|
||||
_check_kind(kind)
|
||||
asset = db.get(BrandingAsset, kind)
|
||||
if asset is None:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Kein eigenes Bild hinterlegt")
|
||||
return Response(
|
||||
content=asset.data,
|
||||
media_type=asset.content_type,
|
||||
headers={
|
||||
# Kurz cachen, damit ein Wechsel schnell sichtbar wird.
|
||||
"Cache-Control": "public, max-age=60",
|
||||
"ETag": f'"{kind}-{int(asset.updated_at.timestamp())}"',
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.put("/{kind}")
|
||||
async def put_branding(
|
||||
kind: str,
|
||||
file: UploadFile = File(...),
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_admin),
|
||||
) -> Response:
|
||||
_check_kind(kind)
|
||||
|
||||
if file.content_type not in ALLOWED_TYPES:
|
||||
erlaubt = ", ".join(sorted(set(ALLOWED_TYPES.values())))
|
||||
raise HTTPException(
|
||||
status.HTTP_400_BAD_REQUEST,
|
||||
f"Dieses Dateiformat wird nicht unterstützt. Erlaubt sind: {erlaubt}.",
|
||||
)
|
||||
|
||||
data = await file.read()
|
||||
if not data:
|
||||
raise HTTPException(status.HTTP_400_BAD_REQUEST, "Die Datei ist leer.")
|
||||
if len(data) > MAX_BYTES:
|
||||
raise HTTPException(
|
||||
status.HTTP_400_BAD_REQUEST,
|
||||
f"Die Datei ist zu groß ({len(data) // 1024} KB). "
|
||||
f"Erlaubt sind höchstens {MAX_BYTES // 1024} KB.",
|
||||
)
|
||||
|
||||
asset = db.get(BrandingAsset, kind)
|
||||
if asset is None:
|
||||
asset = BrandingAsset(kind=kind, content_type=file.content_type, data=data)
|
||||
db.add(asset)
|
||||
else:
|
||||
asset.content_type = file.content_type
|
||||
asset.data = data
|
||||
db.commit()
|
||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
@router.delete("/{kind}")
|
||||
def delete_branding(
|
||||
kind: str,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_admin),
|
||||
) -> Response:
|
||||
"""Zurück auf das eingebaute Zeichen."""
|
||||
_check_kind(kind)
|
||||
asset = db.get(BrandingAsset, kind)
|
||||
if asset is not None:
|
||||
db.delete(asset)
|
||||
db.commit()
|
||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||
Reference in New Issue
Block a user