~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/download_ui.py

  • Committer: Matt Nordhoff
  • Date: 2009-10-17 06:19:40 UTC
  • mto: (329.2.2 trailing-whitespace)
  • mto: This revision was merged to the branch mainline in revision 392.
  • Revision ID: mnordhoff@mattnordhoff.com-20091017061940-w9tcvy0xs1irno3y
Some random PEP 8 and otehr stylistic changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
import logging
21
21
import mimetypes
22
 
import os
23
22
import urllib
24
23
 
25
24
from paste import httpexceptions
26
25
from paste.request import path_info_pop
27
26
 
28
27
from loggerhead.controllers import TemplatedBranchView
29
 
from loggerhead.exporter import export_archive
30
28
 
31
29
log = logging.getLogger("loggerhead.controllers")
32
30
 
33
31
 
34
32
class DownloadUI (TemplatedBranchView):
35
33
 
36
 
    def encode_filename(self, filename):
37
 
 
38
 
        return urllib.quote(filename.encode('utf-8'))
39
 
 
40
 
    def get_args(self, environ):
 
34
    def __call__(self, environ, start_response):
 
35
        # /download/<rev_id>/<file_id>/[filename]
 
36
 
 
37
        h = self._history
 
38
 
41
39
        args = []
42
 
        while True:
 
40
        while 1:
43
41
            arg = path_info_pop(environ)
44
42
            if arg is None:
45
43
                break
46
44
            args.append(arg)
47
 
        return args
48
45
 
49
 
    def __call__(self, environ, start_response):
50
 
        # /download/<rev_id>/<file_id>/[filename]
51
 
        h = self._history
52
 
        args = self.get_args(environ)
53
46
        if len(args) < 2:
54
 
            raise httpexceptions.HTTPMovedPermanently(
55
 
                self._branch.absolute_url('/changes'))
 
47
            raise httpexceptions.HTTPMovedPermanently(self._branch.url(
 
48
                      '../changes'))
 
49
 
56
50
        revid = h.fix_revid(args[0])
57
51
        file_id = args[1]
58
52
        path, filename, content = h.get_file(file_id, revid)
59
53
        mime_type, encoding = mimetypes.guess_type(filename)
60
54
        if mime_type is None:
61
55
            mime_type = 'application/octet-stream'
 
56
 
62
57
        self.log.info('/download %s @ %s (%d bytes)',
63
58
                      path,
64
59
                      h.get_revno(revid),
65
60
                      len(content))
66
 
        encoded_filename = self.encode_filename(filename)
 
61
        encoded_filename = urllib.quote(filename.encode('utf-8'))
67
62
        headers = [
68
63
            ('Content-Type', mime_type),
69
 
            ('Content-Length', str(len(content))),
 
64
            ('Content-Length', len(content)),
70
65
            ('Content-Disposition',
71
 
             "attachment; filename*=utf-8''%s" % (encoded_filename,)),
 
66
             'attachment; filename*=utf-8\'\'%s' % encoded_filename),
72
67
            ]
73
68
        start_response('200 OK', headers)
74
69
        return [content]
75
 
 
76
 
 
77
 
class DownloadTarballUI(DownloadUI):
78
 
 
79
 
    def __call__(self, environ, start_response):
80
 
        """Stream a tarball from a bazaar branch."""
81
 
        # Tried to re-use code from downloadui, not very successful
82
 
        format = "tar"
83
 
        history = self._history
84
 
        self.args = self.get_args(environ)
85
 
        if len(self.args):
86
 
            revid = history.fix_revid(self.args[0])
87
 
        else:
88
 
            revid = self.get_revid()
89
 
        if self._branch.export_tarballs:
90
 
            root = 'branch'
91
 
            encoded_filename = self.encode_filename(root + '.' + format)
92
 
            headers = [
93
 
                ('Content-Type', 'application/octet-stream'),
94
 
                ('Content-Disposition',
95
 
                 "attachment; filename*=utf-8''%s" % (encoded_filename,)),
96
 
                ]
97
 
            start_response('200 OK', headers)
98
 
            return export_archive(history, root, revid, format)
99
 
        else:
100
 
            raise httpexceptions.HTTPSeeOther('/')