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

« back to all changes in this revision

Viewing changes to ivle/webapp/media.py

  • Committer: William Grant
  • Date: 2013-08-08 01:20:27 UTC
  • Revision ID: me@williamgrant.id.au-20130808012027-mb17vkfdosq3y12a
Blargh

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import mimetypes
26
26
import email.utils
27
27
 
28
 
import ivle.conf
29
 
from ivle.config import Config
30
28
from ivle.webapp.base.views import BaseView
31
29
from ivle.webapp.base.plugins import PublicViewPlugin, ViewPlugin, MediaPlugin
32
30
from ivle.webapp.errors import NotFound, Forbidden
 
31
from ivle.webapp.publisher import INF
 
32
from ivle.webapp import ApplicationRoot
 
33
 
 
34
# This maps a media namespace to an external dependency directory (in this
 
35
# case specified by the configuration option media/externals/jquery) and a
 
36
# list of permitted subpaths.
 
37
EXTERNAL_MEDIA_MAP = {'jquery': ('jquery', ['jquery.js']),
 
38
                      'codemirror': ('codemirror', None),
 
39
                      }
33
40
 
34
41
def media_url(req, plugin, path):
35
42
    '''Generates a URL to a media file.
43
50
    if not isinstance(plugin, basestring):
44
51
        plugin = req.config.reverse_plugins[plugin]
45
52
 
46
 
    config = Config()
47
 
 
48
 
    media_path = os.path.join('+media', '+' + config['media']['version']) if \
49
 
                              config['media']['version'] else '+media'
50
 
 
51
 
    return os.path.join(ivle.conf.root_dir, media_path, plugin, path)
52
 
 
53
 
class BaseMediaFileView(BaseView):
54
 
    '''A view for media files.
55
 
 
56
 
    This serves static files from directories registered by plugins.
57
 
 
58
 
    Plugins wishing to export media should declare a 'media' attribute,
