get list of file, it should be rethinked

This commit is contained in:
dm
2025-08-26 16:53:53 +03:00
parent 4ea7d44954
commit 82671319e4
2 changed files with 28 additions and 15 deletions

41
api.py
View File

@@ -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"}