~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/download_ui.py

  • Committer: Robey Pointer
  • Date: 2006-12-16 01:44:06 UTC
  • Revision ID: robey@lag.net-20061216014406-gpm7byj28rfaelfu
fix up atom

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Copyright (C) 2006  Robey Pointer <robey@lag.net>
3
 
# Copyright (C) 2006  Goffredo Baroncelli <kreijack@inwind.it>
4
 
#
5
 
# This program is free software; you can redistribute it and/or modify
6
 
# it under the terms of the GNU General Public License as published by
7
 
# the Free Software Foundation; either version 2 of the License, or
8
 
# (at your option) any later version.
9
 
#
10
 
# This program is distributed in the hope that it will be useful,
11
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
# GNU General Public License for more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License
16
 
# along with this program; if not, write to the Free Software
17
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
#
19
 
 
20
 
import logging
21
 
import mimetypes
22
 
import time
23
 
 
24
 
from paste import httpexceptions
25
 
from paste.request import path_info_pop
26
 
 
27
 
log = logging.getLogger("loggerhead.controllers")
28
 
 
29
 
 
30
 
class DownloadUI (object):
31
 
 
32
 
    def __init__(self, branch):
33
 
        # BranchView object
34
 
        self._branch = branch
35
 
        self.log = branch.log
36
 
 
37
 
    def default(self, request, response):
38
 
        # /download/<rev_id>/<file_id>/[filename]
39
 
        z = time.time()
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()