summaryrefslogtreecommitdiffstats
path: root/pasteme.py
blob: fce1655e1e2569bb818de6ab49094f1a6893ab53 (plain)
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
#!/usr/bin/python3

import bottle
import identigen
import config
from pathlib import Path

pathbase = Path(config.pastedir)

if not pathbase.exists():
    pathbase.mkdir(mode=0o700, parents=True)

@bottle.route('/')
def route_root():
    return bottle.template('root')

@bottle.route('/', method='POST')
def route_paste_post():
    content = bottle.request.forms.get('content')
    pid = identigen.generate(content)
    path = pathbase / pid
    with path.open(mode='wb') as fd:
        fd.write(content.encode('utf8'))
    bottle.redirect('/' + pid)

@bottle.route('/<pid>')
@bottle.route('/<pid>/<pformat>')
def route_paste_get(pid, pformat='colored'):
    if pformat != 'colored' and pformat != 'raw':
        return bottle.template('bad_format')
    path = pathbase / pid
    try:
        with path.open() as fd:
            content = fd.read()
    except IOError:
        # use this template for all file based exception
        return bottle.template('not_found')
    return bottle.template('paste', content=content)

if __name__ == '__main__':
    print('I: Starting application with development server')
    bottle.run(host='0.0.0.0', port=8080, debug=True, reloader=True)
else:
    print('I: Starting application as a wsgi application')
    application = bottle.default_app() # application used for wsgi mode