Skip to content

Commit

Permalink
Merge pull request #83 from openstreetmap-polska/dev
Browse files Browse the repository at this point in the history
Release to main
  • Loading branch information
Zaczero authored Sep 21, 2024
2 parents a42b95c + c0a4f58 commit af32366
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions api/v1/photos.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import timedelta
from io import BytesIO
from typing import Annotated
from urllib.parse import unquote_plus

Expand Down Expand Up @@ -29,9 +30,14 @@ async def _fetch_image(url: str) -> tuple[bytes, str]:
if content_type and content_type not in IMAGE_CONTENT_TYPES:
raise HTTPException(500, f'Unsupported file type {content_type!r}, must be one of {IMAGE_CONTENT_TYPES}')

file = await r.content.read(IMAGE_REMOTE_MAX_FILE_SIZE + 1)
if len(file) > IMAGE_REMOTE_MAX_FILE_SIZE:
raise HTTPException(500, f'File is too large, max allowed size is {IMAGE_REMOTE_MAX_FILE_SIZE} bytes')
with BytesIO() as buffer:
async for chunk, _ in r.content.iter_chunks():
buffer.write(chunk)
if buffer.tell() > IMAGE_REMOTE_MAX_FILE_SIZE:
raise HTTPException(
500, f'File is too large, max allowed size is {IMAGE_REMOTE_MAX_FILE_SIZE} bytes'
)
file = buffer.getvalue()

# Check if file type is supported
content_type = magic.from_buffer(file[:2048], mime=True)
Expand Down

0 comments on commit af32366

Please sign in to comment.