-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.py
More file actions
47 lines (36 loc) · 1.29 KB
/
routes.py
File metadata and controls
47 lines (36 loc) · 1.29 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
from fastapi import APIRouter, UploadFile, File, Form
from fastapi.responses import FileResponse, JSONResponse
from os import getcwd, remove
from shutil import rmtree
router = APIRouter()
@router.post("/upload")
async def upload_file(file: UploadFile = File(...)):
with open(getcwd() + "/" + file.filename, "wb") as myfile:
content = await file.read()
myfile.write(content)
myfile.close()
return "success"
@router.get("/file/{name_file}")
def get_file(name_file: str):
return FileResponse(getcwd() + "/" + name_file)
@router.get("/download/{name_file}")
def download_file(name_file: str):
return FileResponse(getcwd() + "/" + name_file, media_type="application/octet-stream", filename=name_file)
@router.delete("/delete/{name_file}")
def delete_file(name_file: str):
try:
remove(getcwd() + "/" + name_file)
return JSONResponse(content={
"removed": True
}, status_code=200)
except FileNotFoundError:
return JSONResponse(content={
"removed": False,
"message": "File not found"
}, status_code=404)
@router.delete("/folder")
def delete_file(folder_name: str = Form(...)):
rmtree(getcwd() + folder_name)
return JSONResponse(content={
"removed": True
}, status_code=200)