~azzar1/unity/add-show-desktop-key

1099.1.59 by William Grant
Provide a media file framework in ivle.webapp.media.
1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2009 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
# Author: William Grant
19
20
'''Media file support for the framework.'''
21
22
import os
1092.1.23 by William Grant
Add media file versioning. If a media version is specified in the
23
import time
1099.1.59 by William Grant
Provide a media file framework in ivle.webapp.media.
24
import inspect
25
import mimetypes
1092.1.23 by William Grant
Add media file versioning. If a media version is specified in the
26
import email.utils
1099.1.59 by William Grant
Provide a media file framework in ivle.webapp.media.
27
28
from ivle.webapp.base.views import BaseView
1099.1.205 by William Grant
Expose media on the public site.
29
from ivle.webapp.base.plugins import PublicViewPlugin, ViewPlugin, MediaPlugin
1099.1.85 by William Grant
Fix some missing error imports in the webapp.
30
from ivle.webapp.errors import NotFound, Forbidden
1099.1.59 by William Grant
Provide a media file framework in ivle.webapp.media.
31
32
def media_url(req, plugin, path):
1099.1.74 by Nick Chadwick
Added overlay system and console overlay. Note that the console overlay
33
    '''Generates a URL to a media file.
1092.1.23 by William Grant
Add media file versioning. If a media version is specified in the
34
1099.1.74 by Nick Chadwick
Added overlay system and console overlay. Note that the console overlay
35
    Plugin can be a string, in which case it is put into the path literally,
1092.1.23 by William Grant
Add media file versioning. If a media version is specified in the
36
    or a plugin object, in which case its name is looked up.
37
38
    If a version is specified in the IVLE configuration, a versioned URL will
39
    be generated.
40
    '''
1099.1.74 by Nick Chadwick
Added overlay system and console overlay. Note that the console overlay
41
    if not isinstance(plugin, basestring):
1092.1.59 by William Grant
Move the plugin loading/indexing logic into ivle.config.Config.
42
        plugin = req.config.reverse_plugins[plugin]
1099.1.99 by William Grant
Require that plugins providing media subclass MediaPlugin.
43
1221 by William Grant
Get all configuration information from req in ivle.webapp.media.
44
    media_path = os.path.join('+media', '+' + req.config['media']['version']) \
45
                    if req.config['media']['version'] else '+media'
46
47
    return req.make_path(os.path.join(media_path, plugin, path))
1099.1.59 by William Grant
Provide a media file framework in ivle.webapp.media.
48
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
49
class BaseMediaFileView(BaseView):
1099.1.59 by William Grant
Provide a media file framework in ivle.webapp.media.
50
    '''A view for media files.
51
52
    This serves static files from directories registered by plugins.
53
54
    Plugins wishing to export media should declare a 'media' attribute,
55
    pointing to the directory to serve (relative to the module's directory).
56
    The contents of that directory will then be available under
57
    /+media/python.path.to.module.
58
    '''
59
    def __init__(self, req, ns, path):
60
        self.ns = ns
61
        self.path = path
62
63
    def _make_filename(self, req):
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
64
        raise NotImplementedError()
1099.1.59 by William Grant
Provide a media file framework in ivle.webapp.media.
65
66
    def render(self, req):
67
        # If it begins with ".." or separator, it's illegal. Die.
68
        if self.path.startswith("..") or self.path.startswith('/'):
69
            raise Forbidden()
70
71
        filename = self._make_filename(req)
72
73
        # Find an appropriate MIME type.
74
        (type, _) = mimetypes.guess_type(filename)
75
        if type is None:
76
            type = 'application/octet-stream'
77
78
        # Get out if it is unreadable or a directory.
79
        if not os.access(filename, os.F_OK):
1099.1.62 by William Grant
Remove a bit of debugging cruft from ivle.webapp.media.
80
            raise NotFound()
1099.1.59 by William Grant
Provide a media file framework in ivle.webapp.media.
81
        if not os.access(filename, os.R_OK) or os.path.isdir(filename):
82
            raise Forbidden()
83
84
        req.content_type = type
85
        req.sendfile(filename)
86
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
87
88
class MediaFileView(BaseMediaFileView):
89
    '''A view for media files.
90
91
    This serves static files from directories registered by plugins.
92
93
    Plugins wishing to export media should declare a 'media' attribute,
94
    pointing to the directory to serve (relative to the module's directory).
95
    The contents of that directory will then be available under
96
    /+media/python.path.to.module.
97
    '''
98
    permission = None
99
100
    def _make_filename(self, req):
101
        try:
1092.1.59 by William Grant
Move the plugin loading/indexing logic into ivle.config.Config.
102
            plugin = req.config.plugins[self.ns]
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
103
        except KeyError:
