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

« back to all changes in this revision

Viewing changes to ivle/webapp/media.py

Dispatch now generates an index for each plugin type, allowing plugins to
be written which are aware of other plugins, and other plugin types.

All view plugins now subclass from ivle.webapp.base.plugins.ViewPlugin,
as opposed to subclassing BasePlugin directly. This will allow us to
easily re-write console as an OverlayPlugin, and allow future new
plugins types to be created.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
23
import inspect
 
24
import mimetypes
 
25
 
 
26
import ivle.conf
 
27
from ivle.webapp.base.views import BaseView
 
28
from ivle.webapp.base.plugins import ViewPlugin
 
29
from ivle.webapp.errors import NotFound
 
30
 
 
31
def media_url(req, plugin, path):
 
32
    '''Generates a URL to a media file.'''
 
33
    return os.path.join(ivle.conf.root_dir, '+media',
 
34
                        req.reverse_plugins[plugin], path)
 
35
 
 
36
class MediaFileView(BaseView):
 
37
    '''A view for media files.
 
38
 
 
39
    This serves static files from directories registered by plugins.
 
40
 
 
41
    Plugins wishing to export media should declare a 'media' attribute,
 
42
    pointing to the directory to serve (relative to the module's directory).
 
43
    The contents of that directory will then be available under
 
44
    /+media/python.path.to.module.
 
45
    '''
 
46
    def __init__(self, req, ns, path):
 
47
        self.ns = ns
 
48
        self.path = path
 
49
 
 
50
    def _make_filename(self, req):
 
51
        try:
 
52
            plugin = req.plugins[self.ns]
 
53
        except KeyError:
 
54
            raise NotFound()
 
55
 
 
56
        try:
 
57
            mediadir = plugin.media
 
58
        except AttributeError:
 
59
            raise NotFound()
 
60
 
 
61
        plugindir = os.path.dirname(inspect.getmodule(plugin).__file__)
 
62
 
 
63
        return os.path.join(plugindir, mediadir, self.path)
 
64
 
 
65
    def render(self, req):
 
66
        # If it begins with ".." or separator, it's illegal. Die.
 
67
        if self.path.startswith("..") or self.path.startswith('/'):
 
68
            raise Forbidden()
 
69
 
 
70
        filename = self._make_filename(req)
 
71
 
 
72
        # Find an appropriate MIME type.
 
73
        (type, _) = mimetypes.guess_type(filename)
 
74
        if type is None:
 
75
            type = 'application/octet-stream'
 
76
 
 
77
        # Get out if it is unreadable or a directory.
 
78
        if not os.access(filename, os.F_OK):
 
79
            raise NotFound()
 
80
        if not os.access(filename, os.R_OK) or os.path.isdir(filename):
 
81
            raise Forbidden()
 
82
 
 
83
        req.content_type = type
 
84
        req.sendfile(filename)
 
85
 
 
86
class Plugin(ViewPlugin):
 
87
    urls = [
 
88
        ('+media/:ns/*path', MediaFileView),
 
89
    ]