|
@@ -1,19 +1,43 @@
|
|
|
#coding=utf-8
|
|
|
import os
|
|
|
-
|
|
|
+import io
|
|
|
+from PIL import Image
|
|
|
+from HTMLParser import HTMLParser
|
|
|
import tornado.httpserver
|
|
|
import tornado.ioloop
|
|
|
import tornado.options
|
|
|
import tornado.web
|
|
|
-
|
|
|
from tornado.options import define, options
|
|
|
define("port", default=8000, help="run on the given port", type=int)
|
|
|
|
|
|
|
|
|
+PATH=os.path.dirname(os.path.abspath(__file__))
|
|
|
+
|
|
|
+
|
|
|
+class MyHTMLParser(HTMLParser):
|
|
|
+ def __init__(self):
|
|
|
+ self.reset()
|
|
|
+ self.ttype = "p"
|
|
|
+ self.fed = []
|
|
|
+
|
|
|
+ def handle_starttag(self, tag, attrs):
|
|
|
+ self.ttype = str(tag)
|
|
|
+
|
|
|
+ def handle_endtag(self, tag):
|
|
|
+ self.ttype = "p"
|
|
|
+
|
|
|
+ def handle_data(self, data):
|
|
|
+ self.fed.append({"type": self.ttype, "text": data.strip()})
|
|
|
+
|
|
|
+ def get_data(self):
|
|
|
+ return self.fed
|
|
|
+
|
|
|
+
|
|
|
class Application(tornado.web.Application):
|
|
|
def __init__(self):
|
|
|
handlers = [
|
|
|
(r"/", MainHandler),
|
|
|
+ (r"/img", ImageHandler),
|
|
|
(r"/download", GenFileStreamerHandler),
|
|
|
]
|
|
|
settings = dict(
|
|
@@ -25,8 +49,12 @@ class Application(tornado.web.Application):
|
|
|
|
|
|
|
|
|
class MainHandler(tornado.web.RequestHandler):
|
|
|
+
|
|
|
def get(self):
|
|
|
|
|
|
+ parser = MyHTMLParser()
|
|
|
+ parser.feed("<i>Xenomorphia resurrecta</i>, male; well-preserved, head hollow")
|
|
|
+
|
|
|
self.render(
|
|
|
"index.html",
|
|
|
title = """Preservation of three-dimensional anatomy in phosphatized
|
|
@@ -46,26 +74,48 @@ class MainHandler(tornado.web.RequestHandler):
|
|
|
"Saab",
|
|
|
"Mercedes",
|
|
|
"Audi"],
|
|
|
- desc = "<i>Xenomorphia resurrecta</i>, male; well-preserved, head hollow",
|
|
|
+ desc = parser.get_data(),
|
|
|
data_size = "1.3 GB"
|
|
|
)
|
|
|
|
|
|
|
|
|
class GenFileStreamerHandler(tornado.web.RequestHandler):
|
|
|
- # 0.5 MB
|
|
|
CHUNK_SIZE = 512000
|
|
|
- #def initialize(self, file_path):
|
|
|
- # self.path = file_path
|
|
|
+
|
|
|
@tornado.web.asynchronous
|
|
|
@tornado.gen.engine
|
|
|
def get(self):
|
|
|
#print self.get_arguments("did")
|
|
|
- self.path = '/Users/ntj/Development/fossil/data/01_NRM-PZ_Ar65716/test.zip'
|
|
|
+ self.path = PATH + '/data/01_NRM-PZ_Ar65716/test.zip'
|
|
|
file_name = "01_NRM-PZ_Ar65716.zip"
|
|
|
- #self.set_header("Content-Type", "text/plain")
|
|
|
- self.set_header('Content-Type', 'application/octet-stream')
|
|
|
- self.set_header('Content-Disposition', 'attachment; filename=' + file_name)
|
|
|
- self.set_header("Content-Length", os.path.getsize(self.path))
|
|
|
+ self.set_header('Content-Type',
|
|
|
+ 'application/octet-stream')
|
|
|
+ self.set_header('Content-Disposition',
|
|
|
+ 'attachment; filename=' + file_name)
|
|
|
+ self.set_header("Content-Length",
|
|
|
+ os.path.getsize(self.path))
|
|
|
+ self.flush()
|
|
|
+ fd = open(self.path, "rb")
|
|
|
+ data = fd.read(self.CHUNK_SIZE)
|
|
|
+ while data:
|
|
|
+ self.write(data)
|
|
|
+ yield tornado.gen.Task(self.flush)
|
|
|
+ data = fd.read(self.CHUNK_SIZE)
|
|
|
+ fd.close()
|
|
|
+ self.finish()
|
|
|
+
|
|
|
+
|
|
|
+class ImageHandler(tornado.web.RequestHandler):
|
|
|
+ CHUNK_SIZE = 512000
|
|
|
+
|
|
|
+ @tornado.web.asynchronous
|
|
|
+ @tornado.gen.engine
|
|
|
+ def get(self):
|
|
|
+ #print self.get_arguments("did")
|
|
|
+ self.path = PATH + '/static/data/1.png'
|
|
|
+ self.set_header('Content-Type', 'image/png')
|
|
|
+ self.set_header("Content-Length",
|
|
|
+ os.path.getsize(self.path))
|
|
|
self.flush()
|
|
|
fd = open(self.path, "rb")
|
|
|
data = fd.read(self.CHUNK_SIZE)
|