qsomap/web.py

52 lines
1.5 KiB
Python
Raw Normal View History

2023-11-13 20:04:45 +01:00
#!/usr/bin/env python3
from bottle import Bottle, request, response, template, static_file
2023-11-16 22:45:44 +01:00
from io import StringIO, BytesIO
2023-11-13 20:04:45 +01:00
import qsomap
app = application = Bottle()
2023-11-16 22:45:44 +01:00
@app.get('/render/map.svg')
@app.post('/render/map.svg')
def render():
2023-11-13 20:04:45 +01:00
svg_stream = StringIO()
2023-11-16 22:45:44 +01:00
adiffile = request.files.get('adif')
if adiffile and adiffile.filename != "empty":
2023-11-16 22:45:44 +01:00
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)
2023-11-13 20:04:45 +01:00
response.content_type = 'image/svg+xml'
return svg_stream.getvalue()
2023-11-13 22:58:46 +01:00
@app.route('/')
@app.route('/index.html')
2023-11-13 22:58:46 +01:00
def index():
return template('index')
@app.route('/static/<filename:path>')
2023-11-13 22:58:46 +01:00
def send_static(filename):
return static_file(filename, root='./static')
if __name__ == "__main__":
2023-11-16 22:45:44 +01:00
app.run(host='localhost', port=8080, debug=True)