mirror of https://github.com/tiangolo/fastapi.git
🎨 Auto format
This commit is contained in:
parent
ce484fd61d
commit
468c1437e6
|
|
@ -18,11 +18,15 @@ from fastapi import routing
|
|||
from fastapi.datastructures import Default, DefaultPlaceholder
|
||||
from fastapi.exception_handlers import (
|
||||
http_exception_handler,
|
||||
request_entity_too_large_exception_handler,
|
||||
request_validation_exception_handler,
|
||||
websocket_request_validation_exception_handler,
|
||||
request_entity_too_large_exception_handler,
|
||||
)
|
||||
from fastapi.exceptions import RequestValidationError, WebSocketRequestValidationError, RequestEntityTooLarge
|
||||
from fastapi.exceptions import (
|
||||
RequestEntityTooLarge,
|
||||
RequestValidationError,
|
||||
WebSocketRequestValidationError,
|
||||
)
|
||||
from fastapi.logger import logger
|
||||
from fastapi.middleware.asyncexitstack import AsyncExitStackMiddleware
|
||||
from fastapi.openapi.docs import (
|
||||
|
|
@ -994,10 +998,9 @@ class FastAPI(Starlette):
|
|||
websocket_request_validation_exception_handler, # type: ignore
|
||||
)
|
||||
self.exception_handlers.setdefault(
|
||||
RequestEntityTooLarge,
|
||||
request_entity_too_large_exception_handler,
|
||||
)
|
||||
|
||||
RequestEntityTooLarge,
|
||||
request_entity_too_large_exception_handler,
|
||||
)
|
||||
|
||||
self.user_middleware: List[Middleware] = (
|
||||
[] if middleware is None else list(middleware)
|
||||
|
|
|
|||
|
|
@ -36,7 +36,6 @@ from fastapi._compat import (
|
|||
get_annotation_from_field_info,
|
||||
get_cached_model_fields,
|
||||
get_missing_field_error,
|
||||
is_bytes_field,
|
||||
is_bytes_sequence_field,
|
||||
is_scalar_field,
|
||||
is_scalar_sequence_field,
|
||||
|
|
@ -883,6 +882,7 @@ def _should_embed_body_fields(fields: List[ModelField]) -> bool:
|
|||
|
||||
from fastapi.exceptions import RequestEntityTooLarge
|
||||
|
||||
|
||||
async def _extract_form_body(
|
||||
body_fields: List[ModelField],
|
||||
received_body: FormData,
|
||||
|
|
@ -892,11 +892,10 @@ async def _extract_form_body(
|
|||
for field in body_fields:
|
||||
value = _get_multidict_value(field, received_body)
|
||||
field_info = field.field_info
|
||||
if (
|
||||
isinstance(field_info, (params.File, temp_pydantic_v1_params.File))
|
||||
and isinstance(value, UploadFile)
|
||||
):
|
||||
#If a file size limit is defined through max_size
|
||||
if isinstance(
|
||||
field_info, (params.File, temp_pydantic_v1_params.File)
|
||||
) and isinstance(value, UploadFile):
|
||||
# If a file size limit is defined through max_size
|
||||
max_size = getattr(field_info, "max_size", None)
|
||||
if max_size is not None:
|
||||
CHUNK = 8192
|
||||
|
|
@ -910,8 +909,8 @@ async def _extract_form_body(
|
|||
total += len(chunk)
|
||||
if total > max_size:
|
||||
raise RequestEntityTooLarge(
|
||||
f"Uploaded file '{field.alias}' exceeded max size={max_size} bytes"
|
||||
)
|
||||
f"Uploaded file '{field.alias}' exceeded max size={max_size} bytes"
|
||||
)
|
||||
content.extend(chunk)
|
||||
value = bytes(content)
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
from fastapi.encoders import jsonable_encoder
|
||||
from fastapi.exceptions import RequestValidationError, WebSocketRequestValidationError, RequestEntityTooLarge
|
||||
from fastapi.exceptions import (
|
||||
RequestValidationError,
|
||||
WebSocketRequestValidationError,
|
||||
)
|
||||
from fastapi.utils import is_body_allowed_for_status_code
|
||||
from fastapi.websockets import WebSocket
|
||||
from starlette.exceptions import HTTPException
|
||||
|
|
@ -14,6 +17,7 @@ async def request_entity_too_large_exception_handler(request, exc: Exception):
|
|||
content={"detail": str(exc) or "Uploaded file too large"},
|
||||
)
|
||||
|
||||
|
||||
async def http_exception_handler(request: Request, exc: HTTPException) -> Response:
|
||||
headers = getattr(exc, "headers", None)
|
||||
if not is_body_allowed_for_status_code(exc.status_code):
|
||||
|
|
|
|||
|
|
@ -183,7 +183,8 @@ class ResponseValidationError(ValidationException):
|
|||
message += f" {err}\n"
|
||||
return message
|
||||
|
||||
|
||||
class RequestEntityTooLarge(Exception):
|
||||
"""Raised when uploaded content exceeds the configured max_size."""
|
||||
pass
|
||||
|
||||
pass
|
||||
|
|
|
|||
6
main.py
6
main.py
|
|
@ -1,6 +1,6 @@
|
|||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, UploadFile, File
|
||||
from fastapi import FastAPI, File, UploadFile
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
|
@ -30,7 +30,7 @@ def update_item(item_id: int, item: Item):
|
|||
@app.post("/upload")
|
||||
async def upload_file(file: UploadFile = File(max_size=500)):
|
||||
total_bytes = 0
|
||||
|
||||
|
||||
# Safest: process in chunks instead of reading whole file
|
||||
while True:
|
||||
chunk = await file.read(1024 * 1024) # 1MB
|
||||
|
|
@ -39,5 +39,3 @@ async def upload_file(file: UploadFile = File(max_size=500)):
|
|||
total_bytes += len(chunk)
|
||||
|
||||
return {"filename": file.filename, "size": total_bytes}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue