From deeb5490c3481513230a5784dd14069fd09a9345 Mon Sep 17 00:00:00 2001
From: "Kevin S. Hahn" <kevinshahn@gmail.com>
Date: Fri, 27 Mar 2015 10:20:11 -0700
Subject: [PATCH] starts routes to fetch montage info and get tiles

---
 api.py        |  1 +
 containers.py | 28 ++++++++++++++++++++++++++++
 util.py       |  5 +++++
 3 files changed, 34 insertions(+)

diff --git a/api.py b/api.py
index b966c0d5..a032f63c 100755
--- a/api.py
+++ b/api.py
@@ -105,6 +105,7 @@ routes = [
         webapp2.Route(r'/<:[0-9a-f]{24}>',                          acquisitions.Acquisition, name='acquisition'),
         webapp2.Route(r'/<:[0-9a-f]{24}>/file',                     acquisitions.Acquisition, handler_method='get_file', methods=['GET', 'POST']),
         webapp2.Route(r'/<:[0-9a-f]{24}>/file',                     acquisitions.Acquisition, handler_method='put_file', methods=['PUT']),
+        webapp2.Route(r'/<:[0-9a-f]{24}>/tile',                     acquisitions.Acquisition, handler_method='get_tile', methods=['GET']),
         webapp2.Route(r'/<:[0-9a-f]{24}>/attachment',               acquisitions.Acquisition, handler_method='delete_attachment', methods=['DELETE']),
         webapp2.Route(r'/<:[0-9a-f]{24}>/attachment',               acquisitions.Acquisition, handler_method='get_attachment', methods=['GET', 'POST']),
         webapp2.Route(r'/<:[0-9a-f]{24}>/attachment',               acquisitions.Acquisition, handler_method='put_attachment', methods=['PUT']),
diff --git a/containers.py b/containers.py
index d28ea6ca..e3ee9404 100644
--- a/containers.py
+++ b/containers.py
@@ -325,6 +325,34 @@ class Container(base.RequestHandler):
                 else:
                     self.abort(400, '%s is not listed in the sha1s' % fname)
 
+    def get_tile(self, cid):
+        """fetch info about a tiled tiff, or retrieve a specific tile."""
+        _id = bson.ObjectId(cid)
+        montage_info = self.dbc.find_one(
+            {
+                '_id': _id,
+                '$and': [
+                    {'files.kinds': ['montage']},
+                    {'files.ext': '.tiff'},
+                ],
+            },
+            ['files.$'],
+        )
+        if not montage_info:
+            self.abort(404, 'montage tiff not found')
+        fn = montage_info['files'][0]['name'] + montage_info['files'][0]['ext']
+        fp = os.path.join(self.app.config['data_path'], cid[-3:], cid, fn)
+        z = self.request.get('z')
+        x = self.request.get('x')
+        y = self.request.get('y')
+        if not (z and x and y):
+            return util.get_info(fp)
+        else:
+            self.response.content_type = 'image/png'
+            tile = util.get_tile(fp, int(z), int(x), int(y))
+            if tile:
+                self.response.write(tile)
+
     def get_attachment(self, cid):
         """Download one attachment."""
         fname = self.request.get('name')
diff --git a/util.py b/util.py
index 8dbd11f2..49c59f6f 100644
--- a/util.py
+++ b/util.py
@@ -12,6 +12,11 @@ import datetime
 import tempdir as tempfile
 
 import scitran.data
+import scitran.data.medimg.montage
+
+get_info = scitran.data.medimg.montage.get_info
+get_tile = scitran.data.medimg.montage.get_tile
+
 
 PROJECTION_FIELDS = ['timestamp', 'permissions', 'public']
 
-- 
GitLab