~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/download_ui.py

[rs=mwhudson] many many improvements to loggerhead -- faster
        templating, leaner HTTP server, less caching, cleaner urls

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
class DownloadUI (object):
31
31
 
32
 
    def __init__(self, branch, history):
 
32
    def __init__(self, branch):
 
33
        # BranchView object
33
34
        self._branch = branch
34
 
        self._history = history
35
35
        self.log = branch.log
36
36
 
37
 
    def __call__(self, environ, start_response):
 
37
    def default(self, request, response):
38
38
        # /download/<rev_id>/<file_id>/[filename]
39
39
        z = time.time()
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]
 
40
        h = self._branch.history
 
41
 
 
42
        h._branch.lock_read()
 
43
        try:
 
44
            args = []
 
45
            while 1:
 
46
                arg = path_info_pop(request.environ)
 
47
                if arg is None:
 
48
                    break
 
49
                args.append(arg)
 
50
 
 
51
            if len(args) < 2:
 
52
                raise httpexceptions.HTTPMovedPermanently(self._branch.url('../changes'))
 
53
 
 
54
            revid = h.fix_revid(args[0])
 
55
            file_id = args[1]
 
56
            path, filename, content = h.get_file(file_id, revid)
 
57
            mime_type, encoding = mimetypes.guess_type(filename)
 
58
            if mime_type is None:
 
59
                mime_type = 'application/octet-stream'
 
60
 
 
61
            self.log.info('/download %s @ %s (%d bytes)', path, h.get_revno(revid), len(content))
 
62
            response.headers['Content-Type'] = mime_type
 
63
            response.headers['Content-Length'] = len(content)
 
64
            response.headers['Content-Disposition'] = 'attachment; filename=%s'%(filename,)
 
65
            response.write(content)
 
66
        finally:
 
67
            h._branch.unlock()