~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/download_ui.py

  • Committer: Michael Hudson
  • Date: 2008-06-13 05:33:39 UTC
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: michael.hudson@canonical.com-20080613053339-w1in3qyd8gq3e8zg
more progress

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
24
import turbogears
29
 
from cherrypy import HTTPRedirect, response, session
30
 
 
31
 
from loggerhead import util
 
25
from cherrypy import HTTPRedirect, response
32
26
 
33
27
 
34
28
log = logging.getLogger("loggerhead.controllers")
47
41
        z = time.time()
48
42
        h = self._branch.get_history()
49
43
        
50
 
        if len(args) < 2:
51
 
            raise HTTPRedirect(self._branch.url('/changes'))
52
 
        
53
 
        revid = h.fix_revid(args[0])
54
 
        file_id = args[1]
55
 
        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
 
        response.headers['Content-Type'] = mime_type
61
 
        response.headers['Content-Length'] = len(content)
62
 
        response.body = content
63
 
        return response.body
 
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()