fix: expose MinIO presigned URLs over HTTPS

This commit is contained in:
hailt 2026-11-12 22:23:14 +07:00
parent ae20a58a43
commit 2b8f8551ce

View file

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
from functools import lru_cache from functools import lru_cache
from urllib.parse import urlsplit, urlunsplit
from src.core.config import get_settings from src.core.config import get_settings
@ -26,26 +27,37 @@ def _client():
) )
def _publicize(url: str) -> str:
"""Expose an internal signed URL through the HTTPS storage proxy."""
public_base = get_settings().r2_public_base.rstrip("/")
if not public_base:
return url
public = urlsplit(public_base)
signed = urlsplit(url)
return urlunsplit((public.scheme, public.netloc,
f"{public.path}{signed.path}", signed.query, signed.fragment))
def presign_put(key: str, content_type: str = "application/octet-stream") -> str: def presign_put(key: str, content_type: str = "application/octet-stream") -> str:
"""Direct-upload URL: browser/app PUTs bytes straight to R2 (saves Class-A + bandwidth).""" """Direct-upload URL: browser/app PUTs bytes straight to R2 (saves Class-A + bandwidth)."""
s = get_settings() s = get_settings()
return _client().generate_presigned_url( url = _client().generate_presigned_url(
"put_object", "put_object",
Params={"Bucket": s.r2_bucket, "Key": key, "ContentType": content_type}, Params={"Bucket": s.r2_bucket, "Key": key, "ContentType": content_type},
ExpiresIn=s.presign_put_ttl, ExpiresIn=s.presign_put_ttl,
) )
return _publicize(url)
def presign_get(key: str) -> str: def presign_get(key: str) -> str:
"""Short-lived read URL for preview/download. Thumbnails preferred over full images.""" """Short-lived read URL for preview/download. Thumbnails preferred over full images."""
s = get_settings() s = get_settings()
if s.r2_public_base: url = _client().generate_presigned_url(
return f"{s.r2_public_base.rstrip('/')}/{key}"
return _client().generate_presigned_url(
"get_object", "get_object",
Params={"Bucket": s.r2_bucket, "Key": key}, Params={"Bucket": s.r2_bucket, "Key": key},
ExpiresIn=s.presign_get_ttl, ExpiresIn=s.presign_get_ttl,
) )
return _publicize(url)
def head_exists(key: str) -> bool: def head_exists(key: str) -> bool:
@ -88,11 +100,12 @@ def create_multipart(key: str, content_type: str = "application/octet-stream") -
def presign_part(key: str, upload_id: str, part_number: int, def presign_part(key: str, upload_id: str, part_number: int,
expires_in: int = 3600) -> str: expires_in: int = 3600) -> str:
s = get_settings() s = get_settings()
return _client().generate_presigned_url( url = _client().generate_presigned_url(
"upload_part", "upload_part",
Params={"Bucket": s.r2_bucket, "Key": key, Params={"Bucket": s.r2_bucket, "Key": key,
"UploadId": upload_id, "PartNumber": part_number}, "UploadId": upload_id, "PartNumber": part_number},
ExpiresIn=expires_in) ExpiresIn=expires_in)
return _publicize(url)
def complete_multipart(key: str, upload_id: str, parts: list[dict]) -> str: def complete_multipart(key: str, upload_id: str, parts: list[dict]) -> str: