]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/nopaste.py
6 from .misc
import Object
9 def create(self
, content
, subject
=None, mimetype
=None, expires
=None, account
=None, address
=None):
10 self
._cleanup
_database
()
13 blob_id
= self
._store
_blob
(content
)
15 # Guess the mimetype if none set
17 mimetype
= magic
.from_buffer(content
, mime
=True)
24 expires
= datetime
.datetime
.utcnow() + datetime
.timedelta(seconds
=expires
)
26 # http://blog.00null.net/easily-generating-random-strings-in-postgresql/
43 random_slug(), %s, %s, %s, %s, %s, %s, %s, %s
47 """, subject
, content
, expires
or None, address
, uid
, mimetype
, len(content
), blob_id
,
53 def _store_blob(self
, data
):
55 Stores the blob by sending it to the database and returning its ID
57 blob
= self
.db
.get("""
72 last_uploaded_at = CURRENT_TIMESTAMP
82 res
= self
.db
.get("SELECT uuid, subject, time_created, time_expires, address, uid, \
83 mimetype, views, size FROM nopaste WHERE uuid = %s AND (CASE WHEN time_expires \
84 IS NULL THEN TRUE ELSE NOW() < time_expires END)", uuid
)
87 # Get the account that uploaded this if available
90 res
.account
= self
.backend
.accounts
.get_by_uid(res
.uid
)
92 # Touch the entry so it won't be deleted when it is still used
93 self
._update
_lastseen
(uuid
)
97 def get_content(self
, uuid
):
98 res
= self
.db
.get("SELECT content FROM nopaste \
99 WHERE uuid = %s", uuid
)
102 return bytes(res
.content
)
104 def _update_lastseen(self
, uuid
):
105 self
.db
.execute("UPDATE nopaste SET time_lastseen = NOW(), views = views + 1 \
106 WHERE uuid = %s", uuid
)
108 def _cleanup_database(self
):
109 # Delete old pastes when they are expired or when they have not been
110 # accessed in a long time.
111 self
.db
.execute("DELETE FROM nopaste WHERE (CASE \
112 WHEN time_expires IS NULL \
113 THEN time_lastseen + INTERVAL '6 months' <= NOW() \
114 ELSE NOW() >= time_expires END)")