~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-08-04 23:46:37 UTC
  • Revision ID: argentina@gmail.com-20080804234637-a26xh9ea6ge86o4k
Why do we still have turbogears laying around?

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import mimetypes
22
22
import time
23
23
 
24
 
import turbogears
25
 
from cherrypy import HTTPRedirect, response
26
 
 
 
24
from paste import httpexceptions
 
25
from paste.request import path_info_pop
27
26
 
28
27
log = logging.getLogger("loggerhead.controllers")
29
28
 
30
29
 
31
30
class DownloadUI (object):
32
31
 
33
 
    def __init__(self, branch):
34
 
        # BranchView object
 
32
    def __init__(self, branch, history):
35
33
        self._branch = branch
 
34
        self._history = history
36
35
        self.log = branch.log
37
36
 
38
 
    @turbogears.expose()
39
 
    def default(self, *args, **kw):
 
37
    def __call__(self, environ, start_response):
40
38
        # /download/<rev_id>/<file_id>/[filename]
41
39
        z = time.time()
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()
 
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
        if len(args) < 2:
 
51
            raise httpexceptions.HTTPMovedPermanently(self._branch.url('../changes'))
 
52
 
 
53
        revid = h.fix_revid(args[0])
 
54
        file_id = args[1]
 
55
        path, filename, content = h.get_file(file_id, revid)
 
56
        mime_type, encoding = mimetypes.guess_type(filename)
 
57
        if mime_type is None:
 
58
            mime_type = 'application/octet-stream'
 
59
 
 
60
        self.log.info('/download %s @ %s (%d bytes)', path, h.get_revno(revid), len(content))
 
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]