Skip to content

Commit

Permalink
Merge pull request #106 from whyphi/dev/2.0
Browse files Browse the repository at this point in the history
Merge: `2.0.1`
  • Loading branch information
wderocco8 authored Aug 22, 2024
2 parents a06e1b6 + f47fa70 commit b7648d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
40 changes: 24 additions & 16 deletions chalicelib/services/EventsRushService.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from bson import ObjectId
import datetime
from chalicelib.s3 import s3
from chalicelib.utils import get_prev_image_version

class EventsRushService:
class BSONEncoder(json.JSONEncoder):
Expand Down Expand Up @@ -42,7 +43,7 @@ def create_rush_event(self, data: dict):
data["numAttendees"] = 0

# upload eventCoverImage to s3 bucket (convert everything to png files for now... can adjust later)
image_path = f"image/rush/{data['categoryId']}/{event_id}.png"
image_path = f"image/rush/{data['categoryId']}/{event_id}/{data['eventCoverImageVersion']}.png" # MUST initialize version to v0
image_url = s3.upload_binary_data(image_path, data["eventCoverImage"])

# add image_url to data object (this also replaces the original base64 image url)
Expand Down Expand Up @@ -70,7 +71,11 @@ def modify_rush_event(self, data: dict):

data["lastModified"] = datetime.datetime.now()
data["_id"] = event_oid


# get image versions
eventCoverImageVersion = data["eventCoverImageVersion"]
prevEventCoverImageVersion = get_prev_image_version(version=eventCoverImageVersion)

# Check if event exists in the rush-event collection
event = self.mongo_module.get_document_by_id(
f"{self.collection_prefix}rush-event", event_id
Expand All @@ -79,22 +84,24 @@ def modify_rush_event(self, data: dict):
if not event:
raise Exception("Event does not exist.")

# update eventCoverImageVersion in db
event["eventCoverImageVersion"] = eventCoverImageVersion

# get categoryId (for s3 path)
event_category_id = event["categoryId"]

# obtain image paths using versioning
image_path = f"image/rush/{event_category_id}/{event_id}/{eventCoverImageVersion}.png"
prev_image_path = f"image/rush/{event_category_id}/{event_id}/{prevEventCoverImageVersion}.png"

# if eventCoverImage contains https://whyphi-zap.s3.amazonaws.com, no need to update anything, otherwise update s3
if "https://whyphi-zap.s3.amazonaws.com" not in data["eventCoverImage"]:

# get image path
image_path = f"image/rush/{event_category_id}/{event_id}.png"

# remove previous eventCoverImage from s3 bucket
s3.delete_binary_data(object_id=image_path)

# upload eventCoverImage to s3 bucket
image_url = s3.upload_binary_data(path=image_path, data=data["eventCoverImage"])
# remove previous eventCoverImage from s3 bucket
s3.delete_binary_data(object_id=prev_image_path)

# upload eventCoverImage to s3 bucket
image_url = s3.upload_binary_data(path=image_path, data=data["eventCoverImage"])

# add image_url to data object (this also replaces the original base64 image url)
data["eventCoverImage"] = image_url
# add image_url to data object (this also replaces the original base64 image url)
data["eventCoverImage"] = image_url

# Merge data with event (from client + mongo) --> NOTE: event must be unpacked first so
# that data overrides the matching keys
Expand Down Expand Up @@ -310,9 +317,10 @@ def delete_rush_event(self, event_id: str):
raise Exception("Event does not exist.")

event_category_id = event["categoryId"]
event_cover_image_version = event["eventCoverImageVersion"]

# Get eventCoverImage path
image_path = f"image/rush/{event_category_id}/{event_id}.png"
image_path = f"image/rush/{event_category_id}/{event_id}/{event_cover_image_version}.png"

# remove previous eventCoverImage from s3 bucket
s3.delete_binary_data(object_id=image_path)
Expand Down
3 changes: 3 additions & 0 deletions chalicelib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ def get_file_extension_from_base64(base64_data):
extension = extension_map.get(content_type, "dat")

return extension

def get_prev_image_version(version: str):
return "v" + str(int(version[1:]) - 1)

0 comments on commit b7648d0

Please sign in to comment.