PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

/kai/controllers/download.py

https://bitbucket.org/bbangert/kai/
Python | 46 lines | 37 code | 8 blank | 1 comment | 12 complexity | 3fd2fda3f877b5f52df822b4d3813d6a MD5 | raw file
 1import logging
 2import os
 3
 4from paste.urlparser import StaticURLParser
 5from pylons import app_globals, config, request, response, session, tmpl_context as c
 6from pylons.controllers.util import abort, redirect
 7
 8from kai.lib.base import BaseController, render
 9
10log = logging.getLogger(__name__)
11
12class DownloadController(BaseController):
13    def index(self, version, file=None):
14        if version not in app_globals.versions:
15            abort(404)
16        
17        dldir = os.path.join(config['download_dir'], str(version))
18        if not os.path.exists(dldir):
19            abort(404)
20        
21        if file:
22            fdir = os.path.join(dldir, file)
23            if not os.path.exists(fdir):
24                abort(404)
25            dl_srv = StaticURLParser(dldir)
26            request.environ['PATH_INFO'] = '/%s' % (file)
27            return dl_srv(request.environ, self.start_response)
28                
29        
30        files = os.listdir(dldir)
31        files.sort()
32        filelist = []
33        use_cachefly = version == app_globals.current_version
34        # use_cachefly = False
35        for f in files:
36            if f.startswith('.'):
37                continue
38            if use_cachefly:
39                filelist.append('<li><a href="http://cdn.pylonshq.com/download/%s/%s">'
40                                '%s</a></li>\n' % (version, f, f))
41            else:
42                filelist.append('<li><a href="/download/%s/%s">%s</a></li>\n' 
43                                % (version, f, f))
44        filelist = '<ul>\n'+''.join(filelist)+'</ul>\n'
45        c.files = filelist
46        return render('/download/index.mako')