patx/micropie
clean up file_upload example
Commit b24aed4 · patx · 2025-02-21T02:30:14-05:00
Comments
No comments yet.
Diff
diff --git a/examples/file_uploads/app.py b/examples/file_uploads/app.py
index f2d4776..be91291 100644
--- a/examples/file_uploads/app.py
+++ b/examples/file_uploads/app.py
@@ -5,13 +5,14 @@ before the request body is processed by the multipart parser.
from MicroPie import App, HttpMiddleware
-MAX_UPLOAD_SIZE = 10000 * 1024 * 1024 # 1MB
+MAX_UPLOAD_SIZE = 100 * 1024 * 1024 # 100MB
class MaxUploadSizeMiddleware(HttpMiddleware):
async def before_request(self, request):
# Check if we're dealing with a POST, PUT, or PATCH request
- if request.scope["method"] in ("POST", "PUT", "PATCH"):
+ if request.method in ("POST", "PUT", "PATCH"):
content_length = request.headers.get("content-length")
+ # Make sure the file is not too large
if int(content_length) > MAX_UPLOAD_SIZE:
return {
"status_code": 413,
@@ -44,6 +45,5 @@ class FileUploadApp(App):
return f"File '{filename}' uploaded successfully, saved to: {saved_path}!"
-# Instantiate the app and add the middleware.
app = FileUploadApp()
app.middlewares.insert(0, MaxUploadSizeMiddleware())