]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/nopaste.py
5 from .misc
import Object
8 def create(self
, subject
, content
, mimetype
="text", expires
=None, account
=None, address
=None):
9 self
._cleanup
_database
()
16 expires
= datetime
.datetime
.utcnow() + datetime
.timedelta(seconds
=expires
)
18 # http://blog.00null.net/easily-generating-random-strings-in-postgresql/
19 res
= self
.db
.get("INSERT INTO nopaste(uuid, subject, content, time_expires, address, \
20 uid, mimetype, size) VALUES(random_slug(), %s, %s, %s, %s, %s, %s, %s) RETURNING uuid",
21 subject
, content
, expires
or None, address
, uid
, mimetype
, len(content
))
27 res
= self
.db
.get("SELECT uuid, subject, time_created, time_expires, address, uid, \
28 mimetype, views, size FROM nopaste WHERE uuid = %s AND (CASE WHEN time_expires \
29 IS NULL THEN TRUE ELSE NOW() < time_expires END)", uuid
)
32 # Get the account that uploaded this if available
35 res
.account
= self
.backend
.accounts
.get_by_uid(res
.uid
)
37 # Touch the entry so it won't be deleted when it is still used
38 self
._update
_lastseen
(uuid
)
42 def get_content(self
, uuid
):
43 res
= self
.db
.get("SELECT content FROM nopaste \
44 WHERE uuid = %s", uuid
)
47 return bytes(res
.content
)
49 def _update_lastseen(self
, uuid
):
50 self
.db
.execute("UPDATE nopaste SET time_lastseen = NOW(), views = views + 1 \
51 WHERE uuid = %s", uuid
)
53 def _cleanup_database(self
):
54 # Delete old pastes when they are expired or when they have not been
55 # accessed in a long time.
56 self
.db
.execute("DELETE FROM nopaste WHERE (CASE \
57 WHEN time_expires IS NULL \
58 THEN time_lastseen + INTERVAL '6 months' <= NOW() \
59 ELSE NOW() >= time_expires END)")