~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/download_ui.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2008-03-06 04:54:39 UTC
  • mfrom: (148.1.3 changelog-order-aaaargh)
  • Revision ID: launchpad@pqm.canonical.com-20080306045439-qe3j5ryuhb7bmqr8
[r=jamesh] fix the order of the changelog view when the revision cache is not used

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import logging
21
21
import mimetypes
22
22
import time
23
 
import urllib
24
 
 
25
 
from paste import httpexceptions
26
 
from paste.request import path_info_pop
27
 
 
28
 
from loggerhead.controllers import TemplatedBranchView
 
23
 
 
24
import turbogears
 
25
from cherrypy import HTTPRedirect, response
 
26
 
29
27
 
30
28
log = logging.getLogger("loggerhead.controllers")
31
29
 
32
30
 
33
 
class DownloadUI (TemplatedBranchView):
34
 
 
35
 
    def __call__(self, environ, start_response):
 
31
class DownloadUI (object):
 
32
 
 
33
    def __init__(self, branch):
 
34
        # BranchView object
 
35
        self._branch = branch
 
36
        self.log = branch.log
 
37
 
 
38
    @turbogears.expose()
 
39
    def default(self, *args, **kw):
36
40
        # /download/<rev_id>/<file_id>/[filename]
37
41
        z = time.time()
38
 
 
39
 
        h = self._history
40
 
 
41
 
        args = []
42
 
        while 1:
43
 
            arg = path_info_pop(environ)
44
 
            if arg is None:
45
 
                break
46
 
            args.append(arg)
47
 
 
48
 
        if len(args) < 2:
49
 
            raise httpexceptions.HTTPMovedPermanently(self._branch.url(
50
 
                      '../changes'))
51
 
 
52
 
        revid = h.fix_revid(args[0])
53
 
        file_id = args[1]
54
 
        path, filename, content = h.get_file(file_id, revid)
55
 
        mime_type, encoding = mimetypes.guess_type(filename)
56
 
        if mime_type is None:
57
 
            mime_type = 'application/octet-stream'
58
 
 
59
 
        self.log.info('/download %s @ %s (%d bytes)',
60
 
                      path,
61
 
                      h.get_revno(revid),
62
 
                      len(content))
63
 
        encoded_filename = urllib.quote(filename.encode('utf-8'))
64
 
        headers = [
65
 
            ('Content-Type', mime_type),
66
 
            ('Content-Length', len(content)),
67
 
            ('Content-Disposition',
68
 
             'attachment; filename*=utf-8\'\'%s' % encoded_filename),
69
 
            ]
70
 
        start_response('200 OK', headers)
71
 
        return [content]
 
42
        h = self._branch.get_history()
 
43
        
 
44
        h._branch.lock_read()
 
45
        try:
 
46
            if len(args) < 2:
 
47
                raise HTTPRedirect(self._branch.url('/changes'))
 
48
 
 
49
            revid = h.fix_revid(args[0])
 
50
            file_id = args[1]
 
51
            path, filename, content = h.get_file(file_id, revid)
 
52
            mime_type, encoding = mimetypes.guess_type(filename)
 
53
            if mime_type is None:
 
54
                mime_type = 'application/octet-stream'
 
55
 
 
56
            self.log.info('/download %s @ %s (%d bytes)', path, h.get_revno(revid), len(content))
 
57
            response.headers['Content-Type'] = mime_type
 
58
            response.headers['Content-Length'] = len(content)
 
59
            response.headers['Content-Disposition'] = 'attachment; filename=%s'%(filename,)
 
60
            response.body = content
 
61
            return response.body
 
62
        finally:
 
63
            h._branch.unlock()