]> git.ipfire.org Git - ipfire.org.git/blobdiff - src/backend/nopaste.py
nopaste: Tidy up the cleanup task
[ipfire.org.git] / src / backend / nopaste.py
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):