|
5 | 5 | from fastopenapi.core.types import FileUpload |
6 | 6 | from fastopenapi.routers.extractors import BaseAsyncRequestDataExtractor |
7 | 7 |
|
| 8 | +_MULTIPART_CACHE_ATTR = "_fastopenapi_multipart_cache" |
| 9 | + |
8 | 10 |
|
9 | 11 | class AioHttpRequestDataExtractor(BaseAsyncRequestDataExtractor): |
10 | 12 | @classmethod |
@@ -43,57 +45,73 @@ async def _get_body(cls, request: Any) -> dict | list | None: |
43 | 45 | except Exception: |
44 | 46 | return {} |
45 | 47 |
|
| 48 | + @classmethod |
| 49 | + async def _parse_multipart( |
| 50 | + cls, request: Any |
| 51 | + ) -> tuple[dict, dict[str, FileUpload | list[FileUpload]]]: |
| 52 | + """Parse multipart stream once and cache the result on the request.""" |
| 53 | + cached = getattr(request, _MULTIPART_CACHE_ATTR, None) |
| 54 | + if isinstance(cached, tuple): |
| 55 | + return cached |
| 56 | + |
| 57 | + form_data = {} |
| 58 | + files: dict[str, FileUpload | list[FileUpload]] = {} |
| 59 | + |
| 60 | + reader = await request.multipart() |
| 61 | + async for part in reader: |
| 62 | + if part.filename: |
| 63 | + file_data = await part.read() |
| 64 | + file_upload = FileUpload( |
| 65 | + filename=part.filename, |
| 66 | + content_type=part.headers.get("Content-Type"), |
| 67 | + size=len(file_data), |
| 68 | + file=file_data, |
| 69 | + ) |
| 70 | + if part.name in files: |
| 71 | + existing = files[part.name] |
| 72 | + if isinstance(existing, list): |
| 73 | + existing.append(file_upload) |
| 74 | + else: |
| 75 | + files[part.name] = [existing, file_upload] |
| 76 | + else: |
| 77 | + files[part.name] = file_upload |
| 78 | + else: |
| 79 | + form_data[part.name] = await part.text() |
| 80 | + |
| 81 | + result = (form_data, files) |
| 82 | + setattr(request, _MULTIPART_CACHE_ATTR, result) |
| 83 | + return result |
| 84 | + |
46 | 85 | @classmethod |
47 | 86 | async def _get_form_data(cls, request: Any) -> dict: |
48 | 87 | """Extract form data""" |
49 | | - form_data = {} |
50 | 88 | content_type = request.content_type or "" |
51 | 89 |
|
52 | 90 | if content_type == "application/x-www-form-urlencoded": |
53 | 91 | post_data = await request.post() |
| 92 | + form_data = {} |
54 | 93 | for key in post_data: |
55 | 94 | values = ( |
56 | 95 | post_data.getall(key) |
57 | 96 | if hasattr(post_data, "getall") |
58 | 97 | else [post_data[key]] |
59 | 98 | ) |
60 | 99 | form_data[key] = values[0] if len(values) == 1 else values |
| 100 | + return form_data |
61 | 101 |
|
62 | | - elif content_type == "multipart/form-data": |
63 | | - reader = await request.multipart() |
64 | | - async for part in reader: |
65 | | - if part.filename: |
66 | | - continue # Files handled separately |
67 | | - form_data[part.name] = await part.text() |
| 102 | + if content_type == "multipart/form-data": |
| 103 | + form_data, _ = await cls._parse_multipart(request) |
| 104 | + return form_data |
68 | 105 |
|
69 | | - return form_data |
| 106 | + return {} |
70 | 107 |
|
71 | 108 | @classmethod |
72 | 109 | async def _get_files(cls, request: Any) -> dict[str, FileUpload | list[FileUpload]]: |
73 | 110 | """Extract files from AioHTTP request""" |
74 | | - files = {} |
75 | 111 | content_type = request.content_type or "" |
76 | 112 |
|
77 | 113 | if content_type == "multipart/form-data": |
78 | | - reader = await request.multipart() |
79 | | - async for part in reader: |
80 | | - if not part.filename: |
81 | | - continue |
82 | | - |
83 | | - file_data = await part.read() |
84 | | - file_upload = FileUpload( |
85 | | - filename=part.filename, |
86 | | - content_type=part.headers.get("Content-Type"), |
87 | | - size=len(file_data), |
88 | | - file=file_data, |
89 | | - ) |
90 | | - |
91 | | - if part.name in files: |
92 | | - if isinstance(files[part.name], list): |
93 | | - files[part.name].append(file_upload) |
94 | | - else: |
95 | | - files[part.name] = [files[part.name], file_upload] |
96 | | - else: |
97 | | - files[part.name] = file_upload |
| 114 | + _, files = await cls._parse_multipart(request) |
| 115 | + return files |
98 | 116 |
|
99 | | - return files |
| 117 | + return {} |
0 commit comments