-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve_thumb.py
More file actions
67 lines (51 loc) · 1.89 KB
/
serve_thumb.py
File metadata and controls
67 lines (51 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json
import boto3
from bs4 import BeautifulSoup
from PIL import Image
from io import BytesIO
def get_image_name(html_body):
body = BeautifulSoup(html_body, "html.parser")
image_tag = body.find("meta", property="og:image")
image_name = image_tag["content"].split("/")[-1]
return image_name
def resize_image(image):
sizeX = image.size[0]
sizeY = image.size[1]
if sizeX > 300:
image.thumbnail((299, sizeY))
def create_thumbnail(s3_client, s3_bucket, original_image, path):
image_object = s3_client.get_object(Bucket=s3_bucket, Key=original_image)
image = Image.open(BytesIO(image_object["Body"].read()))
resize_image(image)
file_object = BytesIO()
image.save(file_object, "png")
file_object.seek(0)
s3_client.put_object(
Body=file_object,
Bucket=s3_bucket,
Key=path,
ContentType=image_object["ContentType"],
)
def object_exists(s3_client, s3_bucket, path):
try:
s3_client.head_object(Bucket=s3_bucket, Key=path)
return True
except:
return False
def lambda_handler(event, context):
request = event["Records"][0]["cf"]["request"]
if "api/rich_link" in request["uri"]:
s3_bucket = "cambiatus-uploads"
s3_image_path = "cambiatus-uploads/"
s3_thumb_path = "cambiatus-uploads/thumbnails/"
s3_client = boto3.client("s3")
image_name = get_image_name(request["body"])
if not object_exists(s3_client, s3_bucket, s3_thumb_path + image_name):
create_thumbnail(
s3_client,
s3_bucket,
s3_image_path + image_name,
s3_thumb_path + image_name,
)
request["body"] = request["body"].replace(s3_image_path, s3_thumb_path)
return {"statusCode": 200, "body": json.dumps(request["body"])}