~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/download_ui.py

  • Committer: Robey Pointer
  • Date: 2006-12-20 22:17:33 UTC
  • Revision ID: robey@lag.net-20061220221733-1y0ix5n2eexmltdo
update README

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
 
23
import os
 
24
import posixpath
 
25
import textwrap
22
26
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
 
27
 
 
28
import turbogears
 
29
from cherrypy import HTTPRedirect, response, session
 
30
 
 
31
from loggerhead import util
 
32
 
29
33
 
30
34
log = logging.getLogger("loggerhead.controllers")
31
35
 
32
36
 
33
 
class DownloadUI (TemplatedBranchView):
34
 
 
35
 
    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):
36
46
        # /download/<rev_id>/<file_id>/[filename]
37
47
        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
        h = self._branch.get_history()
 
49
        
48
50
        if len(args) < 2:
49
 
            raise httpexceptions.HTTPMovedPermanently(self._branch.url(
50
 
                      '../changes'))
51
 
 
 
51
            raise HTTPRedirect(self._branch.url('/changes'))
 
52
        
52
53
        revid = h.fix_revid(args[0])
53
54
        file_id = args[1]
54
 
        path, filename, content = h.get_file(file_id, revid)
 
55
        filename, content = h.get_file(file_id, revid)
55
56
        mime_type, encoding = mimetypes.guess_type(filename)
56
57
        if mime_type is None:
57
58
            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]
 
59
        
 
60
        response.headers['Content-Type'] = mime_type
 
61
        response.headers['Content-Length'] = len(content)
 
62
        response.body = content
 
63
        return response.body