33import logging
44
55from django .http import HttpResponse
6+ from objectstore_client import ClientError
67from rest_framework .request import Request
78
89from sentry .api .api_owners import ApiOwner
910from sentry .api .api_publish_status import ApiPublishStatus
1011from sentry .api .base import region_silo_endpoint
1112from sentry .api .bases .project import ProjectEndpoint
1213from 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
1616logger = 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"\x89 PNG\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
5520class 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