-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile_Access.py
More file actions
75 lines (59 loc) · 2.1 KB
/
Copy pathFile_Access.py
File metadata and controls
75 lines (59 loc) · 2.1 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
from fastapi import FastAPI, File, UploadFile, HTTPException, Depends
from fastapi.responses import FileResponse
from uuid import uuid4, UUID
from pathlib import Path
#from pydantic import BaseModel, Field
app = FastAPI(title = "File Access API", description = "API for accessing files securely", version = "1.0.0")
target = Path("uploads")
target.mkdir(exist_ok=True)
files = {}
'''class File(BaseModel):
id : UUID = Field(default_factory=uuid4)
filename : str
file_type : str'''
@app.post("/files/uploads")
async def upload_file(file: UploadFile= File(...)):
file_id = uuid4()
file_path = target/file.filename
with open(file_path , "wb") as f:
f.write(await file.read())
files[str(file_id)] = {
"filename" : file.filename,
"filetype": file.content_type
}
return {
"file_id" : file_id,
"filename": file.filename,
"message": "File uploaded successfully"
}
@app.get("/file/details")
async def file_detail(name: str ):
for key, data in files.items():
filename = Path(data["filename"]).stem
if filename == name:
return{
"file_id" : key,
"filename" : data["filename"],
"filetype" : data["filetype"]
}
raise HTTPException(status_code=404, detail="file not found")
@app.get("/files/{name}/download")
async def download_file(name: str):
for item in target.iterdir():
if item.stem == name:
return FileResponse(
path = item,
filename= item.name
)
raise HTTPException(status_code=404, detail= "File not found")
@app.delete("/files/{name}")
async def delete_file(name: str):
for i in target.iterdir():
if i.stem == name:
i.unlink()
for key, data in files.items():
if Path(data["filename"]).stem == name:
del files[key]
break
return {"message":"file deleted "}
raise HTTPException(status_code=404, detail="file not found")