104
            raise NotFound()
105
106
        if not issubclass(plugin, MediaPlugin):
107
            raise NotFound()
108
109
        mediadir = plugin.media
110
        plugindir = os.path.dirname(inspect.getmodule(plugin).__file__)
111
112
        return os.path.join(plugindir, mediadir, self.path)
113
114
    def get_permissions(self, user):
115
        return set()
116
1092.1.23 by William Grant
Add media file versioning. If a media version is specified in the
117
class VersionedMediaFileView(MediaFileView):
118
    '''A view for versioned media files, with aggressive caching.
119
120
    This serves static media files with a version string, and requests that
121
    browsers cache them for a long time.
122
    '''
123
124
    def __init__(self, req, ns, path, version):
125
        super(VersionedMediaFileView, self).__init__(req, ns, path)
126
        self.version = version
127
128
    def _make_filename(self, req):
1221 by William Grant
Get all configuration information from req in ivle.webapp.media.
129
        if self.version != req.config['media']['version']:
1092.1.23 by William Grant
Add media file versioning. If a media version is specified in the
130
            raise NotFound()
131
132
        # Don't expire for a year.
133
        req.headers_out['Expires'] = email.utils.formatdate(
134
                                    timeval=time.time() + (60*60*24*365),
135
                                    localtime=False,
136
                                    usegmt=True)
137
        return super(VersionedMediaFileView, self)._make_filename(req)
138
1158 by William Grant
Expose jQuery in our URL space.
139
140
# This maps a media namespace to an external dependency directory (in this
141
# case specified by the configuration option media/externals/jquery) and a
142
# list of permitted subpaths.
143
EXTERNAL_MEDIA_MAP = {'jquery': ('jquery', ['jquery.js'])}
144
145
class ExternalMediaFileView(BaseMediaFileView):
146
    '''A view for media files from external dependencies.
147
148
    This serves specific static files from external dependencies as defined in
149
    the IVLE configuration.
150
    '''
151
    permission = None
152
153
    def _make_filename(self, req):
154
        try:
155
            extern = EXTERNAL_MEDIA_MAP[self.ns]
156
        except KeyError:
157
            raise NotFound()
158
159
        # Unless it's a whitelisted path, we don't want to hear about it.
160
        if self.path not in extern[1]:
161
            raise NotFound()
162
163
        # Grab the admin-configured path for this particular external dep.
1221 by William Grant
Get all configuration information from req in ivle.webapp.media.
164
        externdir = req.config['media']['externals'][extern[0]]
1158 by William Grant
Expose jQuery in our URL space.
165
166
        assert isinstance(externdir, basestring)
167
168
        return os.path.join(externdir, self.path)
169
170
    def get_permissions(self, user):
171
        return set()
172
173
class ExternalVersionedMediaFileView(ExternalMediaFileView):
174
    '''A view for versioned media files from external dependencies, with
175
    aggressive caching.
176
177
    This serves specific static media files from external dependencies with a
178
    version string, and requests that browsers cache them for a long time.
179
    '''
180
181
    def __init__(self, req, ns, path, version):
182
        super(ExternalVersionedMediaFileView, self).__init__(req, ns, path)
183
        self.version = version
184
185
    def _make_filename(self, req):
1221 by William Grant
Get all configuration information from req in ivle.webapp.media.
186
        if self.version != req.config['media']['version']:
1158 by William Grant
Expose jQuery in our URL space.
187
            raise NotFound()
188
189
        # Don't expire for a year.
190
        req.headers_out['Expires'] = email.utils.formatdate(
191
                                    timeval=time.time() + (60*60*24*365),
192
                                    localtime=False,
193
                                    usegmt=True)
194
        return super(ExternalVersionedMediaFileView, self)._make_filename(req)
195
196
1099.1.205 by William Grant
Expose media on the public site.
197
class Plugin(ViewPlugin, PublicViewPlugin):
1099.1.59 by William Grant
Provide a media file framework in ivle.webapp.media.
198
    urls = [
1160 by William Grant
Swap the version and +external in media URLs, and use media_url for jQuery.
199
        ('+media/+:version/+external/:ns/*path', ExternalVersionedMediaFileView),
1158 by William Grant
Expose jQuery in our URL space.
200
        ('+media/+external/:ns/*path', ExternalMediaFileView),
1092.1.23 by William Grant
Add media file versioning. If a media version is specified in the
201
        ('+media/+:version/:ns/*path', VersionedMediaFileView),
1099.1.59 by William Grant
Provide a media file framework in ivle.webapp.media.
202
        ('+media/:ns/*path', MediaFileView),
203
    ]
1099.1.205 by William Grant
Expose media on the public site.
204
205
    public_urls = urls