~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/download_ui.py

  • Committer: John Arbash Meinel
  • Date: 2011-03-16 12:29:36 UTC
  • Revision ID: john@arbash-meinel.com-20110316122936-u1u98hqpwyv08ry9
Import sys at the start of loggerhead/__init__.py we use it as part of startup.
And sys is always loaded, no need to avoid importing it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
import logging
21
21
import mimetypes
22
 
import time
 
22
import urllib
23
23
 
24
24
from paste import httpexceptions
25
25
from paste.request import path_info_pop
26
26
 
 
27
from loggerhead.controllers import TemplatedBranchView
 
28
 
27
29
log = logging.getLogger("loggerhead.controllers")
28
30
 
29
31
 
30
 
class DownloadUI (object):
31
 
 
32
 
    def __init__(self, branch, history):
33
 
        self._branch = branch
34
 
        self._history = history
35
 
        self.log = branch.log
 
32
class DownloadUI (TemplatedBranchView):
36
33
 
37
34
    def __call__(self, environ, start_response):
38
35
        # /download/<rev_id>/<file_id>/[filename]
39
 
        z = time.time()
40
36
 
41
37
        h = self._history
42
38
 
43
39
        args = []
44
 
        while 1:
 
40
        while True:
45
41
            arg = path_info_pop(environ)
46
42
            if arg is None:
47
43
                break
48
44
            args.append(arg)
49
45
 
50
46
        if len(args) < 2:
51
 
            raise httpexceptions.HTTPMovedPermanently(self._branch.url('../changes'))
 
47
            raise httpexceptions.HTTPMovedPermanently(
 
48
                self._branch.absolute_url('/changes'))
52
49
 
53
50
        revid = h.fix_revid(args[0])
54
51
        file_id = args[1]
57
54
        if mime_type is None:
58
55
            mime_type = 'application/octet-stream'
59
56
 
60
 
        self.log.info('/download %s @ %s (%d bytes)', path, h.get_revno(revid), len(content))
 
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'))
61
62
        headers = [
62
63
            ('Content-Type', mime_type),
63
 
            ('Content-Length', len(content)),
64
 
            ('Content-Disposition', 'attachment; filename=%s'%(filename,)),
 
64
            ('Content-Length', str(len(content))),
 
65
            ('Content-Disposition',
 
66
             "attachment; filename*=utf-8''%s" % (encoded_filename,)),
65
67
            ]
66
68
        start_response('200 OK', headers)
67
69
        return [content]