3 from flask
import Flask
, abort
, request
, Response
, url_for
4 from pygments
.formatters
.html
import HtmlFormatter
5 from pygments
import highlight
6 import pygments
.lexers
as lexers
11 magic
= magic
.Magic(mime
=True)
14 Copyright (c) 2022 Immae
16 Permission is hereby granted, free of charge, to any person obtaining a copy
17 of this software and associated documentation files (the "Software"), to deal
18 in the Software without restriction, including without limitation the rights
19 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 copies of the Software, and to permit persons to whom the Software is
21 furnished to do so, subject to the following conditions:
23 The above copyright notice and this permission notice shall be included in all
24 copies or substantial portions of the Software.
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 "directory": os
.environ
["PASTE_DIRECTORY"],
37 "self_paste_id": "abcd123",
38 "license_paste_id": "license",
39 "max_content_length": 16 * 1000 * 1000
43 app
.config
['MAX_CONTENT_LENGTH'] = config
["max_content_length"]
45 def file_location(paste_id
):
46 if paste_id
== config
["self_paste_id"]:
47 return os
.path
.realpath(__file__
)
49 return os
.path
.join(config
['directory'], paste_id
+ ".dat")
51 def 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."):
58 return (content
, mime
)
59 elif paste_id
== config
["license_paste_id"]:
60 return (mit_license
, "text/plain")
64 def 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
71 return generate_paste_id(n
, attempts
)
74 @app.route('/', methods
=["GET"])
76 return Response('''<pre>
77 $ curl -X POST --data-binary @{self} {host}
85 application/octet-stream
89 tries to decode base64
91 pretty-print python (replace with anything known by pygments)
93 pretty-print (language guessed by pygments)
94 -> GET {paste}/download
95 force download of file
97 <a href="{paste}/py">Get the source</a><br />
98 Software licensed under the terms of the <a href="{host}/license">MIT license</a>
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"]),
101 license
=url_for('get_paste', _external
=True, _scheme
="https", paste_id
=config
["license_paste_id"]),
102 self
=os
.path
.basename(__file__
)
103 ), mimetype
="text/html")
105 @app.route('/', methods
=["POST"])
107 content
= request
.get_data()
108 paste_id
= generate_paste_id()
109 with open(file_location(paste_id
), "wb") as f
:
111 return url_for('get_paste', _external
=True, _scheme
="https", paste_id
=paste_id
) + "\n"
114 @app.route('/<paste_id>', methods
=["GET"])
115 def get_paste(paste_id
):
116 content
, mime
= read_paste(paste_id
)
117 return Response(content
, mimetype
=mime
)
119 @app.route('/<paste_id>/<lang>', methods
=["GET"])
120 def get_paste_with(paste_id
, lang
):
121 content
, mime
= read_paste(paste_id
)
123 return Response(content
, mimetype
="text/plain")
125 return Response(content
, mimetype
="application/octet-stream")
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:
133 cd
= 'attachment; filename="{}{}"'.format(paste_id
, extension
)
134 return Response(content
, mimetype
=mime
,
135 headers
={"Content-Disposition": cd}
)
138 return base64
.b64decode(content
)
143 lexer
= lexers
.guess_lexer(content
)
145 lexer
= lexers
.find_lexer_class_by_name(lang
)()
148 fmter
= HtmlFormatter(
149 noclasses
=True, full
=True, style
="colorful", linenos
="table")
151 return Response(highlight(content
, lexer
, fmter
), mimetype
="text/html")