From: Michael Tremer Date: Thu, 22 Feb 2024 18:50:07 +0000 (+0000) Subject: nopaste: Create an extra object for pastes X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=672150b27b0e44332c71ac265f6c268453c86a06;p=ipfire.org.git nopaste: Create an extra object for pastes Signed-off-by: Michael Tremer --- diff --git a/src/backend/nopaste.py b/src/backend/nopaste.py index 42b5fab9..1d7ecad0 100644 --- a/src/backend/nopaste.py +++ b/src/backend/nopaste.py @@ -10,6 +10,7 @@ import tornado.tcpserver from . import base from .misc import Object +from .decorators import * # Setup logging log = logging.getLogger(__name__) @@ -17,6 +18,9 @@ log = logging.getLogger(__name__) CHUNK_SIZE = 1024 ** 2 class Nopaste(Object): + def _get_paste(self, query, *args, **kwargs): + return self.db.fetch_one(Paste, query, *args, **kwargs) + def create(self, content, subject=None, mimetype=None, expires=None, account=None, address=None): self._cleanup_database() @@ -35,7 +39,7 @@ class Nopaste(Object): expires = datetime.datetime.utcnow() + datetime.timedelta(seconds=expires) # http://blog.00null.net/easily-generating-random-strings-in-postgresql/ - res = self.db.get(""" + paste = self._get_paste(""" INSERT INTO nopaste ( @@ -54,12 +58,30 @@ class Nopaste(Object): random_slug(), %s, %s, %s, %s, %s, %s, %s, %s ) RETURNING - uuid + * """, subject, content, expires or None, address, uid, mimetype, len(content), blob_id, ) - if res: - return res.uuid + # Log result + log.info("Created a new paste (%s) of %s byte(s) from %s (%s - %s)" % ( + paste.uuid, paste.size, paste.address, paste.asn or "N/A", paste.country or "N/A", + )) + + return paste + + def _fetch_blob(self, id): + blob = self.db.get(""" + SELECT + data + FROM + nopaste_blobs + WHERE + id = %s + """, id, + ) + + if blob: + return blob.data def _store_blob(self, data): """ @@ -90,20 +112,22 @@ class Nopaste(Object): return blob.id def get(self, uuid): - res = self.db.get("SELECT uuid, subject, time_created, time_expires, address, uid, \ - mimetype, views, size FROM nopaste WHERE uuid = %s AND (CASE WHEN time_expires \ - IS NULL THEN TRUE ELSE NOW() < time_expires END)", uuid) - - if res: - # Get the account that uploaded this if available - res.account = None - if res.uid: - res.account = self.backend.accounts.get_by_uid(res.uid) - - # Touch the entry so it won't be deleted when it is still used - self._update_lastseen(uuid) + paste = self._get_paste(""" + SELECT + * + FROM + nopaste + WHERE + uuid = %s + AND ( + expires_at >= CURRENT_TIMESTAMP + OR + expires_at IS NULL + ) + """, uuid, + ) - return res + return paste def get_content(self, uuid): res = self.db.get("SELECT content FROM nopaste \ @@ -171,6 +195,12 @@ class Paste(Object): def size(self): return self.data.size + # MIME Type + + @property + def mimetype(self): + return self.data.mimetype or "application/octet-stream" + # Address @property @@ -197,6 +227,12 @@ class Paste(Object): if self.location and self.location.country_code: return self.backend.location.get_country(self.location.country_code) + # Legacy + + @property + def account(self): + return None + class Service(tornado.tcpserver.TCPServer): def __init__(self, config, **kwargs): diff --git a/src/web/nopaste.py b/src/web/nopaste.py index cfedc9c3..4791e9b9 100644 --- a/src/web/nopaste.py +++ b/src/web/nopaste.py @@ -46,13 +46,11 @@ class CreateHandler(base.AnalyticsMixin, base.BaseHandler): except (TypeError, ValueError): expires = None - uid = self.backend.nopaste.create(content, subject=subject, mimetype=mimetype, + paste = self.backend.nopaste.create(content, subject=subject, mimetype=mimetype, expires=expires, account=self.current_user, address=self.get_remote_ip()) - if uid: - return self.redirect("/view/%s" % uid) - - raise tornado.web.HTTPError(500) + # Redirect the user + return self.redirect("/view/%s" % paste.uuid) @property def _max_size(self):