diff --git a/README.md b/README.md index be37285..59997d2 100644 --- a/README.md +++ b/README.md @@ -51,12 +51,13 @@ curl -X POST "http://localhost:8000/paste/create" \ -d '{"title": "My First Paste", "text": "hello2"}' # example response is -# {"status":"success","id":"6556e","url":"/paste/6556e"} +# {"id":"6556e","size_bytes":6} ``` ### To view a paste ```sh # put the paste id after the `/paste/` in the url, like below +# you can also open the url in the browser curl http://localhost:8000/paste/6556e ``` diff --git a/modules/sqlite_helpers.py b/modules/sqlite_helpers.py index 45d0629..5bb39b4 100644 --- a/modules/sqlite_helpers.py +++ b/modules/sqlite_helpers.py @@ -215,3 +215,16 @@ def insert_paste(sqlite_file: str, paste_id: str, title: str): finally: cursor.close() db.close() + + +def get_paste(sqlite_file: str, paste_id: str): + db = sqlite3.connect(sqlite_file) + cursor = db.cursor() + try: + sql = "SELECT title FROM pastes WHERE id = ?" + cursor.execute(sql, (paste_id,)) + result = cursor.fetchone() + return result[0] + except Exception: + logger.exception(f"Getting paste {paste_id} had an error") + return None diff --git a/server.py b/server.py index bd72314..9b59a76 100644 --- a/server.py +++ b/server.py @@ -171,7 +171,7 @@ async def create_paste(request: Request): api_key = request.headers.get("x-api-key") if CLEEZY_PASTE_API_KEY is None: - logging.warning("CLEEZY_PASTE_API_KEY isn't set, skipping api key check") + logging.warning("CLEEZY_PASTE_API_KEY isn't set, skipping api key check for /paste/create") elif api_key != CLEEZY_PASTE_API_KEY: raise HTTPException(status_code=401, detail=f"Invalid API Key '{api_key}'") @@ -203,9 +203,8 @@ async def create_paste(request: Request): paste_path.write_bytes(text_bytes) return { - "status": "success", "id": paste_id, - "url": f"/paste/{paste_id}" + "size_bytes": len(text_bytes) } @@ -214,7 +213,18 @@ async def view_paste(paste_id: str): paste_path = PASTES_DIR / paste_id if not paste_path.exists(): raise HTTPException(status_code=HttpResponse.NOT_FOUND.code) - return PlainTextResponse(paste_path.read_text(encoding="utf-8")) + paste_title = sqlite_helpers.get_paste(DATABASE_FILE, paste_id) + + return HTMLResponse( + f""" + + + {paste_title} + + +
{paste_path.read_text(encoding="utf-8")}
+ +""") @app.get("/qr/{alias}") async def qr(alias: str):