~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/download_ui.py

  • Committer: Robey Pointer
  • Date: 2007-03-26 06:21:06 UTC
  • Revision ID: robey@lag.net-20070326062106-1fehbvag3z2o2q0f
(bug 86247)
it's possible for a commit to have no message at all.  in that case, don't
try to index into it. :)

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
20
21
import logging
21
22
import mimetypes
22
 
import urllib
23
 
 
24
 
from paste import httpexceptions
25
 
from paste.request import path_info_pop
26
 
 
27
 
from loggerhead.controllers import TemplatedBranchView
 
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
 
28
33
 
29
34
log = logging.getLogger("loggerhead.controllers")
30
35
 
31
36
 
32
 
class DownloadUI (TemplatedBranchView):
33
 
 
34
 
    def __call__(self, environ, start_response):
 
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):
35
46
        # /download/<rev_id>/<file_id>/[filename]
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
 
 
 
47
        z = time.time()
 
48
        h = self._branch.get_history()
 
49
        
46
50
        if len(args) < 2:
47
 
            raise httpexceptions.HTTPMovedPermanently(self._branch.url(
48
 
                      '../changes'))
49
 
 
 
51
            raise HTTPRedirect(self._branch.url('/changes'))
 
52
        
50
53
        revid = h.fix_revid(args[0])
51
54
        file_id = args[1]
52
55
        path, filename, content = h.get_file(file_id, revid)
54
57
        if mime_type is None:
55
58
            mime_type = 'application/octet-stream'
56
59
 
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', len(content)),
65
 
            ('Content-Disposition',
66
 
             "attachment; filename*=utf-8''%s" % (encoded_filename,)),
67
 
            ]
68
 
        start_response('200 OK', headers)
69
 
        return [content]
 
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.body = content
 
64
        return response.body