]> git.ipfire.org Git - ipfire.org.git/commitdiff
nopaste: Tidy up the cleanup task
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Feb 2024 19:00:45 +0000 (19:00 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Feb 2024 19:00:45 +0000 (19:00 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/base.py
src/backend/nopaste.py

index 4af92e275728ee3c607e2186f03ba2fe261a6e2e..9f013f906423e809378a1a7da9354aa077b696db 100644 (file)
@@ -234,3 +234,7 @@ class Backend(object):
                # Cleanup in accounts
                with self.db.transaction():
                        self.accounts.cleanup()
+
+               # Cleanup nopasts
+               with self.db.transaction():
+                       self.nopaste.cleanup()
index 1d7ecad01faefdd656c3328471857c88be1b061f..7cda2b8af6a1a510459a9724f9901f829b4ca724 100644 (file)
@@ -22,8 +22,6 @@ class Nopaste(Object):
                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()
-
                # Store the blob
                blob_id = self._store_blob(content)
 
@@ -140,13 +138,32 @@ class Nopaste(Object):
                self.db.execute("UPDATE nopaste SET time_lastseen = NOW(), views = views + 1 \
                        WHERE uuid = %s", uuid)
 
-       def _cleanup_database(self):
-               # Delete old pastes when they are expired or when they have not been
-               # accessed in a long time.
-               self.db.execute("DELETE FROM nopaste WHERE (CASE \
-                       WHEN time_expires IS NULL \
-                               THEN time_lastseen + INTERVAL '6 months' <= NOW() \
-                       ELSE NOW() >= time_expires END)")
+       def cleanup(self):
+               """
+                       Removes all expired pastes and removes any unneeded blobs
+               """
+               # Remove all expired pastes
+               self.db.execute("""
+                       DELETE FROM
+                               nopaste
+                       WHERE
+                               expires_at < CURRENT_TIMESTAMP
+               """)
+
+               # Remove unneeded blobs
+               self.db.execute("""
+                       DELETE FROM
+                               nopaste_blobs
+                       WHERE NOT EXISTS
+                       (
+                               SELECT
+                                       1
+                               FROM
+                                       nopaste
+                               WHERE
+                                       nopaste.blob_id = nopaste_blobs.id
+                       )
+               """)
 
 
 class Paste(Object):