This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
forked from e-dard/flask-s3
-
Notifications
You must be signed in to change notification settings - Fork 4
/
flask_cdn.py
98 lines (74 loc) · 3.21 KB
/
flask_cdn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
from flask import url_for as flask_url_for
from flask import current_app, request
def url_for(endpoint, **values):
"""
Generates a URL to the given endpoint.
If the endpoint is for a static resource then a URL to the CDN is
generated, otherwise the call is passed on to `flask.url_for`.
Because this function is set as a jinja environment variable when
`CDN.init_app` is invoked, this function replaces `flask.url_for` in
templates automatically. It is unlikely that this function will need to be
directly called from within your application code, unless you need to refer
to static assets outside of your templates.
"""
app = current_app
if app.config['CDN_DEBUG']:
return flask_url_for(endpoint, **values)
def endpoint_match(endpoint):
if endpoint in app.config['CDN_ENDPOINTS']:
return True
for x in app.config['CDN_ENDPOINTS']:
if endpoint.endswith('.%s' % x):
return True
return False
if endpoint_match(endpoint):
try:
scheme = values.pop('_scheme')
except KeyError:
scheme = 'http'
cdn_https = app.config['CDN_HTTPS']
if cdn_https is True or (cdn_https is None and request.is_secure):
scheme = 'https'
static_folder = app.static_folder
if (request.blueprint is not None and
request.blueprint in app.blueprints and
app.blueprints[request.blueprint].has_static_folder):
static_folder = app.blueprints[request.blueprint].static_folder
urls = app.url_map.bind(app.config['CDN_DOMAIN'], url_scheme=scheme)
if app.config['CDN_TIMESTAMP']:
path = os.path.join(static_folder, values['filename'])
values['t'] = int(os.path.getmtime(path))
values['v'] = app.config['CDN_VERSION']
return urls.build(endpoint, values=values, force_external=True)
return flask_url_for(endpoint, **values)
class CDN(object):
"""
The CDN object allows your application to use Flask-CDN.
When initialising a CDN object you may optionally provide your
:class:`flask.Flask` application object if it is ready. Otherwise,
you may provide it later by using the :meth:`init_app` method.
:param app: optional :class:`flask.Flask` application object
:type app: :class:`flask.Flask` or None
"""
def __init__(self, app=None):
"""
An alternative way to pass your :class:`flask.Flask` application
object to Flask-CDN. :meth:`init_app` also takes care of some
default `settings`_.
:param app: the :class:`flask.Flask` application object.
"""
self.app = app
if app is not None:
self.init_app(app)
def init_app(self, app):
defaults = [('CDN_DEBUG', app.debug),
('CDN_DOMAIN', None),
('CDN_HTTPS', None),
('CDN_TIMESTAMP', True),
('CDN_VERSION', None),
('CDN_ENDPOINTS', ['static'])]
for k, v in defaults:
app.config.setdefault(k, v)
if app.config['CDN_DOMAIN']:
app.jinja_env.globals['url_for'] = url_for