qsomap/web.py

52 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python3
from bottle import Bottle, request, response, template, static_file
from io import StringIO, BytesIO
import qsomap
app = application = Bottle()
@app.get('/render/map.svg')
@app.post('/render/map.svg')
def render():
svg_stream = StringIO()
adiffile = request.files.get('adif')
if adiffile and len(adiffile.filename) > 0:
if adiffile.filename[-4:] not in ['.adi', '.adf'] and adiffile.filename[-5:] not in ['.adif']:
return "File extension not allowed."
if adiffile.content_length > 1024 * 1024:
return "Maximum allowed ADIF file size: 1 MiB."
# parse ADIF data as UTF-8
adif_bytes = adiffile.file.read()
adif_unicode = adif_bytes.decode('utf-8')
print("==========================================")
print(adif_unicode)
print("==========================================")
adif_stream = StringIO(adif_unicode)
qsomap.render(float(request.params.lat), float(request.params.lon), svg_stream, adif_stream)
else:
qsomap.render(float(request.params.lat), float(request.params.lon), svg_stream, None)
response.content_type = 'image/svg+xml'
return svg_stream.getvalue()
@app.route('/')
@app.route('/index.html')
def index():
return template('index')
@app.route('/static/<filename:path>')
def send_static(filename):
return static_file(filename, root='./static')
if __name__ == "__main__":
app.run(host='localhost', port=8080, debug=True)