~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/download_ui.py

  • Committer: Ian Clatworthy
  • Date: 2010-04-22 08:52:59 UTC
  • mfrom: (405.1.8 small-cleanups)
  • Revision ID: ian.clatworthy@canonical.com-20100422085259-1nb59i53emu2ny5b
Merge John's minor cleanups

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
 
        
50
 
        h._branch.lock_read()
51
 
        try:
52
 
            if len(args) < 2:
53
 
                raise HTTPRedirect(self._branch.url('/changes'))
54
 
 
55
 
            revid = h.fix_revid(args[0])
56
 
            file_id = args[1]
57
 
            path, filename, content = h.get_file(file_id, revid)
58
 
            mime_type, encoding = mimetypes.guess_type(filename)
59
 
            if mime_type is None:
60
 
                mime_type = 'application/octet-stream'
61
 
 
62
 
            self.log.info('/download %s @ %s (%d bytes)', path, h.get_revno(revid), len(content))
63
 
            response.headers['Content-Type'] = mime_type
64
 
            response.headers['Content-Length'] = len(content)
65
 
            response.headers['Content-Disposition'] = 'attachment; filename=%s'%(filename,)
66
 
            response.body = content
67
 
            return response.body
68
 
        finally:
69
 
            h._branch.unlock()
 
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
 
 
46
        if len(args) < 2:
 
47
            raise httpexceptions.HTTPMovedPermanently(self._branch.url(
 
48
                      '../changes'))
 
49
 
 
50
        revid = h.fix_revid(args[0])
 
51
        file_id = args[1]
 
52
        path, filename, content = h.get_file(file_id, revid)
 
53
        mime_type, encoding = mimetypes.guess_type(filename)
 
54
        if mime_type is None:
 
55
            mime_type = 'application/octet-stream'
 
56
 
 
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]