aboutsummaryrefslogtreecommitdiffstats
path: root/paste/store.py
diff options
context:
space:
mode:
Diffstat (limited to 'paste/store.py')
-rw-r--r--paste/store.py26
1 files changed, 25 insertions, 1 deletions
diff --git a/paste/store.py b/paste/store.py
index ed81560..dd00edd 100644
--- a/paste/store.py
+++ b/paste/store.py
@@ -1,4 +1,5 @@
-from sqlite3 import Connection
+from secrets import token_urlsafe
+from sqlite3 import Connection, IntegrityError
def put(conn: Connection, name: str, content: bytes, content_type: str):
@@ -25,6 +26,29 @@ def put(conn: Connection, name: str, content: bytes, content_type: str):
return True, content_hash
+def post(conn: Connection, prefix: str, content: bytes, content_type: str):
+ with conn:
+ conn.execute(
+ "INSERT OR IGNORE INTO file (content) VALUES (?)",
+ (content,),
+ )
+ (content_hash,) = conn.execute("SELECT DATA_HASH(?)", (content,)).fetchone()
+ for _ in range(16):
+ name = prefix + token_urlsafe(5)
+ try:
+ conn.execute(
+ """INSERT INTO link (name, content_type, file_hash)
+ VALUES (?, ?, ?)""",
+ (name, content_type, content_hash),
+ )
+ except IntegrityError:
+ continue
+ break
+ else:
+ raise RuntimeError("Could not insert a link in 16 attempts")
+ return name, content_hash
+
+
def get(conn: Connection, name: str):
row = conn.execute(
"""SELECT link.content_type, file.hash, file.content