~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-06-25 09:39:58 UTC
  • mfrom: (176.1.1 new-style-classes)
  • Revision ID: argentina@gmail.com-20080625093958-j5iv9w20szi0vqxn
Switch to new-style classes (Matt Nordhoff)

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import logging
21
21
import mimetypes
22
22
import time
23
 
import urllib
24
23
 
25
24
from paste import httpexceptions
26
25
from paste.request import path_info_pop
27
26
 
28
 
from loggerhead.controllers import TemplatedBranchView
29
 
 
30
27
log = logging.getLogger("loggerhead.controllers")
31
28
 
32
29
 
33
 
class DownloadUI (TemplatedBranchView):
 
30
class DownloadUI (object):
 
31
 
 
32
    def __init__(self, branch):
 
33
        # BranchView object
 
34
        self._branch = branch
 
35
        self.log = branch.log
34
36
 
35
37
    def __call__(self, environ, start_response):
36
38
        # /download/<rev_id>/<file_id>/[filename]
37
39
        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
 
        if len(args) < 2:
49
 
            raise httpexceptions.HTTPMovedPermanently(self._branch.url(
50
 
                      '../changes'))
51
 
 
52
 
        revid = h.fix_revid(args[0])
53
 
        file_id = args[1]
54
 
        path, filename, content = h.get_file(file_id, revid)
55
 
        mime_type, encoding = mimetypes.guess_type(filename)
56
 
        if mime_type is None:
57
 
            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]
 
40
        h = self._branch.history
 
41
 
 
42
        h._branch.lock_read()
 
43
        try:
 
44
            args = []
 
45
            while 1:
 
46
                arg = path_info_pop(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
            headers = [
 
63
                ('Content-Type', mime_type),
 
64
                ('Content-Length', len(content)),
 
65
                ('Content-Disposition', 'attachment; filename=%s'%(filename,)),
 
66
                ]
 
67
            start_response('200 OK', headers)
 
68
            return [content]
 
69
        finally:
 
70
            h._branch.unlock()