Browse Source

image handler updated for latest folder structure.

Signed-off-by: root <root@ipekatrinadei.ipe.kit.edu>
root 6 years ago
parent
commit
29970beb2f
3 changed files with 44 additions and 170 deletions
  1. 0 159
      index.py
  2. 35 6
      main.py
  3. 9 5
      templates/index.html

+ 0 - 159
index.py

@@ -1,159 +0,0 @@
-#!/usr/bin/python
-# -*- coding: utf-8 -*-
-import os
-import io
-from PIL import Image
-from HTMLParser import HTMLParser
-import sqlite3 as lite
-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(
-            template_path=os.path.join(os.path.dirname(__file__), "templates"),
-            static_path=os.path.join(os.path.dirname(__file__), "static"),
-            debug=True,
-        )
-        tornado.web.Application.__init__(self, handlers, **settings)
-
-
-class MainHandler(tornado.web.RequestHandler):
-    
-    #def __init__(self):
-    #    con = lite.connect('fossil.db')
-        #with con:
-        #    cur = con.cursor() 
-
-    def get(self):
-        
-        con = lite.connect('fossil.db')
-        with con:
-            cur = con.cursor()
-            cur.execute("SELECT * FROM Fossil ORDER BY id")
-            data = cur.fetchall()
-        
-        data_list = [ [x[0], x[1]] for x in data]
-
-        current_index = 0
-        parser = MyHTMLParser()
-        print data[current_index][2]
-        parser.feed(data[current_index][2])
-        desc = parser.get_data()
-        
-        data_size = ("%.2f" % (data[current_index][3] / 1000000.0))
-        
-        self.render(
-            "index.html",
-            title = """Preservation of three-dimensional anatomy in phosphatized 
-                fossil arthropods enriches evolutionary inference""",
-            author = """Achim H Schwermann, Tomy dos Santos Rolo, Michael S Caterino, 
-                Günter Bechly, Heiko Schmied, Tilo Baumbach, Thomas van de Kamp""",
-            institute = """Steinmann Institute for Geology, Mineralogy and Paleontology, 
-                University of Bonn, Bonn, Germany; ANKA/Institute for Photon Science and 
-                Synchrotron Radiation, Karlsruhe Institute of Technology, Eggenstein-Leopoldshafen, 
-                Germany; Department of Agricultural and Environmental Sciences, Clemson University, 
-                Clemson, United States; State Museum of Natural History Stuttgart, Stuttgart, Germany; 
-                Institute of Crop Science and Resource Conservation, University of Bonn, Bonn, Germany; 
-                Laboratory for Applications of Synchrotron Radiation, Karlsruhe Institute of Technology, 
-                Karlsruhe, Germany""",
-            data_list = data_list,
-            desc = desc,
-            data_size = data_size
-        )
-
-
-class GenFileStreamerHandler(tornado.web.RequestHandler):
-    CHUNK_SIZE = 512000
-    DATA_PATH = "/home/ntj/fossils_for_nicholas"
-    
-    @tornado.web.asynchronous
-    @tornado.gen.engine
-    def get(self):
-        cdid = self.get_arguments("did")
-        con = lite.connect('fossil.db')
-        with con:
-            cur = con.cursor()
-            cur.execute("SELECT name FROM Fossil WHERE name = ?", (cdid))
-            data_key = cur.fetchall()
-            
-        if len(data_key) > 0:
-            file_name = str(cdid[0]) + ".zip"
-            self.path = self.DATA_PATH + str(cdid[0]) + "/" + file_name
-            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)
-        while data:
-            self.write(data)
-            yield tornado.gen.Task(self.flush)
-            data = fd.read(self.CHUNK_SIZE)
-        fd.close()
-        self.finish()
-
-
-if __name__ == "__main__":
-    tornado.options.parse_command_line()
-    http_server = tornado.httpserver.HTTPServer(Application())
-    http_server.listen(options.port)
-    tornado.ioloop.IOLoop.instance().start()

+ 35 - 6
main.py

@@ -139,11 +139,15 @@ class DATA:
 class MainHandler(tornado.web.RequestHandler):
 
     def get(self):