59
 
    pointing to the directory to serve (relative to the module's directory).
60
 
    The contents of that directory will then be available under
61
 
    /+media/python.path.to.module.
62
 
    '''
63
 
    def __init__(self, req, ns, path):
 
53
    media_path = os.path.join('+media', '+' + req.config['media']['version']) \
 
54
                    if req.config['media']['version'] else '+media'
 
55
 
 
56
    return req.make_path(os.path.join(media_path, plugin, path))
 
57
 
 
58
class MediaFile(object):
 
59
    def __init__(self, root, external, version, ns, path):
 
60
        self.root = root
 
61
        self.external = external
 
62
        self.version = version
64
63
        self.ns = ns
65
64
        self.path = path
66
65
 
67
 
    def _make_filename(self, req):
68
 
        raise NotImplementedError()
 
66
    @property
 
67
    def filename(self):
 
68
        if self.external:
 
69
            try:
 
70
                extern = EXTERNAL_MEDIA_MAP[self.ns]
 
71
            except KeyError:
 
72
                return None
 
73
 
 
74
            # Unless it's a whitelisted path, we don't want to hear about it.
 
75
            # If the whitelist is None (not []), we allow all.
 
76
            if extern[1] is not None and self.path not in extern[1]:
 
77
                return None
 
78
 
 
79
            # Grab the admin-configured path for this particular external dep.
 
80
            externdir = self.root.config['media']['externals'][extern[0]]
 
81
 
 
82
            assert isinstance(externdir, basestring)
 
83
 
 
84
            return os.path.join(externdir, self.path)
 
85
        else:
 
86
            try:
 
87
                plugin = self.root.config.plugins[self.ns]
 
88
            except KeyError:
 
89
                return None
 
90
 
 
91
            if not issubclass(plugin, MediaPlugin):
 
92
                return None
 
93
 
 
94
            mediadir = plugin.media
 
95
            plugindir = os.path.dirname(inspect.getmodule(plugin).__file__)
 
96
 
 
97
            return os.path.join(plugindir, mediadir, self.path)
 
98
 
 
99
class MediaFileView(BaseView):
 
100
    def authorize(self, req):
 
101
        return True
69
102
 
70
103
    def render(self, req):
71
104
        # If it begins with ".." or separator, it's illegal. Die.
72
 
        if self.path.startswith("..") or self.path.startswith('/'):
 
105
        if self.context.path.startswith("..") or \
 
106
           self.context.path.startswith('/'):
73
107
            raise Forbidden()
74
108
 
75
 
        filename = self._make_filename(req)
 
109
        filename = self.get_filename(req)
 
110
        if filename is None:
 
111
            raise NotFound()
76
112
 
77
113
        # Find an appropriate MIME type.
78
114
        (type, _) = mimetypes.guess_type(filename)
85
121
        if not os.access(filename, os.R_OK) or os.path.isdir(filename):
86
122
            raise Forbidden()
87
123
 
 
124
        if self.context.version is not None:
 
125
            req.headers_out['Expires'] = email.utils.formatdate(
 
126
                                timeval=time.time() + (60*60*24*365),
 
127
                                localtime=False,
 
128
                                usegmt=True)
 
129
 
 
130
 
88
131
        req.content_type = type
89
132
        req.sendfile(filename)
90
133
 
91
 
 
92
 
class MediaFileView(BaseMediaFileView):
93
 
    '''A view for media files.
94
 
 
95
 
    This serves static files from directories registered by plugins.
96
 
 
97
 
    Plugins wishing to export media should declare a 'media' attribute,
98
 
    pointing to the directory to serve (relative to the module's directory).
99
 
    The contents of that directory will then be available under
100
 
    /+media/python.path.to.module.
101
 
    '''
102
 
    permission = None
103
 
 
104
 
    def _make_filename(self, req):
105
 
        try:
106
 
            plugin = req.config.plugins[self.ns]
107
 
        except KeyError:
108
 
            raise NotFound()
109
 
 
110
 
        if not issubclass(plugin, MediaPlugin):
111
 
            raise NotFound()
112
 
 
113
 
        mediadir = plugin.media
114
 
        plugindir = os.path.dirname(inspect.getmodule(plugin).__file__)
115
 
 
116
 
        return os.path.join(plugindir, mediadir, self.path)
117
 
 
118
 
    def get_permissions(self, user):
119
 
        return set()
120
 
 
121
 
class VersionedMediaFileView(MediaFileView):
122
 
    '''A view for versioned media files, with aggressive caching.
123
 
 
124
 
    This serves static media files with a version string, and requests that
125
 
    browsers cache them for a long time.
126
 
    '''
127
 
 
128
 
    def __init__(self, req, ns, path, version):
129
 
        super(VersionedMediaFileView, self).__init__(req, ns, path)
130
 
        self.version = version
131
 
 
132
 
    def _make_filename(self, req):
133
 
        if self.version != Config()['media']['version']:
134
 
            raise NotFound()
135
 
 
136
 
        # Don't expire for a year.
137
 
        req.headers_out['Expires'] = email.utils.formatdate(
138
 
                                    timeval=time.time() + (60*60*24*365),
139
 
                                    localtime=False,
140
 
                                    usegmt=True)
141
 
        return super(VersionedMediaFileView, self)._make_filename(req)
 
134
    def get_filename(self, req):
 
135
        return self.context.filename
 
136
 
 
137
def root_to_media(root, *segments):
 
138
    if segments[0].startswith('+'):
 
139
        if segments[0] == '+external':
 
140
            external = True
 
141
            version = None
 
142
            path = segments[1:]
 
143
        else:
 
144
            version = segments[0][1:]
 
145
            if segments[1] == '+external':
 
146
                external = True
 
147
                path = segments[2:]
 
148
            else:
 
149
                external = False
 
150
                path = segments[1:]
 
151
    else:
 
152
        external = False
 
153
        version = None
 
154
        path = segments
 
155
 
 
156
    if version is not None and version != root.config['media']['version']:
 
157
        return None
 
158
 
 
159
    ns = path[0]
 
160
    path = os.path.normpath(os.path.join(*path[1:]))
 
161
 
 
162
    return MediaFile(root, external, version, ns, path)
142
163
 
143
164
class Plugin(ViewPlugin, PublicViewPlugin):
144
 
    urls = [
145
 
        ('+media/+:version/:ns/*path', VersionedMediaFileView),
146
 
        ('+media/:ns/*path', MediaFileView),
147
 
    ]
148
 
 
149
 
    public_urls = urls
 
165
    forward_routes = [(ApplicationRoot, '+media', root_to_media, INF)]
 
166
    views = [(MediaFile, '+index', MediaFileView)]
 
167
    public_forward_routes = forward_routes
 
168
    public_views = views