25
24
from paste import httpexceptions
26
25
from paste.request import path_info_pop
28
27
from loggerhead.controllers import TemplatedBranchView
29
from loggerhead.exporter import export_archive
31
29
log = logging.getLogger("loggerhead.controllers")
34
32
class DownloadUI (TemplatedBranchView):
36
def encode_filename(self, filename):
38
return urllib.quote(filename.encode('utf-8'))
40
def get_args(self, environ):
34
def __call__(self, environ, start_response):
35
# /download/<rev_id>/<file_id>/[filename]
43
41
arg = path_info_pop(environ)
49
def __call__(self, environ, start_response):
50
# /download/<rev_id>/<file_id>/[filename]
52
args = self.get_args(environ)
54
raise httpexceptions.HTTPMovedPermanently(
55
self._branch.absolute_url('/changes'))
47
raise httpexceptions.HTTPMovedPermanently(self._branch.url(
56
50
revid = h.fix_revid(args[0])
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'
62
57
self.log.info('/download %s @ %s (%d bytes)',
64
59
h.get_revno(revid),
66
encoded_filename = self.encode_filename(filename)
61
encoded_filename = urllib.quote(filename.encode('utf-8'))
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),
73
68
start_response('200 OK', headers)
77
class DownloadTarballUI(DownloadUI):
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
83
history = self._history
84
self.args = self.get_args(environ)
86
revid = history.fix_revid(self.args[0])
88
revid = self.get_revid()
89
if self._branch.export_tarballs:
91
encoded_filename = self.encode_filename(root + '.' + format)
93
('Content-Type', 'application/octet-stream'),
94
('Content-Disposition',
95
"attachment; filename*=utf-8''%s" % (encoded_filename,)),
97
start_response('200 OK', headers)
98
return export_archive(history, root, revid, format)
100
raise httpexceptions.HTTPSeeOther('/')