forked from sahilchinoy/public-trust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.py
97 lines (75 loc) · 2.9 KB
/
static.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
#!/usr/bin/env python
import json
from mimetypes import guess_type
import os
from re import findall
import subprocess
from flask import abort, make_response
from werkzeug.datastructures import Headers
import app_config
import copytext
from flask import Blueprint
from render_utils import BetterJSONEncoder, flatten_app_config
static = Blueprint('static', __name__)
# Render JST templates on-demand
@static.route('/js/templates.js')
def _templates_js():
r = subprocess.check_output(["node_modules/universal-jst/bin/jst.js", "--template", "underscore", "jst"])
return make_response(r, 200, { 'Content-Type': 'application/javascript' })
# Render LESS files on-demand
@static.route('/less/<string:filename>')
def _less(filename):
if not os.path.exists('less/%s' % filename):
abort(404)
r = subprocess.check_output(["node_modules/less/bin/lessc", "less/%s" % filename])
return make_response(r, 200, { 'Content-Type': 'text/css' })
# Render application configuration
@static.route('/js/app_config.js')
def _app_config_js():
config = flatten_app_config()
js = 'window.APP_CONFIG = ' + json.dumps(config, cls=BetterJSONEncoder)
return make_response(js, 200, { 'Content-Type': 'application/javascript' })
# Render copytext
@static.route('/js/copy.js')
def _copy_js():
copy = 'window.COPY = ' + copytext.Copy(app_config.COPY_PATH).json()
return make_response(copy, 200, { 'Content-Type': 'application/javascript' })
# Audio route to serve range headers for Safari.
@static.route('/assets/audio/<string:filename>')
def audio(filename):
from flask import Response, request
path = 'www/assets/audio/%s' % filename
with open(path) as f:
headers = Headers()
headers.add('Content-Disposition', 'attachment', filename=filename)
headers.add('Content-Transfer-Encoding','binary')
status = 200
size = os.path.getsize(path)
begin = 0
end = size - 1
if request.headers.has_key('Range'):
ranges = findall(r'\d+', request.headers['Range'])
begin = int(ranges[0])
if len(ranges) > 1:
end = int(ranges[1])
if begin != 0 or end != size - 1:
status = 206
headers.add('Accept-Ranges', 'bytes')
headers.add('Content-Range', 'bytes %i-%i/%i' % (begin, end, end - begin) )
headers.add('Content-Length', str( (end - begin) + 1) )
response = Response(
file(path),
status = status,
mimetype = 'application/octet-stream',
headers = headers,
direct_passthrough = True
)
return response
# Serve arbitrary static files on-demand
@static.route('/<path:path>')
def _static(path):
try:
with open('www/%s' % path) as f:
return make_response(f.read(), 200, { 'Content-Type': guess_type(path)[0] })
except IOError:
abort(404)