release: publish saldo 0.1.0
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
from flask import current_app, url_for
|
||||
from werkzeug.datastructures import FileStorage
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
|
||||
ALLOWED_AVATAR_EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp", ".gif"}
|
||||
|
||||
|
||||
def save_avatar_upload(file_storage: FileStorage | None) -> str | None:
|
||||
if file_storage is None or not file_storage.filename:
|
||||
return None
|
||||
|
||||
original_name = secure_filename(file_storage.filename)
|
||||
suffix = Path(original_name).suffix.lower()
|
||||
if suffix not in ALLOWED_AVATAR_EXTENSIONS:
|
||||
raise ValueError("Bitte ein Bild als PNG, JPG, WEBP oder GIF hochladen.")
|
||||
|
||||
upload_dir = Path(current_app.config["AVATAR_UPLOAD_DIR"])
|
||||
upload_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
filename = f"{uuid4().hex}{suffix}"
|
||||
file_storage.save(upload_dir / filename)
|
||||
return url_for("main.uploaded_avatar", filename=filename)
|
||||
Reference in New Issue
Block a user