]> git.ipfire.org Git - ipfire.org.git/commitdiff
nopaste: Store blobs in a separate table
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Feb 2024 18:24:11 +0000 (18:24 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Feb 2024 18:24:11 +0000 (18:24 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/nopaste.py

index 7224ebc3c51ac33817a636d02ccb8abda6f734b4..d6101a26627553214c711104993b1015fb68c4d9 100644 (file)
@@ -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 \