-        data = DATA()
+        index = self.get_argument("index", None)
+        print index
+        data = DATA(index)
         
         self.render(
             "index.html",
             slug = data.slug(),
+            index = data.get_index(),
+            name = data.get_name(),
             title = data.title(),
             authors = data.get_author(),
             institutes = data.get_institute(),
@@ -154,6 +158,7 @@ class MainHandler(tornado.web.RequestHandler):
 
 
 class GenFileStreamerHandler(tornado.web.RequestHandler):
+    
     CHUNK_SIZE = 512000
     DATA_PATH = "/home/ntj/fossils_for_nicholas"
     
@@ -189,15 +194,39 @@ class GenFileStreamerHandler(tornado.web.RequestHandler):
 
 class ImageHandler(tornado.web.RequestHandler):
     CHUNK_SIZE = 512000
+    #DATA_PATH = "/srv/www/katrin/static/fossils_for_nicholas"
+    DATA_PATH = "/home/ntj/fossils_for_nicholas"
 
     @tornado.web.asynchronous
     @tornado.gen.engine
     def get(self):
-        print self.get_arguments("id")
-        print self.get_arguments("name")
-        print self.get_arguments("type")
-        return
-        self.path = PATH + '/static/data/1.png'
+        did = self.get_argument("id")
+        dname = self.get_argument("name")
+        dtype = self.get_argument("type")
+        dcounter = self.get_argument("counter").zfill(4)
+        
+        if dtype == "left" or dtype == "left-resized":
+            prefix = "left"
+        elif dtype == "top" or dtype == "top-resized":
+            prefix = "top"
+        elif dtype == "front" or dtype == "front-resized":
+            prefix = "02_quercy_10_merged"
+        
+        if dtype == "preview":
+            self.path = os.path.join(
+                self.DATA_PATH,
+                dname,
+                str(dname)+".png")
+        else:
+            self.path = os.path.join(
+                self.DATA_PATH,
+                dname,
+                dtype,
+                prefix+str(dcounter)+".png")
+
+        print did, dname, dtype, dcounter
+        print self.path
+
         self.set_header('Content-Type', 'image/png')
         self.set_header("Content-Length",
                         os.path.getsize(self.path))

+ 9 - 5
templates/index.html

@@ -61,7 +61,11 @@
         <span>Sample:</span> 
         <select style="width: auto;">
             {% for sample in data_list %}
-                <option value={{ sample[0] }}>{{ sample[1] }}</option>
+            {% if sample[1]  == name %}
+            <option value={{ sample[0] }} selected>{{ sample[1] }}</option> 
+            {% else %}
+            <option value={{ sample[0] }}>{{ sample[1] }}</option> 
+            {% end %}
             {% end %}
         </select>
         <img class="icon" id="download" src="{{ static_url("img/icon/download.svg") }}"/>
@@ -76,7 +80,7 @@
                 <img class="icon" src="{{ static_url("img/icon/right-arrow.svg") }}"/>
             </div>
 	        <div class="photowrapper">
-                <img src="/img"/>
+                <img src="img?id={{ index }}&name={{ name }}&type=left-resized&counter=0"/>
             </div>
 	        <div id="slider-left"></div>
         </div>
@@ -88,7 +92,7 @@
                 <img class="icon" src="{{ static_url("img/icon/right-arrow.svg") }}"/>
             </div>
 	        <div class="photowrapper">
-                <img src="{{ static_url("data/1.png") }}"/>
+                <img src="img?id={{ index }}&name={{ name }}&type=top-resized&counter=0"/>
             </div>
 	        <div id="slider-right"></div>
         </div>
@@ -100,7 +104,7 @@
                 <img class="icon" src="{{ static_url("img/icon/right-arrow.svg") }}"/>
             </div>
 	        <div class="photowrapper">
-                <img src="{{ static_url("data/1.png") }}"/>
+                <img src="img?id={{ index }}&name={{ name }}&type=front-resized&counter=0"/>
             </div>
 	        <div id="slider-center"></div>
         </div>
@@ -121,7 +125,7 @@
         </div>
         <div id="details">
             <p><span>Overview</span></p>
-            <img src="{{ static_url("data/NRM-PZ_Ar65716.jpg") }}"/>
+            <img src="img?id={{ index }}&name={{ name }}&type=preview&counter=0"/>
         </div>
     </div>