Skip to content

Commit

Permalink
udpated get_thumbnails.py
Browse files Browse the repository at this point in the history
  • Loading branch information
John-J-Riehl committed Jul 29, 2022
1 parent 276953d commit 2d29bd6
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions back-end/get_thumbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import time

def lambda_handler(event, context):
# get a Lambda client for the im-delete-meme function

# get the S3 bucket
### get the S3 service resource
s3 =
bucket =

# get entries from the database
### get all meme entries from the database
dynamodb =
table =
db_memes =
Expand All @@ -24,30 +22,29 @@ def lambda_handler(event, context):
thumbnails = []

for meme in memes:
thumbnail = dict()

# get the thumbnail image from S3
thumbnail_image = bucket.Object(f"thumbnails/{meme['id']}")

# skip this thumbnail if the image isn't present or it's past its
# time to die

# skip this thumbnail if the it's past its time to die
if time_now > : ### time to die value from database
continue

# create a thumbnail item with metadata and image data
thumbnail = {
"timeToLive": , ### timeToLive (in seconds remaining) computed as time_now - timeToDie
"timePosted": , ### directly from database
"userName": , ### directly from database
"id": ### directly from database
}

# load the image into an in-memory file object
in_mem_file = io.BytesIO()
thumbnail_image.
with io.BytesIO() as in_mem_file:
### download the thumbnail image from S3. skip the meme if the thumbnail doesn't exist

# now write the image into the thumbnail as a base64 data URL
# base 64 conversion code courtesy of https://stackoverflow.com/a/68989496/4062628
thumbnail["imageUrl"] = (
"data:image/jpeg;base64,"
+ base64.b64encode(in_mem_file.getvalue()).decode("utf-8"))
in_mem_file.close()
# now write the image into the thumbnail as a base64 data URL
# base 64 conversion code courtesy of https://stackoverflow.com/a/68989496/4062628
thumbnail["imageUrl"] = (
"data:image/jpeg;base64,"
+ base64.b64encode(in_mem_file.getvalue()).decode("utf-8"))

# build the thumnail's metadata
thumbnail["timeToLive"] =
thumbnail["timePosted"] =
thumbnail["userName"] =
thumbnail["id"] =
# add the thumbnail to the response
thumbnails.append(thumbnail)

# return the thumbnails
### return thumbnails as the body

0 comments on commit 2d29bd6

Please sign in to comment.