diff --git a/api.py b/api.py index f18808f..d473891 100644 --- a/api.py +++ b/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"} diff --git a/db.py b/db.py index 0cf9b1c..bb66408 100644 --- a/db.py +++ b/db.py @@ -82,7 +82,7 @@ def add_file(filename: str, content_type, size: int, hash): url += f".{new_file.extension}" return url - + if __name__ == "__main__": for i in get_all_files(): print(f"{i.id} {i.name}.{i.extension} ({i.hash}) {i.content_type} {i.size}")