-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
38 lines (32 loc) · 1.17 KB
/
Copy pathmain.py
File metadata and controls
38 lines (32 loc) · 1.17 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
from fastapi import FastAPI, File, UploadFile
from typing import List
from typing import Annotated
import shutil
from pathlib import Path
app = FastAPI(title="FastAPI Tutorial")
UPLOAD_DIR = Path("uploads")
UPLOAD_DIR.mkdir(exist_ok=True)
@app.get("/")
def home():
return {"message":"Welcome to FastAPI Tutorial", "description":"This is a fastapi tutorial for beginners with docker","developer":"Yahya"}
@app.post("/upload")
async def upload_file(file: Annotated[UploadFile, File(...)]):
contents = await file.read()
return {
"filename": file.filename,
"content_type": file.content_type,
"size": len(contents)
}
@app.post("/upload-multiple")
async def upload_multiple(
file1: UploadFile = File(...),
file2: UploadFile = File(...)
):
files = [file1, file2]
return [{"filename":f.filename, "content_type": f.content_type, "size":f.size, "metadata":{"headers":f.headers, "file":f.file}} for f in files]
@app.post("/upload-save")
async def upload_and_save(file: UploadFile = File(...)):
dest = UPLOAD_DIR / file.filename
with dest.open("wb") as buffer:
shutil.copyfileobj(file.file, buffer)
return {"saved_to": str(dest)}