]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/nopaste.py
5 from .misc
import Object
8 def create(self
, subject
, content
, type="text", expires
=None, account
=None, address
=None):
9 self
._cleanup
_database
()
16 mimetype
= "text/plain"
19 mimetype
= self
._guess
_mimetype
(content
)
21 # http://blog.00null.net/easily-generating-random-strings-in-postgresql/
22 res
= self
.db
.get("INSERT INTO nopaste(uuid, subject, content, time_expires, address, \
23 uid, mimetype, size) VALUES(random_slug(), %s, %s, \
24 (CASE WHEN %s = 0 THEN NULL ELSE NOW() + INTERVAL '%s seconds' END), \
25 %s, %s, %s, %s) RETURNING uuid",
26 subject
, content
, expires
, expires
, address
, uid
, mimetype
, len(content
))
31 def _guess_mimetype(self
, buf
):
32 ms
= magic
.open(magic
.NONE
)
35 # Return the mime type
36 ms
.setflags(magic
.MAGIC_MIME_TYPE
)
44 res
= self
.db
.get("SELECT uuid, subject, time_created, time_expires, address, uid, \
45 mimetype, views, size FROM nopaste WHERE uuid = %s AND (CASE WHEN time_expires \
46 IS NULL THEN TRUE ELSE NOW() < time_expires END)", uuid
)
49 # Get the account that uploaded this if available
52 res
.account
= self
.backend
.accounts
.get_by_uid(res
.uid
)
54 # Touch the entry so it won't be deleted when it is still used
55 self
._update
_lastseen
(uuid
)
59 def get_content(self
, uuid
):
60 res
= self
.db
.get("SELECT content FROM nopaste \
61 WHERE uuid = %s", uuid
)
64 return bytes(res
.content
)
66 def _update_lastseen(self
, uuid
):
67 self
.db
.execute("UPDATE nopaste SET time_lastseen = NOW(), views = views + 1 \
68 WHERE uuid = %s", uuid
)
70 def _cleanup_database(self
):
71 # Delete old pastes when they are expired or when they have not been
72 # accessed in a long time.
73 self
.db
.execute("DELETE FROM nopaste WHERE (CASE \
74 WHEN time_expires IS NULL \
75 THEN time_lastseen + INTERVAL '6 months' <= NOW() \
76 ELSE NOW() >= time_expires END)")