~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:
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
#
19
19
 
20
 
import datetime
21
20
import logging
22
21
import mimetypes
23
 
import os
24
 
import posixpath
25
 
import textwrap
26
 
import time
27
 
 
28
 
import turbogears
29
 
from cherrypy import HTTPRedirect, response, session
30
 
 
31
 
from loggerhead import util
32
 
 
 
22
import urllib
 
23
 
 
24
from paste import httpexceptions
 
25
from paste.request import path_info_pop
 
26
 
 
27
from loggerhead.controllers import TemplatedBranchView
33
28
 
34
29
log = logging.getLogger("loggerhead.controllers")
35
30
 
36
31
 
37
 
class DownloadUI (object):
38
 
 
39
 
    def __init__(self, branch):
40
 
        # BranchView object
41
 
        self._branch = branch
42
 
        self.log = branch.log
43
 
 
44
 
    @turbogears.expose()
45
 
    def default(self, *args, **kw):
 
32
class DownloadUI (TemplatedBranchView):
 
33
 
 
34
    def __call__(self, environ, start_response):
46
35
        # /download/<rev_id>/<file_id>/[filename]
47
 
        z = time.time()
48
 
        h = self._branch.get_history()
49
 
        
 
36
 
 
37
        h = self._history
 
38
 
 
39
        args = []
 
40
        while True:
 
41
            arg = path_info_pop(environ)
 
42
            if arg is None:
 
43
                break
 
44
            args.append(arg)
 
45
 
50
46
        if len(args) < 2:
51
 
            raise HTTPRedirect(self._branch.url('/changes'))
52
 
        
 
47
            raise httpexceptions.HTTPMovedPermanently(
 
48
                self._branch.absolute_url('/changes'))
 
49
 
53
50
        revid = h.fix_revid(args[0])
54
51
        file_id = args[1]
55
52
        path, filename, content = h.get_file(file_id, revid)
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))
61
 
        response.headers['Content-Type'] = mime_type
62
 
        response.headers['Content-Length'] = len(content)
63
 
        response.headers['Content-Disposition'] = 'attachment; filename=%s'%(filename,)
64
 
        response.body = content
65
 
        return response.body
 
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'))
 
62
        headers = [
 
63
            ('Content-Type', mime_type),
 
64
            ('Content-Length', str(len(content))),
 
65
            ('Content-Disposition',
 
66
             "attachment; filename*=utf-8''%s" % (encoded_filename,)),
 
67
            ]
 
68
        start_response('200 OK', headers)
 
69
        return [content]