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)
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):