17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
from cherrypy import HTTPRedirect, response, session
31
from loggerhead import util
24
from paste import httpexceptions
25
from paste.request import path_info_pop
27
from loggerhead.controllers import TemplatedBranchView
34
29
log = logging.getLogger("loggerhead.controllers")
37
class DownloadUI (object):
39
def __init__(self, branch):
45
def default(self, *args, **kw):
32
class DownloadUI (TemplatedBranchView):
34
def __call__(self, environ, start_response):
46
35
# /download/<rev_id>/<file_id>/[filename]
48
h = self._branch.get_history()
41
arg = path_info_pop(environ)
51
raise HTTPRedirect(self._branch.url('/changes'))
47
raise httpexceptions.HTTPMovedPermanently(
48
self._branch.absolute_url('/changes'))
53
50
revid = h.fix_revid(args[0])
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'
60
response.headers['Content-Type'] = mime_type
61
response.headers['Content-Length'] = len(content)
62
response.body = content
57
self.log.info('/download %s @ %s (%d bytes)',
61
encoded_filename = urllib.quote(filename.encode('utf-8'))
63
('Content-Type', mime_type),
64
('Content-Length', str(len(content))),
65
('Content-Disposition',
66
"attachment; filename*=utf-8''%s" % (encoded_filename,)),
68
start_response('200 OK', headers)