Skip to content

Commit ea622d8

Browse files
committed
Squashed commit of the following:
#56 #57
2 parents 527abf9 + c2ded44 commit ea622d8

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

docs/index.rst

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Flask-S3 know about your :class:`flask.Flask` application object.
7777
from flask_s3 import FlaskS3
7878
7979
app = Flask(__name__)
80-
app.config['S3_BUCKET_NAME'] = 'mybucketname'
80+
app.config['FLASKS3_BUCKET_NAME'] = 'mybucketname'
8181
s3 = FlaskS3(app)
8282
8383
In many cases, however, one cannot expect a Flask instance to be ready
@@ -143,11 +143,11 @@ Setting Custom HTTP Headers
143143
~~~~~~~~~~~~~~~~~
144144

145145
To set custom HTTP headers on the files served from S3 specify what
146-
headers you want to use with the `S3_HEADERS` option.
146+
headers you want to use with the `FLASKS3_HEADERS` option.
147147

148148
.. code-block:: python
149149
150-
S3_HEADERS = {
150+
FLASKS3_HEADERS = {
151151
'Expires': 'Thu, 15 Apr 2010 20:00:00 GMT',
152152
'Cache-Control': 'max-age=86400',
153153
}
@@ -180,7 +180,7 @@ uploading assets to S3.
180180
`FLASKS3_CDN_DOMAIN` AWS makes it easy to attach CloudFront to an S3
181181
bucket. If you want to use this or another CDN,
182182
set the base domain here. This is distinct from the
183-
`S3_BUCKET_DOMAIN` since it will not include the
183+
`FLASKS3_BUCKET_DOMAIN` since it will not include the
184184
bucket name in the base url.
185185
`FLASKS3_BUCKET_NAME` The desired name for your Amazon S3 bucket. Note:
186186
the name will be visible in all your assets' URLs.
@@ -201,17 +201,16 @@ uploading assets to S3.
201201
`flask.url_for`.
202202
**Default:** `True`
203203
**Note**: if you run your application in `debug`_
204-
mode (and `USE_S3_DEBUG` is `False` - see next
205-
item), `USE_S3` will be changed to `False`. This
206-
allows the `USE_S3` config variable to be the
207-
definitive check as to whether `flask_s3.url_for`
204+
mode (and `FLASKS3_DEBUG` is `False` - see next
205+
item), `FLASKS3_ACTIVE` will be changed to `False`.
206+
This allows the `FLASKS3_ACTIVE` config variable to be the definitive check as to whether `flask_s3.url_for`
208207
is overriding `flask.url_for`.
209208
`FLASKS3_DEBUG` By default, Flask-S3 will be switched off when
210209
running your application in `debug`_ mode, so that
211210
your templates include static asset locations
212211
specified by `flask.url_for`. If you wish to enable
213212
Flask-S3 in debug mode, set this value to `True`.
214-
**Note**: if `USE_S3` is set to `False` then
213+
**Note**: if `FLASKS3_ACTIVE` is set to `False` then
215214
templates will always include asset locations
216215
specified by `flask.url_for`.
217216
`FLASKS3_HEADERS` Sets custom headers to be sent with each file to S3.
@@ -228,10 +227,17 @@ uploading assets to S3.
228227
`FLASKS3_ONLY_MODIFIED` Only upload files that have been modified since last
229228
upload to S3. SHA-1 file hashes are used to compute
230229
file changes. You can delete `.file-hashes` from
231-
your S3 bucket to force all files to upload again.ad.
230+
your S3 bucket to force all files to upload again.
232231
`FLASKS3_GZIP` Compress all assets using GZIP and set the
233232
corresponding Content-Type and Content-Encoding
234233
headers on the S3 files.
234+
`FLASKS3_GZIP_ONLY_EXTS` A list of file extensions that should be gzipped.
235+
``FLASKS3_GZIP`` should be ``True`` for this to take effect.
236+
If mentioned and non-empty, then only files with the
237+
specified extensions are gzipped.
238+
Defaults to empty list, meaning all files will be
239+
gzipped.
240+
Eg:- ``['.js', '.css']`` will gzip only js and css files.
235241
`FLASKS3_FORCE_MIMETYPE` Always set the Content-Type header on the S3 files
236242
irrespective of gzipping. Defaults to `False`.
237243
=========================== ===================================================

flask_s3.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
'expires': 'Expires',
3838
}
3939

40-
__version__ = (0, 2, 7, "post2")
40+
__version__ = (0, 2, 8)
4141

4242
def split_metadata_params(headers):
4343
"""
@@ -194,9 +194,11 @@ def _write_files(s3, app, static_url_loc, static_folder, files, bucket,
194194
""" Writes all the files inside a static folder to S3. """
195195
should_gzip = app.config.get('FLASKS3_GZIP')
196196
add_mime = app.config.get('FLASKS3_FORCE_MIMETYPE')
197+
gzip_include_only = app.config.get('FLASKS3_GZIP_ONLY_EXTS')
197198
new_hashes = []
198199
static_folder_rel = _path_to_relative_url(static_folder)
199200
for file_path in files:
201+
per_file_should_gzip = should_gzip
200202
asset_loc = _path_to_relative_url(file_path)
201203
full_key_name = _static_folder_path(static_url_loc, static_folder_rel,
202204
asset_loc)
@@ -224,10 +226,15 @@ def _write_files(s3, app, static_url_loc, static_folder, files, bucket,
224226
for header, value in headers.iteritems():
225227
h[header] = value
226228

227-
if should_gzip:
229+
# check for extension, only if there are extensions provided
230+
if per_file_should_gzip and gzip_include_only:
231+
if os.path.splitext(file_path)[1] not in gzip_include_only:
232+
per_file_should_gzip = False
233+
234+
if per_file_should_gzip:
228235
h["content-encoding"] = "gzip"
229236

230-
if add_mime or should_gzip and "content-type" not in h:
237+
if add_mime or per_file_should_gzip and "content-type" not in h:
231238
# When we use GZIP we have to explicitly set the content type
232239
# or if the mime flag is True
233240
(mimetype, encoding) = mimetypes.guess_type(file_path,
@@ -240,7 +247,7 @@ def _write_files(s3, app, static_url_loc, static_folder, files, bucket,
240247

241248
with open(file_path) as fp:
242249
metadata, params = split_metadata_params(merge_two_dicts(app.config['FLASKS3_HEADERS'], h))
243-
if should_gzip:
250+
if per_file_should_gzip:
244251
compressed = StringIO()
245252
z = gzip.GzipFile(os.path.basename(file_path), 'wb', 9,
246253
compressed)
@@ -464,6 +471,7 @@ def init_app(self, app):
464471
('FLASKS3_ONLY_MODIFIED', False),
465472
('FLASKS3_URL_STYLE', 'host'),
466473
('FLASKS3_GZIP', False),
474+
('FLASKS3_GZIP_ONLY_EXTS', []),
467475
('FLASKS3_FORCE_MIMETYPE', False)]
468476

469477
for k, v in defaults:

0 commit comments

Comments
 (0)