]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - flakes/paste/paste/paste.py
Squash changes containing private information
[perso/Immae/Config/Nix.git] / flakes / paste / paste / paste.py
CommitLineData
a9f52ec5
IB
1import os
2import secrets
3from flask import Flask, abort, request, Response, url_for
4from pygments.formatters.html import HtmlFormatter
5from pygments import highlight
6import pygments.lexers as lexers
7import base64
8import magic
9import mimetypes
10
11magic = magic.Magic(mime=True)
12
1a64deeb
IB
13mit_license = """
14Copyright (c) 2022 Immae
15
16Permission is hereby granted, free of charge, to any person obtaining a copy
17of this software and associated documentation files (the "Software"), to deal
18in the Software without restriction, including without limitation the rights
19to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20copies of the Software, and to permit persons to whom the Software is
21furnished to do so, subject to the following conditions:
22
23The above copyright notice and this permission notice shall be included in all
24copies or substantial portions of the Software.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32SOFTWARE.
33"""
34
a9f52ec5
IB
35config = {
36 "directory": os.environ["PASTE_DIRECTORY"],
37 "self_paste_id": "abcd123",
1a64deeb 38 "license_paste_id": "license",
a9f52ec5
IB
39 "max_content_length": 16 * 1000 * 1000
40 }
41
42app = Flask(__name__)
43app.config['MAX_CONTENT_LENGTH'] = config["max_content_length"]
44
45def file_location(paste_id):
46 if paste_id == config["self_paste_id"]:
47 return os.path.realpath(__file__)
48 else:
49 return os.path.join(config['directory'], paste_id + ".dat")
50
51def read_paste(paste_id):
52 file = file_location(paste_id)
53 if os.path.isfile(file):
54 content = open(file, "rb").read()
55 mime = magic.from_buffer(content)
56 if mime.startswith("text/x-script."):
57 mime="text/plain"
58 return (content, mime)
1a64deeb
IB
59 elif paste_id == config["license_paste_id"]:
60 return (mit_license, "text/plain")
a9f52ec5
IB
61 else:
62 abort(404)
63
64def generate_paste_id(n=3, attempts=0):
65 path = secrets.token_hex(n)[:n]
66 file = file_location(path)
67 if os.path.isfile(file):
68 attempts = attempts + 1
69 if attempts > 5:
70 n = n + 1
71 return generate_paste_id(n, attempts)
72 return path
73
74@app.route('/', methods=["GET"])
75def index():
76 return Response('''<pre>
77$ curl -X POST --data-binary @{self} {host}
78{paste}
79
80-> GET {paste}
81 guesses mimetype
82-> GET {paste}/raw
83 text/plain
84-> GET {paste}/bin
85 application/octet-stream
86-> GET {paste}/b64
87 base64 encoded
88-> GET {paste}/ub64
89 tries to decode base64
90-> GET {paste}/python
91 pretty-print python (replace with anything known by pygments)
92-> GET {paste}/guess
93 pretty-print (language guessed by pygments)
94-> GET {paste}/download
95 force download of file
96</pre>
1a64deeb
IB
97<a href="{paste}/py">Get the source</a><br />
98Software licensed under the terms of the <a href="{host}/license">MIT license</a>
a9f52ec5
IB
99'''.format(host=url_for('post_paste', _external=True, _scheme="https"),
100 paste=url_for('get_paste', _external=True, _scheme="https", paste_id=config["self_paste_id"]),
1a64deeb 101 license=url_for('get_paste', _external=True, _scheme="https", paste_id=config["license_paste_id"]),
a9f52ec5
IB
102 self=os.path.basename(__file__)
103 ), mimetype="text/html")
104
105@app.route('/', methods=["POST"])
106def post_paste():
107 content = request.get_data()
108 paste_id = generate_paste_id()
109 with open(file_location(paste_id), "wb") as f:
110 f.write(content)
111 return url_for('get_paste', _external=True, _scheme="https", paste_id=paste_id) + "\n"
112
113
114@app.route('/<paste_id>', methods=["GET"])
115def get_paste(paste_id):
116 content, mime = read_paste(paste_id)
117 return Response(content, mimetype=mime)
118
119@app.route('/<paste_id>/<lang>', methods=["GET"])
120def get_paste_with(paste_id, lang):
121 content, mime = read_paste(paste_id)
122 if lang == "raw":
123 return Response(content, mimetype="text/plain")
124 elif lang == "bin":
125 return Response(content, mimetype="application/octet-stream")
126 elif lang == "b64":
127 return Response(base64.encodebytes(content), mimetype="text/plain")
128 elif lang == "download":
129 extension = mimetypes.guess_extension(mime, strict=False)
130 if extension is None:
131 cd = "attachment"
132 else:
133 cd = 'attachment; filename="{}{}"'.format(paste_id, extension)
134 return Response(content, mimetype=mime,
135 headers={"Content-Disposition": cd})
136 elif lang == "ub64":
137 try:
138 return base64.b64decode(content)
139 except:
140 abort(400)
141 try:
142 if lang == "guess":
143 lexer = lexers.guess_lexer(content)
144 else:
145 lexer = lexers.find_lexer_class_by_name(lang)()
146 except:
147 abort(400)
148 fmter = HtmlFormatter(
149 noclasses=True, full=True, style="colorful", linenos="table")
150
151 return Response(highlight(content, lexer, fmter), mimetype="text/html")