Commit 968ac64e authored by Thomas Tsiakalakis's avatar Thomas Tsiakalakis

support non-svg images + error handling

parent 60dc969f
......@@ -4,12 +4,13 @@ Usage:
./server.py [<port>]
"""
import sys
import struct
import atexit
from http.server import BaseHTTPRequestHandler, HTTPServer
from subprocess import Popen, PIPE, STDOUT
from PIL import Image
from io import BytesIO
import struct
import sys
from os import path, chdir
FRAMEBUFFER = 'fb0'
......@@ -58,18 +59,17 @@ class Server(BaseHTTPRequestHandler):
self.wfile.write(bytes('Empty payload', 'utf-8'))
return False
if not process(post_data, self):
self.send_response(500)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(bytes('Internal Server Eror', 'utf-8'))
return False
return True
return process(post_data, self)
def process(svg, http):
try:
image = render(svg)
except OSError:
except OSError as e:
print(e)
http.send_response(500)
http.send_header('Content-type', 'text/plain')
http.end_headers()
http.wfile.write(bytes('Internal Server Eror', 'utf-8'))
return False
if not check_size(image, http):
return False
......@@ -79,13 +79,20 @@ def process(svg, http):
print('done')
return True
def render(svg):
print('rendering')
args = [RSVGCONVERT] + RSVGCONVERT_ARGS
rsvg = Popen(args, stdout=PIPE, stdin=PIPE, stderr=PIPE)
stdout_data = rsvg.communicate(input=svg)[0]
im = Image.open(BytesIO(stdout_data)).convert('RGBA')
return im
def render(payload):
if payload[0:4] == b'<?xm':
print('detected SVG')
print('rendering')
args = [RSVGCONVERT] + RSVGCONVERT_ARGS
rsvg = Popen(args, stdout=PIPE, stdin=PIPE, stderr=PIPE)
imageData = rsvg.communicate(input=payload)[0]
else:
print('detected non-SVG')
imageData = payload
f = open('test', 'w+b')
f.write(payload)
f.close()
return Image.open(BytesIO(imageData)).convert('RGBA')
def check_size(image, http):
w, h = image.size
......@@ -113,11 +120,18 @@ def draw(image):
def display_off():
args = [BLANKINGCMD] + BLANKINGARGS_OFF
Popen(args, stdout=PIPE, stdin=PIPE, stderr=PIPE)
try:
Popen(args, stdout=PIPE, stdin=PIPE, stderr=PIPE)
except Exception as e:
print(e)
def display_on():
args = [BLANKINGCMD] + BLANKINGARGS_ON
Popen(args, stdout=PIPE, stdin=PIPE, stderr=PIPE)
try:
Popen(args, stdout=PIPE, stdin=PIPE, stderr=PIPE)
except Exception as e:
print(e)
def init():
initfile = '%dX%d.svg' % (buf_width, buf_height)
......@@ -150,9 +164,11 @@ def shutdown():
print('exiting')
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
atexit.register(shutdown)
try:
if len(sys.argv) == 2:
run(port=int(sys.argv[1]))
else:
run()
except KeyboardInterrupt as e:
print(e)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment