From 707574bdd6d664e1ddaa55bfb725790992a0492d Mon Sep 17 00:00:00 2001 From: Tyler Hwang Date: Tue, 30 Jun 2026 20:44:17 -0700 Subject: [PATCH 1/2] Add pastes table --- modules/sqlite_helpers.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/modules/sqlite_helpers.py b/modules/sqlite_helpers.py index d44b1ff..53699af 100644 --- a/modules/sqlite_helpers.py +++ b/modules/sqlite_helpers.py @@ -13,6 +13,15 @@ def maybe_create_table(sqlite_file: str) -> bool: db = sqlite3.connect(sqlite_file) cursor = db.cursor() +#new paste table +cursor.execute(""" + CREATE TABLE IF NOT EXISTS pastes ( + alias TEXT PRIMARY KEY, + title TEXT, + content TEXT NOT NULL + ) +""") + try: create_table_query = """ CREATE TABLE IF NOT EXISTS urls ( From 90f14ee0ef46b9b920b4a207ad927437b1224c54 Mon Sep 17 00:00:00 2001 From: Tyler Hwang Date: Wed, 5 Aug 2026 18:45:13 -0700 Subject: [PATCH 2/2] Add paste API --- Dockerfile | 2 +- modules/sqlite_helpers.py | 44 +++++++++++++++++++++++++++++++-------- pastes/1 | 1 + pastes/2 | 1 + server.py | 32 +++++++++++++++++++++++++++- 5 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 pastes/1 create mode 100644 pastes/2 diff --git a/Dockerfile b/Dockerfile index 70300bc..b0927cf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.9-slim-buster +FROM python:3.9-bullseye WORKDIR /app diff --git a/modules/sqlite_helpers.py b/modules/sqlite_helpers.py index 53699af..858702a 100644 --- a/modules/sqlite_helpers.py +++ b/modules/sqlite_helpers.py @@ -13,15 +13,6 @@ def maybe_create_table(sqlite_file: str) -> bool: db = sqlite3.connect(sqlite_file) cursor = db.cursor() -#new paste table -cursor.execute(""" - CREATE TABLE IF NOT EXISTS pastes ( - alias TEXT PRIMARY KEY, - title TEXT, - content TEXT NOT NULL - ) -""") - try: create_table_query = """ CREATE TABLE IF NOT EXISTS urls ( @@ -37,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: @@ -197,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() \ No newline at end of file diff --git a/pastes/1 b/pastes/1 new file mode 100644 index 0000000..95d09f2 --- /dev/null +++ b/pastes/1 @@ -0,0 +1 @@ +hello world \ No newline at end of file diff --git a/pastes/2 b/pastes/2 new file mode 100644 index 0000000..649297a --- /dev/null +++ b/pastes/2 @@ -0,0 +1 @@ +hello evan \ No newline at end of file diff --git a/server.py b/server.py index 044d0ac..66b013d 100644 --- a/server.py +++ b/server.py @@ -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 @@ -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() @@ -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):