-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
executable file
·45 lines (37 loc) · 1.33 KB
/
server.py
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/env python
import re
import os
import base64
import mimetypes
from bottle import route, run, static_file, redirect, request, post
def abspath(*relpath):
currdir = os.path.dirname(os.path.abspath(__file__))
return os.path.join(currdir, *relpath)
@route('/')
def index():
redirect('/static/index.html')
@route('/static/<filename:path>')
def server_static(filename):
return static_file(filename, root=abspath('static'))
@post('/savescene')
def save_scene():
name = request.POST.get('name').strip()
dataurl = request.POST.get('dataurl').strip()
data = request.POST.get('data').strip()
match = re.search(r'^data:(?P<mimetype>[^;]+);base64,(?P<data>.+)$', dataurl)
if match:
# create directory to store scene
dirname = abspath('static', 'scenes', name)
if not os.path.exists(dirname):
os.makedirs(dirname)
# save image
ext = mimetypes.guess_extension(match.group('mimetype'))
imagename = os.path.join(dirname, 'snapshot' + ext)
with open(imagename, 'wb') as image:
image.write(base64.b64decode(match.group('data')))
dataname = os.path.join(dirname, 'data.json')
with open(dataname, 'wb') as datafile:
datafile.write(data)
if __name__ == '__main__':
mimetypes.init()
run(host='localhost', port=8080)