async def get_thumbnail(dimensions: str, response: Response, request: Request): cached_url = request.cookies.get(dimensions) if cached_url: return { 'img_url': cached_url, "message": "Thumbnail has been created within last hour." } try: width, height = [int(dimension) for dimension in dimensions.split('x')] except ValueError: response.status_code = status.HTTP_400_BAD_REQUEST return {"message": "Your dimensions are invalid. Please try again."} if width <= 0 or height <= 0: response.status_code = status.HTTP_400_BAD_REQUEST return {"message": "Height and width must be greater then 0."} s3_resource = S3Resource() img = s3_resource.get_random_file(os.getenv('SOURCE_BUCKET')) if img: resized_image_url = resize_image(img, width, height) response.set_cookie(key=dimensions, value=resized_image_url, max_age=3600) return { 'message': f"Thumbnail {dimensions} successfully created", 'img_url': resized_image_url } response.status_code = status.HTTP_404_NOT_FOUND return {"message": "Please upload images in order to create a thumbnail."}
def test_non_empty_bucket(s3_test, bucket_name, test_file): my_client = S3Resource() bucket = my_client.save_image_to_bucket(test_file, bucket_name, 'testing_file.png') assert bucket == resized_image_url('testing_file.png') random_file = my_client.get_random_file(bucket_name) assert random_file is not None
def resize_image(image, width: int, height: int) -> str: """Resize image with PIL library :param image: binary representation of image :param width: target image width :param height: target image height :return: ready image url """ image_body = image.get()['Body'].read() img = Image.open(io.BytesIO(image_body)) img = img.resize((width, height), Image.ANTIALIAS) buffer = io.BytesIO() img.save(buffer, "PNG") buffer.seek(0) new_file_name = f'{width}x{height}-{image.key}' s3_resource = S3Resource() img_url = s3_resource.save_image_to_bucket( buffer, os.getenv('RESIZED_BUCKET'), f'resized_images/{new_file_name}') return img_url
def upload_file_to_s3(file: bytes, file_name: str, file_extension: str) -> Optional[bool]: """Upload a file to an S3 bucket :param file: File to upload(bytes) :param file_name: S3 object name. If not specified then file_name is used :param file_extension: file extension :return True if no errors occur """ img = Image.open(io.BytesIO(file)) buffer = io.BytesIO() img.save(buffer, file_extension) buffer.seek(0) try: s3_resource = S3Resource() s3_resource.save_image_to_bucket(buffer, os.getenv('SOURCE_BUCKET'), file_name, file_extension) except botocore.exceptions.ClientError as error: raise error except botocore.exceptions.ParamValidationError as error: raise error return True
def s3_file_created(s3_test, test_app, test_file, bucket_name): my_client = S3Resource() my_client.save_image_to_bucket(test_file, bucket_name, 'testing_file.png') file = my_client.get_random_file(bucket_name) return file
def test_empty_bucket(s3_test, bucket_name): my_client = S3Resource() bucket = my_client.get_random_file(bucket_name) assert bucket is None
def test_save_item_to_bucket(s3_test, bucket_name, test_file): my_client = S3Resource() bucket = my_client.save_image_to_bucket(test_file, bucket_name, 'testing_file.png') assert bucket == resized_image_url('testing_file.png')