Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
13 changes: 13 additions & 0 deletions modules/sqlite_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 14 additions & 4 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'")

Expand Down Expand Up @@ -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)
}


Expand All @@ -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"""<!DOCTYPE html>
<html>
<head>
<title>{paste_title}</title>
</head>
<body>
<pre>{paste_path.read_text(encoding="utf-8")}</pre>
</body>
</html>""")

@app.get("/qr/{alias}")
async def qr(alias: str):
Expand Down
Loading