1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
# Copyright (C) 2025 John Turner
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import sys
import asyncio
import secrets
import aiohttp
import aiosqlite
import zstandard
from . import s3
from hashlib import sha256
from argparse import ArgumentParser
from aiohttp import web
from datetime import datetime, UTC
from pathlib import Path
from dataclasses import dataclass
from typing import Optional, List
from pygments import highlight
from pygments.lexers import guess_lexer, get_lexer_by_name
from pygments.formatters import HtmlFormatter
from pygments.styles import get_style_by_name
def pygmentize(
content: str, syntax: Optional[str], style: str, line_numbers: str | bool
) -> str:
if syntax is not None:
try:
lexer = get_lexer_by_name(syntax)
except Exception as e:
print(e, file=sys.stderr)
lexer = guess_lexer(content)
else:
lexer = guess_lexer(content)
try:
style = get_style_by_name(style)
except Exception as e:
print(e, file=sys.stderr)
style = "default"
formatter = HtmlFormatter(full=True, style=style, linenos="table")
return highlight(content, lexer, formatter)
def generate_key(words: List[str], length: int) -> str:
choices = []
for _ in range(length):
choices.append(secrets.choice(words))
return "-".join(word for word in choices).lower()
@dataclass
class PasteRow:
key: str
date: datetime
size: int
syntax: Optional[str]
class Database:
def __init__(self, path: Path):
self.path = path
async def insert(self, paste: PasteRow):
async with aiosqlite.connect(self.path) as db:
await db.execute(
"insert into pastes values(?, ?, ?, ?)",
(
paste.key,
paste.date.isoformat(),
paste.size,
paste.syntax,
),
)
await db.commit()
async def delete(self, key: str):
async with aiosqlite.connect(self.path) as db:
await db.execute("delete from pastes where key=?", (key,))
await db.commit()
async def exists(self, key: str) -> bool:
async with aiosqlite.connect(self.path) as db:
result = await db.execute("select 1 from pastes where pastes.key=?", (key,))
return await result.fetchone() is not None
async def oldest(self) -> Optional[str]:
async with aiosqlite.connect(self.path) as db:
result = await db.execute(
"select pastes.key from pastes order by pastes.datetime limit 1",
)
row = await result.fetchone()
return row[0]
async def storage_use(self) -> Optional[int]:
async with aiosqlite.connect(self.path) as db:
result = await db.execute("select sum(pastes.size) from pastes")
row = await result.fetchone()
return row[0]
@dataclass
class AppConfig:
site: str
content_length_max_bytes: int
s3_max_bytes: int
key_length: int
dictionary: List[str]
default_style: str
line_numbers: str | bool
class App:
def __init__(
self,
config: AppConfig,
database: Database,
bucket: s3.Bucket,
):
self.database = database
self.config = config
self.bucket = bucket
async def download(self, request: web.Request) -> web.Response:
try:
key = request.match_info["key"]
except KeyError:
return web.HTTPBadRequest(text="provide a key to fetch")
if not await self.database.exists(key):
return web.HTTPNotFound()
req = self.bucket.get(key)
try:
async with aiohttp.ClientSession().get(req.url, headers=req.headers) as get:
if get.status == 200:
data = await get.read()
else:
print(
f"{self.bucket.endpoint} returned status ({get.status}) while fetching {key}"
)
return web.HTTPInternalServerError()
except Exception as e:
print(f"failed to get {key} from s3: {e}", file=sys.stderr)
return web.HTTPInternalServerError()
def decompress():
return zstandard.decompress(data)
try:
decompressed = await asyncio.to_thread(decompress)
except Exception as e:
print(f"failed to decompress blob {key}: {e}", file=sys.stderr)
return web.HTTPInternalServerError()
try:
text = decompressed.decode()
except Exception as e:
print(f"failed to decode blob: {key}: {e}", file=sys.stderr)
return web.HTTPInternalServerError()
syntax = request.query.get("syntax")
raw = request.query.get("raw")
if (style := request.query.get("style")) is None:
style = self.config.default_style
if raw is not None:
return web.HTTPOk(text=text, content_type="text/plain")
else:
def render():
return pygmentize(text, syntax, style, self.config.line_numbers)
highlighted = await asyncio.to_thread(render)
return web.HTTPOk(text=highlighted, content_type="text/html")
async def upload(self, request: web.Request) -> web.Response:
try:
await self.vacuum()
except Exception as e:
print(f"vacuum failed: {e}")
return web.HTTPInternalServerError()
match request.content_length:
case int(i) if i > self.config.content_length_max_bytes:
return web.HTTPBadRequest(
text=f"max content length is {self.config.content_length_max_bytes}"
)
case _:
pass
try:
data = await request.read()
except Exception as e:
print(f"failed to read data: {e}", file=sys.stderr)
return web.HTTPInternalServerError(text="failed to read data")
try:
data.decode()
except UnicodeError:
return web.HTTPBadRequest(
text="content must be unicode only, no binary data is allowed"
)
def compress():
return zstandard.compress(data)
try:
compressed = await asyncio.to_thread(compress)
except Exception as e:
print(f"failed to compress data: {e}", file=sys.stderr)
return web.HTTPInternalServerError()
key = generate_key(self.config.dictionary, self.config.key_length)
req = self.bucket.put(
key,
len(compressed),
"application/octet-stream",
sha256(compressed).hexdigest(),
)
try:
async with aiohttp.ClientSession().put(
req.url, headers=req.headers, data=compressed
) as put:
if put.status != 200:
print(f"failed to put {key} to bucket with status: {put.status}")
return web.HTTPInternalServerError()
except Exception as e:
print(f"failed to put {key} to bucket: {e}")
return web.HTTPInternalServerError()
try:
await self.database.insert(
PasteRow(
key, datetime.now(UTC), len(compressed), request.query.get("syntax")
)
)
except Exception as e:
print(f"failed to insert {key} into database: {e}", file=sys.stderr)
return web.HTTPInternalServerError()
return web.HTTPOk(text=f"https://{self.config.site}/paste/{key}")
async def vacuum(self):
while (
use := await self.database.storage_use()
) is not None and use > self.config.s3_max_bytes:
oldest = await self.database.oldest()
# If use is not None, there must be at least 1 paste, so we let the
# type checker know that this is an unreachable case.
assert oldest is not None
req = self.bucket.delete(oldest)
async with aiohttp.ClientSession().delete(
req.url, headers=req.headers
) as delete:
if delete.status != 200:
print(
f"failed to delete {oldest}: got status {delete.status}",
file=sys.stderr,
)
else:
print(f"successfully deleted {oldest}", file=sys.stderr)
await self.database.delete(oldest)
def main() -> int:
parser = ArgumentParser()
parser.add_argument("--host")
parser.add_argument("--port", type=int)
parser.add_argument("--path")
parser.add_argument("--site", required=True)
parser.add_argument("--content-length-max-bytes", type=int, required=True)
parser.add_argument("--key-length", type=int, required=True)
parser.add_argument("--dictionary", type=Path, required=True)
parser.add_argument("--database", type=Path, required=True)
parser.add_argument("--endpoint", required=True)
parser.add_argument("--region", required=True)
parser.add_argument("--bucket", required=True)
parser.add_argument("--access-key", required=True)
parser.add_argument("--secret-key", type=Path, required=True)
parser.add_argument("--s3-max-bytes", type=int, required=True)
parser.add_argument("--default-style", default="native")
parser.add_argument("--line-numbers", action="store_true")
parser.add_argument("--line-numbers-inline", action="store_true")
args = parser.parse_args()
try:
secret_key = args.secret_key.read_text().strip()
except Exception as e:
print(
f"failed to read secret key from: {str(args.secret_key)}: {e}",
file=sys.stderr,
)
return 1
try:
dictionary = args.dictionary.read_text().split("\n")
except Exception as e:
print(f"failed to open dictionary: {str(args.dictionary)}: {e}")
return 1
match [args.line_numbers, args.line_numbers_inline]:
case [True, _]:
line_numbers: str | bool = "table"
case [_, True]:
line_numbers = "inline"
case [_, _]:
line_numbers = False
config = AppConfig(
args.site,
args.content_length_max_bytes,
args.s3_max_bytes,
args.key_length,
dictionary,
args.default_style,
line_numbers,
)
if not args.database.is_file():
print(f"{args.database} does not exist or is not a file", file=sys.stderr)
return 1
try:
database = Database(args.database)
except Exception as e:
print(f"failed to open database: {e}", file=sys.stderr)
return 1
bucket = s3.Bucket(
args.endpoint,
args.region,
args.bucket,
args.access_key,
secret_key,
)
webapp = web.Application()
app = App(config, database, bucket)
webapp.add_routes(
[web.get("/paste/{key}", app.download), web.post("/paste", app.upload)]
)
web.run_app(webapp, host=args.host, port=args.port, path=args.path)
return 0
if __name__ == "__main__":
sys.exit(main())
|