~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/download_ui.py

  • Committer: Martin Albisetti
  • Date: 2008-07-26 17:41:49 UTC
  • mfrom: (183.2.1 copyright)
  • Revision ID: argentina@gmail.com-20080726174149-o64p6v3p3301km91
 * Clarify License and add copyright to all file headers (John Arbash Meinel)

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