/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/controllers/download_ui.py

  • Committer: Jelmer Vernooij
  • Date: 2018-10-20 17:34:46 UTC
  • mto: (491.6.1 breezy)
  • mto: This revision was merged to the branch mainline in revision 494.
  • Revision ID: jelmer@jelmer.uk-20181020173446-eywejowpq25sg8pw
Rename serve-branches to loggerhead-serve.

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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335  USA
 
18
#
 
19
 
 
20
import logging
 
21
import mimetypes
 
22
import urllib
 
23
 
 
24
from breezy.errors import (
 
25
    NoSuchId,
 
26
    NoSuchRevision,
 
27
    )
 
28
from breezy import osutils, urlutils
 
29
from paste import httpexceptions
 
30
from paste.request import path_info_pop
 
31
 
 
32
from ..controllers import TemplatedBranchView
 
33
 
 
34
log = logging.getLogger("loggerhead.controllers")
 
35
 
 
36
 
 
37
class DownloadUI (TemplatedBranchView):
 
38
 
 
39
    def encode_filename(self, filename):
 
40
 
 
41
        return urlutils.escape(filename)
 
42
 
 
43
    def get_args(self, environ):
 
44
        args = []
 
45
        while True:
 
46
            arg = path_info_pop(environ)
 
47
            if arg is None:
 
48
                break
 
49
            args.append(arg)
 
50
        return args
 
51
 
 
52
    def __call__(self, environ, start_response):
 
53
        # /download/<rev_id>/<file_id>/[filename]
 
54
        h = self._history
 
55
        args = self.get_args(environ)
 
56
        if len(args) < 2:
 
57
            raise httpexceptions.HTTPMovedPermanently(
 
58
                self._branch.absolute_url('/changes'))
 
59
        revid = h.fix_revid(args[0])
 
60
        file_id = urlutils.unquote_to_bytes(osutils.safe_utf8(args[1]))
 
61
        try:
 
62
            path, filename, content = h.get_file(file_id, revid)
 
63
        except (NoSuchId, NoSuchRevision):
 
64
            raise httpexceptions.HTTPNotFound()
 
65
        mime_type, encoding = mimetypes.guess_type(filename)
 
66
        if mime_type is None:
 
67
            mime_type = 'application/octet-stream'
 
68
        self.log.info('/download %s @ %s (%d bytes)',
 
69
                      path,
 
70
                      h.get_revno(revid),
 
71
                      len(content))
 
72
        encoded_filename = self.encode_filename(filename)
 
73
        headers = [
 
74
            ('Content-Type', mime_type),
 
75
            ('Content-Length', str(len(content))),
 
76
            ('Content-Disposition',
 
77
             "attachment; filename*=utf-8''%s" % (encoded_filename,)),
 
78
            ]
 
79
        start_response('200 OK', headers)
 
80
        return [content]
 
81
 
 
82
 
 
83
class DownloadTarballUI(DownloadUI):
 
84
 
 
85
    def __call__(self, environ, start_response):
 
86
        """Stream a tarball from a bazaar branch."""
 
87
        # Tried to re-use code from downloadui, not very successful
 
88
        if not self._branch.export_tarballs:
 
89
            raise httpexceptions.HTTPForbidden(
 
90
                "Tarball downloads are not allowed")
 
91
        archive_format = "tgz"
 
92
        history = self._history
 
93
        self.args = self.get_args(environ)
 
94
        if len(self.args):
 
95
            revid = history.fix_revid(self.args[0])
 
96
            version_part = '-r' + self.args[0]
 
97
        else:
 
98
            revid = self.get_revid()
 
99
            version_part = ''
 
100
        # XXX: Perhaps some better suggestion based on the URL or path?
 
101
        #
 
102
        # TODO: Perhaps set the tarball suggested mtime to the revision
 
103
        # mtime.
 
104
        root = self._branch.friendly_name or 'branch'
 
105
        filename = root + version_part + '.' + archive_format
 
106
        encoded_filename = self.encode_filename(filename)
 
107
        headers = [
 
108
            ('Content-Type', 'application/octet-stream'),
 
109
            ('Content-Disposition',
 
110
                "attachment; filename*=utf-8''%s" % (encoded_filename,)),
 
111
            ]
 
112
        start_response('200 OK', headers)
 
113
        tree = history._branch.repository.revision_tree(revid)
 
114
        return tree.archive(root=root, format=archive_format, name=filename)