~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/download_ui.py

  • Committer: Jasper St. Pierre
  • Date: 2011-04-22 18:18:02 UTC
  • mto: This revision was merged to the branch mainline in revision 448.
  • Revision ID: jstpierre@mecheye.net-20110422181802-7y2yambz9iscfa17
Make breadcrumbs consistent

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
#
19
19
 
20
 
import datetime
21
20
import logging
22
21
import mimetypes
23
 
import os
24
 
import posixpath
25
 
import textwrap
26
 
import time
27
 
 
28
 
import turbogears
29
 
from cherrypy import HTTPRedirect, response, session
30
 
 
31
 
from loggerhead import util
32
 
 
 
22
import urllib
 
23
 
 
24
from paste import httpexceptions
 
25
from paste.request import path_info_pop
 
26
 
 
27
from loggerhead.controllers import TemplatedBranchView
33
28
 
34
29
log = logging.getLogger("loggerhead.controllers")
35
30
 
36
31
 
37
 
class DownloadUI (object):
38
 
 
39
 
    def __init__(self, branch):
40
 
        # BranchView object
41
 
        self._branch = branch
42
 
        self.log = branch.log
43
 
 
44
 
    @turbogears.expose()
45
 
    def default(self, *args, **kw):
 
32
class DownloadUI (TemplatedBranchView):
 
33
 
 
34
    def __call__(self, environ, start_response):
46
35
        # /download/<rev_id>/<file_id>/[filename]
47
 
        z = time.time()
48
 
        h = self._branch.get_history()
49
 
        
 
36
 
 
37
        h = self._history
 
38
 
 
39
        args = []
 
40
        while True:
 
41
            arg = path_info_pop(environ)
 
42
            if arg is None:
 
43
                break
 
44
            args.append(arg)
 
45
 
50
46
        if len(args) < 2:
51
 
            raise HTTPRedirect(self._branch.url('/changes'))
52
 
        
 
47
            raise httpexceptions.HTTPMovedPermanently(
 
48
                self._branch.absolute_url('/changes'))
 
49
 
53
50
        revid = h.fix_revid(args[0])
54
51
        file_id = args[1]
55
 
        filename, content = h.get_file(file_id, revid)
 
52
        path, filename, content = h.get_file(file_id, revid)
56
53
        mime_type, encoding = mimetypes.guess_type(filename)
57
54
        if mime_type is None:
58
55
            mime_type = 'application/octet-stream'
59
 
        
60
 
        response.headers['Content-Type'] = mime_type
61
 
        response.headers['Content-Length'] = len(content)
62
 
        response.body = content
63
 
        return response.body
 
56
 
 
57
        self.log.info('/download %s @ %s (%d bytes)',
 
58
                      path,
 
59
                      h.get_revno(revid),
 
60
                      len(content))
 
61
        encoded_filename = urllib.quote(filename.encode('utf-8'))
 
62
        headers = [
 
63
            ('Content-Type', mime_type),
 
64
            ('Content-Length', str(len(content))),
 
65
            ('Content-Disposition',
 
66
             "attachment; filename*=utf-8''%s" % (encoded_filename,)),
 
67
            ]
 
68
        start_response('200 OK', headers)
 
69
        return [content]