1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import secrets
def insert_file(conn, filename: str, expiration_days: int, content_length: int):
cur = conn.cursor()
file_id = secrets.token_urlsafe()
cur.execute(
'INSERT INTO files(id, filename, created, expires, content_length) VALUES(?, ?, datetime(), datetime(datetime(), ?), ?)',
(file_id, filename, f'+{expiration_days} days', content_length)
)
conn.commit()
return file_id
def get_file(conn, file_id: str):
cur = conn.cursor()
res = cur.execute(
'''
SELECT filename, expires, content_length
FROM files
WHERE id = ? AND expires > datetime()
''',
(file_id,)
)
return res.fetchone()
|