Skip to content
Open
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.9-slim-buster
FROM python:3.9-bullseye

WORKDIR /app

Expand Down
35 changes: 35 additions & 0 deletions modules/sqlite_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@ def maybe_create_table(sqlite_file: str) -> bool:
CREATE UNIQUE INDEX IF NOT EXISTS idx_urls_alias
ON urls (alias);
"""
create_pastes_table_query = """
CREATE TABLE IF NOT EXISTS pastes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
"""

cursor.execute(create_table_query)
cursor.execute(create_index_query)
cursor.execute(create_pastes_table_query)

db.commit()
return True
except Exception:
Expand Down Expand Up @@ -188,3 +196,30 @@ def increment_used_column(sqlite_file, alias: str, count=1):
finally:
cursor.close()
db.close()
def insert_text_paste(sqlite_file: str) -> typing.Optional[int]:
db = sqlite3.connect(sqlite_file)
cursor = db.cursor()
try:
cursor.execute("INSERT INTO pastes DEFAULT VALUES")
db.commit()
return cursor.lastrowid
except Exception:
logger.exception("Inserting paste had an error")
return None
finally:
cursor.close()
db.close()


def paste_exists(sqlite_file: str, paste_id: int) -> bool:
db = sqlite3.connect(sqlite_file)
cursor = db.cursor()
try:
cursor.execute("SELECT id FROM pastes WHERE id = ?", (paste_id,))
return cursor.fetchone() is not None
except Exception:
logger.exception("Getting paste had an error")
return False
finally:
cursor.close()
db.close()
1 change: 1 addition & 0 deletions pastes/1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
1 change: 1 addition & 0 deletions pastes/2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello evan
32 changes: 31 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional
from fastapi.responses import RedirectResponse, HTMLResponse, FileResponse, PlainTextResponse
from fastapi import FastAPI, Request, HTTPException, Response
from fastapi.responses import RedirectResponse, HTMLResponse, FileResponse
from fastapi.middleware.cors import CORSMiddleware
import logging
import time
Expand All @@ -18,6 +18,13 @@
from modules.cache import Cache
from modules.qr_code import QRCode

from pathlib import Path
import os

PASTES_DIR = Path("pastes")
PASTES_DIR.mkdir(exist_ok=True)

MAX_PASTE_SIZE_BYTES = 10 * 1024 * 1024

app = FastAPI()
args = get_args()
Expand Down Expand Up @@ -155,6 +162,29 @@ async def delete_url(alias: str):
return {"message": "URL deleted successfully"}
else:
raise HTTPException(status_code=HttpResponse.NOT_FOUND.code)
@app.post("/paste/create")
async def create_paste(request: Request):
body = await request.json()
text = body.get("text")
if text is None:
raise HTTPException(status_code=HttpResponse.BAD_REQUEST.code)

paste_id = sqlite_helpers.insert_text_paste(DATABASE_FILE)
if paste_id is None:
raise HTTPException(status_code=500)

paste_path = PASTES_DIR / str(paste_id)
paste_path.write_text(text, encoding="utf-8")

return {"id": paste_id}


@app.get("/paste/view/{paste_id}")
async def view_paste(paste_id: int):
paste_path = PASTES_DIR / str(paste_id)
if not paste_path.exists():
raise HTTPException(status_code=HttpResponse.NOT_FOUND.code)
return PlainTextResponse(paste_path.read_text(encoding="utf-8"))

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