Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize facility cover image before upload #1106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion care/facility/api/serializers/facility.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from io import BytesIO

import boto3
from django.conf import settings
from django.contrib.auth import get_user_model
from PIL import Image
from rest_framework import serializers

from care.facility.models import FACILITY_TYPES, Facility, FacilityLocalGovtBody
Expand Down Expand Up @@ -132,18 +135,41 @@ class Meta:
# Check DRYpermissions before updating
fields = ("cover_image", "read_cover_image_url")

def resize_image(self, image, image_extension="PNG"):
image_format = (
"JPEG" if image_extension.lower() == "jpg" else image_extension.upper()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we moving to uppercase?

)

img = Image.open(image)
img.thumbnail((720, 720))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we only allow images of this ratio to be uploaded in the FE? ( Square ) Anything not in this ration would look weird right?

img_io = BytesIO()
img.save(img_io, format=image_format)
img_size = img_io.tell()
img_io.seek(0)
return (img_io, img_size)

def save(self, **kwargs):
facility = self.instance
image = self.validated_data["cover_image"]
image_extension = image.name.rsplit(".", 1)[-1]

resized_image, resized_image_size = self.resize_image(
image, image_extension=image_extension
)

if resized_image_size > settings.FACILITY_COVER_IMAGE_MAX_SIZE:
raise serializers.ValidationError(
{"cover_image": "The image size is too large."}
)

s3 = boto3.client(
"s3", **cs_provider.get_client_config(cs_provider.BucketType.FACILITY.value)
)
image_location = f"cover_images/{facility.external_id}_cover.{image_extension}"
s3.put_object(
Bucket=settings.FACILITY_S3_BUCKET,
Key=image_location,
Body=image.file,
Body=resized_image,
)
facility.cover_image_url = image_location
facility.save()
Expand Down
3 changes: 3 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ def GETKEY(group, request):
"FILE_UPLOAD_BUCKET_ENDPOINT",
default=f"https://{FILE_UPLOAD_BUCKET}.s3.amazonaws.com",
)
FACILITY_COVER_IMAGE_MAX_SIZE = env(
"FACILITY_COVER_IMAGE_MAX_SIZE", default=1024 * 1024
) # 1MB, in bytes

FACILITY_S3_BUCKET = env("FACILITY_S3_BUCKET", default="")
FACILITY_S3_REGION = env("FACILITY_S3_REGION_CODE", default="ap-south-1")
Expand Down