Fix: Strip whitespace from S3_PUBLIC_BASE_URL before building CDN URLs
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -30,7 +30,7 @@ def _error(detail: str, code: str, status: int):
|
|||||||
def _image_to_dict(
|
def _image_to_dict(
|
||||||
image: Image, *, cdn_base: str | None = None, duplicate: bool | None = None
|
image: Image, *, cdn_base: str | None = None, duplicate: bool | None = None
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
_base = cdn_base.rstrip("/") if cdn_base else None
|
_base = cdn_base.strip().rstrip("/") if cdn_base else None
|
||||||
file_url = f"{_base}/{image.storage_key}" if _base else f"/api/v1/images/{image.id}/file"
|
file_url = f"{_base}/{image.storage_key}" if _base else f"/api/v1/images/{image.id}/file"
|
||||||
thumbnail_url = (
|
thumbnail_url = (
|
||||||
(f"{_base}/{image.thumbnail_key}" if _base else f"/api/v1/images/{image.id}/thumbnail")
|
(f"{_base}/{image.thumbnail_key}" if _base else f"/api/v1/images/{image.id}/thumbnail")
|
||||||
|
|||||||
@@ -56,3 +56,10 @@ def test_cdn_trailing_slash_normalised():
|
|||||||
assert result["file_url"] == "https://cdn.example.com/abc123storagekey"
|
assert result["file_url"] == "https://cdn.example.com/abc123storagekey"
|
||||||
assert result["thumbnail_url"] == "https://cdn.example.com/abc123storagekey-thumb"
|
assert result["thumbnail_url"] == "https://cdn.example.com/abc123storagekey-thumb"
|
||||||
assert "//" not in result["file_url"].replace("https://", "")
|
assert "//" not in result["file_url"].replace("https://", "")
|
||||||
|
|
||||||
|
|
||||||
|
def test_cdn_trailing_whitespace_normalised():
|
||||||
|
img = _make_image(thumbnail_key="abc123storagekey-thumb")
|
||||||
|
result = _image_to_dict(img, cdn_base="https://cdn.example.com ")
|
||||||
|
assert result["file_url"] == "https://cdn.example.com/abc123storagekey"
|
||||||
|
assert result["thumbnail_url"] == "https://cdn.example.com/abc123storagekey-thumb"
|
||||||
|
|||||||
Reference in New Issue
Block a user