Skip to content

Commit 4f0e1e9

Browse files
committed
Update to latest practices
1 parent c1f1b3d commit 4f0e1e9

File tree

2 files changed

+22
-41
lines changed

2 files changed

+22
-41
lines changed

src/sentry/objectstore/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def distribution(
3636
_ATTACHMENTS_CLIENT: Client | None = None
3737
_ATTACHMENTS_USECASE = Usecase("attachments", expiration_policy=TimeToLive(timedelta(days=30)))
3838

39+
_PREPROD_CLIENT: Client | None = None
40+
_PREPROD_USECASE = Usecase("preprod", expiration_policy=TimeToLive(timedelta(days=30)))
41+
3942

4043
def get_attachments_session(org: int, project: int) -> Session:
4144
global _ATTACHMENTS_CLIENT
@@ -49,3 +52,17 @@ def get_attachments_session(org: int, project: int) -> Session:
4952
)
5053

5154
return _ATTACHMENTS_CLIENT.session(_ATTACHMENTS_USECASE, org=org, project=project)
55+
56+
57+
def get_preprod_session(org: int, project: int) -> Session:
58+
global _PREPROD_CLIENT
59+
if not _PREPROD_CLIENT:
60+
from sentry import options as options_store
61+
62+
options = options_store.get("objectstore.config")
63+
_PREPROD_CLIENT = Client(
64+
options["base_url"],
65+
metrics_backend=SentryMetricsBackend(),
66+
)
67+
68+
return _PREPROD_CLIENT.session(_PREPROD_USECASE, org=org, project=project)

src/sentry/preprod/api/endpoints/project_preprod_artifact_image.py

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,19 @@
33
import logging
44

55
from django.http import HttpResponse
6+
from objectstore_client import ClientError
67
from rest_framework.request import Request
78

89
from sentry.api.api_owners import ApiOwner
910
from sentry.api.api_publish_status import ApiPublishStatus
1011
from sentry.api.base import region_silo_endpoint
1112
from sentry.api.bases.project import ProjectEndpoint
1213
from sentry.models.project import Project
13-
from sentry.objectstore import preprod
14-
from sentry.objectstore.service import ClientError
14+
from sentry.objectstore import get_preprod_session
1515

1616
logger = logging.getLogger(__name__)
1717

1818

19-
def detect_image_content_type(image_data: bytes) -> str:
20-
if not image_data:
21-
return "application/octet-stream"
22-
23-
# Check magic bytes for common image formats
24-
if image_data[:8] == b"\x89PNG\r\n\x1a\n":
25-
return "image/png"
26-
elif image_data[:3] == b"\xff\xd8\xff":
27-
return "image/jpeg"
28-
elif image_data[:4] == b"RIFF" and image_data[8:12] == b"WEBP":
29-
return "image/webp"
30-
elif image_data[:2] in (b"BM", b"BA", b"CI", b"CP", b"IC", b"PT"):
31-
return "image/bmp"
32-
elif image_data[:6] in (b"GIF87a", b"GIF89a"):
33-
return "image/gif"
34-
elif image_data[:4] == b"\x00\x00\x01\x00":
35-
return "image/x-icon"
36-
elif len(image_data) >= 12 and image_data[4:12] in (b"ftypavif", b"ftypavis"):
37-
return "image/avif"
38-
elif len(image_data) >= 12 and image_data[4:12] in (
39-
b"ftypheic",
40-
b"ftypheix",
41-
b"ftyphevc",
42-
b"ftyphevx",
43-
):
44-
return "image/heic"
45-
46-
# Default to generic binary if we can't detect the type
47-
logger.warning(
48-
"Could not detect image content type from magic bytes",
49-
extra={"first_bytes": image_data[:16].hex() if len(image_data) >= 16 else image_data.hex()},
50-
)
51-
return "application/octet-stream"
52-
53-
5419
@region_silo_endpoint
5520
class ProjectPreprodArtifactImageEndpoint(ProjectEndpoint):
5621
owner = ApiOwner.EMERGE_TOOLS
@@ -69,16 +34,15 @@ def get(
6934
project_id = project.id
7035

7136
object_key = f"{organization_id}/{project_id}/{image_id}"
72-
client = preprod.for_project(organization_id, project_id)
37+
session = get_preprod_session(organization_id, project_id)
7338

7439
try:
75-
result = client.get(object_key)
40+
result = session.get(object_key)
7641
# Read the entire stream at once (necessary for content_type)
7742
image_data = result.payload.read()
7843

7944
# Detect content type from the image data
80-
content_type = detect_image_content_type(image_data)
81-
return HttpResponse(image_data, content_type=content_type)
45+
return HttpResponse(image_data, content_type=result.metadata.content_type)
8246

8347
except ClientError as e:
8448
if e.status == 404:

0 commit comments

Comments
 (0)