/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk
38 by Robey Pointer
another pile of semi-related changes:
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
264.1.2 by Michael Hudson
this content-disposition seems to be better
22
import urllib
38 by Robey Pointer
another pile of semi-related changes:
23
159.2.8 by Michael Hudson
no more cherrypy! in the code anyway, tests currently screwed of course
24
from paste import httpexceptions
25
from paste.request import path_info_pop
38 by Robey Pointer
another pile of semi-related changes:
26
249.1.1 by Robert Collins
Faster search logic - less double checking etc etc, seems to work.
27
from loggerhead.controllers import TemplatedBranchView
28
38 by Robey Pointer
another pile of semi-related changes:
29
log = logging.getLogger("loggerhead.controllers")
30
31
249.1.1 by Robert Collins
Faster search logic - less double checking etc etc, seems to work.
32
class DownloadUI (TemplatedBranchView):
48 by Robey Pointer
the big migration of branch-specific data to a BranchView object: actually
33
174.1.8 by Michael Hudson
even more wsgi like interface
34
    def __call__(self, environ, start_response):
38 by Robey Pointer
another pile of semi-related changes:
35
        # /download/<rev_id>/<file_id>/[filename]
179.1.12 by Michael Hudson
less repeated locking
36
37
        h = self._history
38
39
        args = []
389.2.4 by Matt Nordhoff
Replace "while 1" with "while True".
40
        while True:
179.1.12 by Michael Hudson
less repeated locking
41
            arg = path_info_pop(environ)
42
            if arg is None:
43
                break
44
            args.append(arg)
45
46
        if len(args) < 2:
230.1.1 by Steve 'Ashcrow' Milner
Updated to follow pep8.
47
            raise httpexceptions.HTTPMovedPermanently(self._branch.url(
48
                      '../changes'))
179.1.12 by Michael Hudson
less repeated locking
49
50
        revid = h.fix_revid(args[0])
51
        file_id = args[1]
52
        path, filename, content = h.get_file(file_id, revid)
53
        mime_type, encoding = mimetypes.guess_type(filename)
54
        if mime_type is None:
55
            mime_type = 'application/octet-stream'
56
230.1.1 by Steve 'Ashcrow' Milner
Updated to follow pep8.
57
        self.log.info('/download %s @ %s (%d bytes)',
58
                      path,
59
                      h.get_revno(revid),
60
                      len(content))
264.1.2 by Michael Hudson
this content-disposition seems to be better
61
        encoded_filename = urllib.quote(filename.encode('utf-8'))
179.1.12 by Michael Hudson
less repeated locking
62
        headers = [
63
            ('Content-Type', mime_type),
64
            ('Content-Length', len(content)),
264.1.2 by Michael Hudson
this content-disposition seems to be better
65
            ('Content-Disposition',
389.2.5 by Matt Nordhoff
Little more.
66
             "attachment; filename*=utf-8''%s" % (encoded_filename,)),
179.1.12 by Michael Hudson
less repeated locking
67
            ]
68
        start_response('200 OK', headers)
69
        return [content]