-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from Mr-Sunglasses/testss
feat: file upload limit
- Loading branch information
Showing
3 changed files
with
58 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from starlette import status | ||
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint | ||
from starlette.requests import Request | ||
from starlette.responses import Response | ||
from starlette.types import ASGIApp | ||
|
||
|
||
class LimitUploadSize(BaseHTTPMiddleware): | ||
def __init__(self, app: ASGIApp, max_upload_size: int) -> None: | ||
super().__init__(app) | ||
self.max_upload_size = max_upload_size | ||
|
||
async def dispatch( | ||
self, request: Request, call_next: RequestResponseEndpoint | ||
) -> Response: | ||
if request.method == "POST": | ||
if "content-length" not in request.headers: | ||
return Response(status_code=status.HTTP_411_LENGTH_REQUIRED) | ||
content_length = int(request.headers["content-length"]) | ||
if content_length > self.max_upload_size: | ||
return Response( | ||
"File is too large", | ||
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, | ||
) | ||
return await call_next(request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters