]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/nopaste.py
10 import tornado
.iostream
11 import tornado
.tcpserver
14 from .misc
import Object
15 from .decorators
import *
18 log
= logging
.getLogger(__name__
)
20 CHUNK_SIZE
= 1024 ** 2
22 class Nopaste(Object
):
23 def _get_paste(self
, query
, *args
, **kwargs
):
24 return self
.db
.fetch_one(Paste
, query
, *args
, **kwargs
)
26 def create(self
, content
, account
, subject
=None, mimetype
=None, expires
=None, address
=None):
27 # Convert any text to bytes
28 if isinstance(content
, str):
29 content
= content
.encode("utf-8")
32 blob_id
= self
._store
_blob
(content
)
34 # Guess the mimetype if none set
36 mimetype
= magic
.from_buffer(content
, mime
=True)
39 expires
= datetime
.datetime
.utcnow() + datetime
.timedelta(seconds
=expires
)
41 # http://blog.00null.net/easily-generating-random-strings-in-postgresql/
42 paste
= self
._get
_paste
("""
57 random_slug(), %s, %s, %s, %s, %s, %s, %s
61 """, account
.uid
, subject
, expires
or None, address
, mimetype
, len(content
), blob_id
,
65 log
.info("Created a new paste (%s) of %s byte(s) from %s (%s - %s)" % (
66 paste
.uuid
, paste
.size
, paste
.address
, paste
.asn
or "N/A", paste
.country
or "N/A",
71 def _fetch_blob(self
, id):
72 blob
= self
.db
.get("""
85 def _store_blob(self
, data
):
87 Stores the blob by sending it to the database and returning its ID
89 blob
= self
.db
.get("""
101 digest(data, 'sha256')
104 last_uploaded_at = CURRENT_TIMESTAMP
114 paste
= self
._get
_paste
("""
122 expires_at >= CURRENT_TIMESTAMP
133 Removes all expired pastes and removes any unneeded blobs
135 # Remove all expired pastes
140 expires_at < CURRENT_TIMESTAMP
143 # Remove unneeded blobs
154 nopaste.blob_id = nopaste_blobs.id
160 def init(self
, id, data
):
161 self
.id, self
.data
= id, data
164 return self
.subject
or self
.uuid
170 return self
.data
.uuid
176 return self
.data
.subject
181 def created_at(self
):
182 return self
.data
.created_at
184 time_created
= created_at
189 def expires_at(self
):
190 return self
.data
.expires_at
192 time_expires
= expires_at
198 return self
.backend
.accounts
.get_by_uid(self
.data
.account
)
204 return self
.backend
.nopaste
._fetch
_blob
(self
.data
.blob_id
)
212 return self
.data
.size
218 return self
.data
.mimetype
or "application/octet-stream"
224 return self
.data
.address
230 return self
.backend
.location
.lookup("%s" % self
.address
)
236 if self
.location
and self
.location
.asn
:
237 return self
.backend
.location
.get_as(self
.location
.asn
)
243 if self
.location
and self
.location
.country_code
:
244 return self
.backend
.location
.get_country(self
.location
.country_code
)
250 Call this when this paste has been viewed/downloaded/etc.
256 last_accessed_at = CURRENT_TIMESTAMP,