forked from e-dard/flask-s3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flask_cdn.py
82 lines (61 loc) · 2.78 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
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.debug and not app.config['CDN_DEBUG']:
return flask_url_for(endpoint, **values)
if endpoint == 'static' or endpoint.endswith('.static'):
cdn_https = app.config['CDN_HTTPS']
scheme = 'http'
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 app.blueprints[request.blueprint].static_folder:
static_folder = app.blueprints[request.blueprint].static_folder
cdn_domain = app.config['CDN_DOMAIN']
urls = app.url_map.bind(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))
url = urls.build(endpoint, values=values, force_external=True)
return url.replace(cdn_domain.lower(), cdn_domain)
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_DOMAIN', None),
('CDN_DEBUG', False),
('CDN_HTTPS', None),
('CDN_TIMESTAMP', True)]
for k, v in defaults:
app.config.setdefault(k, v)
if app.config['CDN_DOMAIN']:
app.jinja_env.globals['url_for'] = url_for