feat: serve Belki images through private MinIO

This commit is contained in:
nyan404 committed 2026-09-08 23:27:24 +03:00
1 parent b45e4925a6
commit ecc1f696da
14 files changed
+196 -121

No files matched your search

+30 -2
View File
@@ -5,6 +5,7 @@ import base64
import hashlib
import mimetypes
import os
import re
import secrets
import threading
import time
@@ -19,7 +20,7 @@ from .belki.applications import ApplicationError, ApplicationStore, validate_app
from .belki.admin_filters import WEB_TIMEZONE, matches_period, period_bounds
from .belki.google_sheets_export import GoogleSheetsExportService
from .belki.identity import IdentityDirectory
from .belki.image_host import ImageHostClient, ImageHostError
from .belki.image_host import ImageHostError, ImageStoreClient
from .belki.xlsx_export import build_xlsx
from .sudoku import SudokuError, SudokuStore
from .belki.vk_scheduler import VkConversationStore
@@ -48,6 +49,7 @@ MAX_ADMIN_AUTH_ATTEMPTS = 5
_ADMIN_UPDATE_SLOTS = threading.BoundedSemaphore(MAX_CONCURRENT_ADMIN_UPDATES)
WEB_UPLOAD_URL_TTL_SECONDS = 30 * 60
MAX_ISSUED_WEB_UPLOAD_URLS = 1_000
IMAGE_OBJECT_NAME = re.compile(r"^[a-f0-9]{32}\.(?:png|jpg|webp)$")
ADMIN_SESSION_TTL_SECONDS = 12 * 60 * 60
MAX_ADMIN_SESSIONS = 100
@@ -401,6 +403,32 @@ class Handler(BaseHTTPRequestHandler):
if path == "/api/admin/updates":
self._serve_admin_updates()
return
if path.startswith("/api/images/"):
object_name = path.removeprefix("/api/images/")
if not IMAGE_OBJECT_NAME.fullmatch(object_name):
self._json(404, {"error": "Не найдено"})
return
try:
response = ImageStoreClient().get_object(object_name)
except ImageHostError:
self._json(404, {"error": "Не найдено"})
return
try:
self.send_response(200)
self.send_header("Content-Type", response.headers.get("Content-Type", mimetypes.guess_type(object_name)[0] or "application/octet-stream"))
content_length = response.headers.get("Content-Length")
if content_length:
self.send_header("Content-Length", content_length)
self.send_header("Cache-Control", "public, max-age=31536000, immutable")
self.end_headers()
while chunk := response.read(64 * 1024):
self.wfile.write(chunk)
except (BrokenPipeError, ConnectionError, OSError):
return
finally:
response.close()
response.release_conn()
return
if path == "/api/admin/calculations":
if not self._admin_authorized():
return
@@ -548,7 +576,7 @@ class Handler(BaseHTTPRequestHandler):
if declared_type and declared_type != content_type:
raise ApiError("Тип файла не совпадает с содержимым изображения")
try:
url = ImageHostClient(api_url=os.getenv("BELKI_IMAGE_HOST_API_URL", "https://nyan.nyan404.ru")).upload_bytes(
url = ImageStoreClient().upload_bytes(
body, safe_upload_name(payload.get("name"), content_type), content_type
)
except ImageHostError as exc: