Skip to content

Commit

Permalink
Added Mime/type to model #81
Browse files Browse the repository at this point in the history
  • Loading branch information
asuresh-code committed Dec 19, 2024
1 parent af5e70d commit f099d4d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions object_storage_api/models/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ImageBase(BaseModel):
"""

file_name: str
file_type: str
# Key of the image file in object storage
object_key: str
thumbnail_base64: str
Expand Down
1 change: 1 addition & 0 deletions object_storage_api/schemas/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ImageMetadataSchema(CreatedModifiedSchemaMixin, ImagePostMetadataSchema):

id: str = Field(description="ID of the image")
file_name: str = Field(description="File name of the image")
file_type: str = Field(description="File type of the image")
primary: bool = Field(description="Whether the image is the primary for its related entity")
thumbnail_base64: str = Field(description="Thumbnail of the image as a base64 encoded byte string")

Expand Down
1 change: 1 addition & 0 deletions object_storage_api/services/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def create(self, image_metadata: ImagePostMetadataSchema, upload_file: UploadFil
**image_metadata.model_dump(),
id=image_id,
file_name=upload_file.filename,
file_type=upload_file.content_type,
object_key=object_key,
thumbnail_base64=thumbnail_base64,
)
Expand Down
6 changes: 5 additions & 1 deletion object_storage_api/stores/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import mimetypes

from fastapi import UploadFile

Expand Down Expand Up @@ -47,12 +48,15 @@ def create_presigned_get(self, image: ImageOut) -> str:
:return: Presigned url to get the image.
"""
logger.info("Generating presigned url to get image with object key: %s from the object store", image.object_key)

extension = mimetypes.guess_extension(image.file_type)
response = s3_client.generate_presigned_url(
"get_object",
Params={
"Bucket": object_storage_config.bucket_name.get_secret_value(),
"Key": image.object_key,
"ResponseContentDisposition": f'inline; filename="{image.file_name}"',
"ResponseContentDisposition": f'attachment; filename="{image.file_name}{extension}"',
"ResponseContentType": "application/octet-stream",
},
ExpiresIn=object_storage_config.presigned_url_expiry_seconds,
)
Expand Down

0 comments on commit f099d4d

Please sign in to comment.