Skip to content

Commit 5d39772

Browse files
author
Evan Lin
committed
fix(handle_callback): Improve image handling by using iter_content for streaming
1 parent 14f181b commit 5d39772

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,18 @@ async def handle_callback(request: Request):
132132
print(f"Received image from user: {user_id}")
133133

134134
message_content = await line_bot_api.get_message_content(event.message.id)
135-
image_bytes = await message_content.read()
136-
image_stream = BytesIO(image_bytes)
135+
image_content = b''
136+
async for s in message_content.iter_content():
137+
image_content += s
138+
img_stream = PIL.Image.open(BytesIO(image_content))
137139

138140
file_name = f"{uuid.uuid4()}.jpg"
139141
gcs_uri = None
140142
response = "抱歉,處理您的圖片時發生錯誤。" # Default error message
141143

142144
try:
143145
gcs_uri = upload_to_gcs(
144-
image_stream, file_name, google_storage_bucket)
146+
img_stream, file_name, google_storage_bucket)
145147
if gcs_uri:
146148
print(f"Image uploaded to {gcs_uri}")
147149
response = generate_image_description(gcs_uri)

0 commit comments

Comments
 (0)