From: Michael Tremer Date: Thu, 22 Feb 2024 18:24:11 +0000 (+0000) Subject: nopaste: Store blobs in a separate table X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2268f20bd162806f32889afa09e8fd0a17771ee5;p=ipfire.org.git nopaste: Store blobs in a separate table Signed-off-by: Michael Tremer --- diff --git a/src/backend/nopaste.py b/src/backend/nopaste.py index 7224ebc3..d6101a26 100644 --- a/src/backend/nopaste.py +++ b/src/backend/nopaste.py @@ -9,6 +9,9 @@ class Nopaste(Object): def create(self, content, subject=None, mimetype=None, expires=None, account=None, address=None): self._cleanup_database() + # Store the blob + blob_id = self._store_blob(content) + # Guess the mimetype if none set if not mimetype: mimetype = magic.from_buffer(content, mime=True) @@ -21,13 +24,60 @@ 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("INSERT INTO nopaste(uuid, subject, content, time_expires, address, \ - uid, mimetype, size) VALUES(random_slug(), %s, %s, %s, %s, %s, %s, %s) RETURNING uuid", - subject, content, expires or None, address, uid, mimetype, len(content)) + res = self.db.get(""" + INSERT INTO + nopaste + ( + uuid, + subject, + content, + time_expires, + address, + uid, + mimetype, + size, + blob_id + ) + VALUES + ( + 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 + def _store_blob(self, data): + """ + Stores the blob by sending it to the database and returning its ID + """ + blob = self.db.get(""" + INSERT INTO + nopaste_blobs + ( + data + ) + VALUES + ( + %s + ) + ON CONFLICT + ( + data + ) + DO UPDATE SET + last_uploaded_at = CURRENT_TIMESTAMP + RETURNING + id + """, data + ) + + # Return the ID + 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 \