pngtile/wsgi.py
changeset 42 a5bca7b0cd8a
parent 40 5454d2e2f633
child 44 1a93b5a6efd0
equal deleted inserted replaced
41:941090e3d094 42:a5bca7b0cd8a
     2     Our WSGI web interface, which can serve the JS UI and any .png tiles via HTTP.
     2     Our WSGI web interface, which can serve the JS UI and any .png tiles via HTTP.
     3 """
     3 """
     4 
     4 
     5 from werkzeug import Request, Response, responder
     5 from werkzeug import Request, Response, responder
     6 from werkzeug import exceptions
     6 from werkzeug import exceptions
     7 import os.path
     7 import os.path, os
     8 
     8 
     9 import pypngtile as pt
     9 import pypngtile as pt
    10 
    10 
    11 DATA_ROOT = os.path.abspath('data')
    11 DATA_ROOT = os.environ.get("PNGTILE_DATA_PATH") or os.path.abspath('data/')
    12 
    12 
    13 IMAGE_CACHE = {}
    13 IMAGE_CACHE = {}
    14 
    14 
    15 TILE_WIDTH = 256
    15 TILE_WIDTH = 256
    16 TILE_HEIGHT = 256
    16 TILE_HEIGHT = 256
    17 
    17 
    18 def dir_view (req, name, path) :
    18 def dir_view (req, name, path) :
    19     prefix = os.path.dirname(req.script_root).rstrip('/')
    19     prefix = os.path.dirname(req.script_root).rstrip('/')
       
    20     script_prefix = req.script_root
    20     name = name.rstrip('/')
    21     name = name.rstrip('/')
    21 
    22 
    22 
    23 
    23     return """\
    24     return """\
    24 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    25 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    37         prefix          = prefix,
    38         prefix          = prefix,
    38         dir             = name,
    39         dir             = name,
    39         
    40         
    40         listing         = "\n".join(
    41         listing         = "\n".join(
    41             """<li><a href="%(url)s">%(name)s</a></li>""" % dict(
    42             """<li><a href="%(url)s">%(name)s</a></li>""" % dict(
    42                 url         = '/'.join((prefix, name, item)),
    43                 url         = '/'.join((script_prefix, name, item)),
    43                 name        = item,
    44                 name        = item,
    44             ) for item in ['..'] + os.listdir(path)
    45             ) for item in ['..'] + [i for i in os.listdir(path) if i.endswith('.png') or os.path.isdir(os.path.join(path, i))]
    45         ),
    46         ),
    46     )
    47     )
    47 
    48 
    48 def image_view (req, image_path, image) :
    49 def image_view (req, image_path, image) :
    49     image_name = os.path.basename(image_path)
    50     image_name = os.path.basename(image_path)
   116     # path to image
   117     # path to image
   117     image_name = req.path.lstrip('/')
   118     image_name = req.path.lstrip('/')
   118     
   119     
   119     # build absolute path
   120     # build absolute path
   120     image_path = os.path.abspath(os.path.join(DATA_ROOT, image_name))
   121     image_path = os.path.abspath(os.path.join(DATA_ROOT, image_name))
       
   122 
   121     
   123     
   122     # ensure the path points inside the data root
   124     # ensure the path points inside the data root
   123     if not image_path.startswith(DATA_ROOT) :
   125     if not image_path.startswith(DATA_ROOT) :
   124         raise exceptions.NotFound(image_name)
   126         raise exceptions.NotFound(image_name)
   125 
   127