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