~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-25 14:14:42 UTC
  • Revision ID: john@arbash-meinel.com-20110325141442-536j4be3x0c464zy
Don't access loggerhead code from under 'bzrlib.plugins'.

Only test_load_test was doing so. And in the Launchpad test suite
loggerhead is a standalone app, rather than a bzr plugin.
This allows us to run the test suite as part of the Launchpad
test suite (bug #742446)

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
40
        while True:
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
47
            raise httpexceptions.HTTPMovedPermanently(
55
48
                self._branch.absolute_url('/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
64
            ('Content-Length', str(len(content))),
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
 
        if not self._branch.export_tarballs:
83
 
            raise httpexceptions.HTTPForbidden(
84
 
                "Tarball downloads are not allowed")
85
 
        archive_format = "tgz"
86
 
        history = self._history
87
 
        self.args = self.get_args(environ)
88
 
        if len(self.args):
89
 
            revid = history.fix_revid(self.args[0])
90
 
            version_part = '-r' + self.args[0]
91
 
        else:
92
 
            revid = self.get_revid()
93
 
            version_part = ''
94
 
        # XXX: Perhaps some better suggestion based on the URL or path?
95
 
        #
96
 
        # TODO: Perhaps set the tarball suggested mtime to the revision
97
 
        # mtime.
98
 
        root = self._branch.friendly_name or 'branch'
99
 
        encoded_filename = self.encode_filename(
100
 
            root + version_part + '.' + archive_format)
101
 
        headers = [
102
 
            ('Content-Type', 'application/octet-stream'),
103
 
            ('Content-Disposition',
104
 
                "attachment; filename*=utf-8''%s" % (encoded_filename,)),
105
 
            ]
106
 
        start_response('200 OK', headers)
107
 
        return export_archive(history, root, revid, archive_format)