get list of file, it should be rethinked
This commit is contained in:
41
api.py
41
api.py
@@ -18,20 +18,7 @@ FILES_DIR = "./files"
|
||||
def root():
|
||||
return {"message": "hiii from sfs"}
|
||||
|
||||
@app.get("/{filename}")
|
||||
def get_file(filename: str, raw: bool = False):
|
||||
file_path = os.path.join(FILES_DIR, filename)
|
||||
|
||||
if not os.path.exists(file_path):
|
||||
raise HTTPException(status_code=404, detail="File not found")
|
||||
if raw:
|
||||
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
||||
return PlainTextResponse(f.read())
|
||||
|
||||
return FileResponse(file_path, filename=filename)
|
||||
|
||||
|
||||
@app.post("/")
|
||||
@app.post("/file")
|
||||
async def save_file(file: UploadFile = File(...)):
|
||||
contents = await file.read()
|
||||
|
||||
@@ -49,6 +36,32 @@ async def save_file(file: UploadFile = File(...)):
|
||||
else:
|
||||
return {"status": "file_exists", "filename": existed_url}
|
||||
|
||||
@app.get("/file/{filename}")
|
||||
def get_file(filename: str, raw: bool = False):
|
||||
file_path = os.path.join(FILES_DIR, filename)
|
||||
|
||||
if not os.path.exists(file_path):
|
||||
raise HTTPException(status_code=404, detail="File not found")
|
||||
if raw:
|
||||
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
||||
return PlainTextResponse(f.read())
|
||||
|
||||
return FileResponse(file_path, filename=filename)
|
||||
|
||||
@app.get("/files/")
|
||||
def get_list_of_files():
|
||||
files = db.get_all_files()
|
||||
return [
|
||||
{
|
||||
"id": f.id,
|
||||
"name": f.name,
|
||||
"content_type": f.content_type,
|
||||
"size": f.size,
|
||||
"hash": f.hash,
|
||||
}
|
||||
for f in files
|
||||
]
|
||||
|
||||
@app.get("/healthchecker")
|
||||
def healthchecker():
|
||||
return {"message": "Howdy :3"}
|
||||
|
||||
Reference in New Issue
Block a user