Skip to content

Commit 13fc8fe

Browse files
committed
feat: image upload tests (18 passing) + FileTooLarge 413 exception
1 parent 4758ee6 commit 13fc8fe

2 files changed

Lines changed: 417 additions & 12 deletions

File tree

annotations/services/image_storage.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@
1414
from google.auth.transport.requests import Request
1515
from google.cloud import storage
1616
from django.conf import settings
17-
from rest_framework.exceptions import UnsupportedMediaType
17+
from rest_framework import status as drf_status
18+
from rest_framework.exceptions import (
19+
APIException,
20+
UnsupportedMediaType,
21+
)
22+
23+
24+
class FileTooLarge(APIException):
25+
status_code = drf_status.HTTP_413_REQUEST_ENTITY_TOO_LARGE
26+
default_detail = "File exceeds maximum allowed size."
27+
default_code = "file_too_large"
28+
1829

1930
logger = logging.getLogger(__name__)
2031

@@ -41,10 +52,8 @@ def upload_original(
4152
4253
Raises:
4354
UnsupportedMediaType: content-type not in allowlist.
44-
rest_framework.exceptions.ValidationError: file too large.
55+
FileTooLarge: file exceeds IMAGE_MAX_BYTES.
4556
"""
46-
from rest_framework.exceptions import ValidationError
47-
4857
content_type = getattr(
4958
django_uploaded_file, "content_type", ""
5059
) or ""
@@ -64,14 +73,9 @@ def upload_original(
6473
django_uploaded_file.seek(0)
6574

6675
if size_bytes > max_bytes:
67-
raise ValidationError(
68-
{
69-
"file": (
70-
f"File too large ({size_bytes} bytes). "
71-
f"Maximum allowed: {max_bytes} bytes."
72-
)
73-
},
74-
code="file_too_large",
76+
raise FileTooLarge(
77+
f"File too large ({size_bytes} bytes). "
78+
f"Maximum allowed: {max_bytes} bytes."
7579
)
7680

7781
ext = _ALLOWED_EXTENSIONS[content_type]

0 commit comments

Comments
 (0)