-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
56 lines (48 loc) · 1.92 KB
/
app.py
File metadata and controls
56 lines (48 loc) · 1.92 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
from typing import List
from fastapi import (FastAPI, File, UploadFile)
from starlette.responses import (
HTMLResponse, RedirectResponse, PlainTextResponse, JSONResponse)
from starlette.status import (HTTP_500_INTERNAL_SERVER_ERROR, HTTP_200_OK)
from utilities import (generate_unique_name, filename_validation)
app = FastAPI()
# CREATE routes for image uploads
# Multi File Upload (FastAPI UploadFile)
@app.post("/uploadfiles/")
async def create_upload_files(files: List[UploadFile] = File(...)):
try:
[filename_validation(file.filename) for file in files]
return JSONResponse(status_code=HTTP_200_OK, content={"filenames": [generate_unique_name(file.filename) for file in files]})
except Exception as e:
print(e)
return PlainTextResponse(status_code=HTTP_500_INTERNAL_SERVER_ERROR, content=str(e))
# Single File Upload (FastAPI UploadFile)
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
try:
filename_validation(file.filename)
return JSONResponse(status_code=HTTP_200_OK, content={"filename": file.filename})
except Exception as e:
print(e)
return PlainTextResponse(status_code=HTTP_500_INTERNAL_SERVER_ERROR, content=str(e))
# For Testing the endpoints
@app.get("/")
async def main():
content = """
<body>
<div align="center">
<br/>
<h1> UploadFile test (Multiple)</h1>
<form action="/uploadfiles/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
<br/>
<h1> UploadFile test (Single)</h1>
<form action="/uploadfile/" enctype="multipart/form-data" method="post">
<input name="files" type="file">
<input type="submit">
</form>
</div>
</body>
"""
return HTMLResponse(content=content)