def create(self, content, subject=None, mimetype=None, expires=None, account=None, address=None):
self._cleanup_database()
+ # Store the blob
+ blob_id = self._store_blob(content)
+
# Guess the mimetype if none set
if not mimetype:
mimetype = magic.from_buffer(content, mime=True)
expires = datetime.datetime.utcnow() + datetime.timedelta(seconds=expires)
# http://blog.00null.net/easily-generating-random-strings-in-postgresql/
- res = self.db.get("INSERT INTO nopaste(uuid, subject, content, time_expires, address, \
- uid, mimetype, size) VALUES(random_slug(), %s, %s, %s, %s, %s, %s, %s) RETURNING uuid",
- subject, content, expires or None, address, uid, mimetype, len(content))
+ res = self.db.get("""
+ INSERT INTO
+ nopaste
+ (
+ uuid,
+ subject,
+ content,
+ time_expires,
+ address,
+ uid,
+ mimetype,
+ size,
+ blob_id
+ )
+ VALUES
+ (
+ random_slug(), %s, %s, %s, %s, %s, %s, %s, %s
+ )
+ RETURNING
+ uuid
+ """, subject, content, expires or None, address, uid, mimetype, len(content), blob_id,
+ )
if res:
return res.uuid
+ def _store_blob(self, data):
+ """
+ Stores the blob by sending it to the database and returning its ID
+ """
+ blob = self.db.get("""
+ INSERT INTO
+ nopaste_blobs
+ (
+ data
+ )
+ VALUES
+ (
+ %s
+ )
+ ON CONFLICT
+ (
+ data
+ )
+ DO UPDATE SET
+ last_uploaded_at = CURRENT_TIMESTAMP
+ RETURNING
+ id
+ """, data
+ )
+
+ # Return the ID
+ return blob.id
+
def get(self, uuid):
res = self.db.get("SELECT uuid, subject, time_created, time_expires, address, uid, \
mimetype, views, size FROM nopaste WHERE uuid = %s AND (CASE WHEN time_expires \