-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdatastructures.py
More file actions
108 lines (90 loc) · 2.76 KB
/
Copy pathdatastructures.py
File metadata and controls
108 lines (90 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import typing as t
from io import BytesIO, StringIO
from ellar.pydantic import as_pydantic_validator
from starlette.datastructures import (
URL as URL,
)
from starlette.datastructures import (
Address as Address,
)
from starlette.datastructures import (
FormData as FormData,
)
from starlette.datastructures import (
Headers as Headers,
)
from starlette.datastructures import (
QueryParams as QueryParams,
)
from starlette.datastructures import (
State as State,
)
from starlette.datastructures import (
UploadFile as StarletteUploadFile,
)
from starlette.datastructures import (
URLPath,
)
from typing_extensions import Annotated, Doc
__all__ = [
"URL",
"Address",
"FormData",
"Headers",
"QueryParams",
"UploadFile",
"URLPath",
"State",
"ContentFile",
]
@as_pydantic_validator(
"__validate_input__", schema={"type": "string", "format": "binary"}
)
class UploadFile(StarletteUploadFile):
"""
A file uploaded in a request.
Define it as a *path operation function* (or dependency) parameter.
## Example
```python
from ellar.common import ModuleRouter, File, UploadFile
router = ModuleRouter()
@router.post("/files/")
async def create_file(file: File[bytes]):
return {"file_size": len(file)}
@router.post("/uploadfile/")
async def create_upload_file(file: UploadFile):
return {"filename": file.filename}
```
"""
file: Annotated[
t.BinaryIO,
Doc("The standard Python file object (non-async)."),
]
filename: Annotated[t.Optional[str], Doc("The original file name.")]
size: Annotated[t.Optional[int], Doc("The size of the file in bytes.")]
headers: Annotated[Headers, Doc("The headers of the request.")]
content_type: Annotated[
t.Optional[str], Doc("The content type of the request, from the headers.")
]
@classmethod
def __validate_input__(cls, __input_value: t.Any, _: t.Any) -> "UploadFile":
if not isinstance(__input_value, StarletteUploadFile):
raise ValueError(f"Expected UploadFile, received: {type(__input_value)}")
return cls(
__input_value.file,
size=__input_value.size,
filename=__input_value.filename,
headers=__input_value.headers,
)
class ContentFile(UploadFile):
"""
A File-like object that takes just raw content, rather than an actual file.
"""
def __init__(
self, content: t.Union[str, bytes], name: t.Optional[str] = None
) -> None:
stream_class = StringIO if isinstance(content, str) else BytesIO
headers = Headers({"content-type": "text/plain"})
super().__init__(
stream_class(content), filename=name, size=len(content), headers=headers
)