Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuyukai committed Dec 3, 2015
2 parents 527abf9 + c2ded44 commit ea622d8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
26 changes: 16 additions & 10 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Flask-S3 know about your :class:`flask.Flask` application object.
from flask_s3 import FlaskS3
app = Flask(__name__)
app.config['S3_BUCKET_NAME'] = 'mybucketname'
app.config['FLASKS3_BUCKET_NAME'] = 'mybucketname'
s3 = FlaskS3(app)
In many cases, however, one cannot expect a Flask instance to be ready
Expand Down Expand Up @@ -143,11 +143,11 @@ Setting Custom HTTP Headers
~~~~~~~~~~~~~~~~~

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

.. code-block:: python
S3_HEADERS = {
FLASKS3_HEADERS = {
'Expires': 'Thu, 15 Apr 2010 20:00:00 GMT',
'Cache-Control': 'max-age=86400',
}
Expand Down Expand Up @@ -180,7 +180,7 @@ uploading assets to S3.
`FLASKS3_CDN_DOMAIN` AWS makes it easy to attach CloudFront to an S3
bucket. If you want to use this or another CDN,
set the base domain here. This is distinct from the
`S3_BUCKET_DOMAIN` since it will not include the
`FLASKS3_BUCKET_DOMAIN` since it will not include the
bucket name in the base url.
`FLASKS3_BUCKET_NAME` The desired name for your Amazon S3 bucket. Note:
the name will be visible in all your assets' URLs.
Expand All @@ -201,17 +201,16 @@ uploading assets to S3.
`flask.url_for`.
**Default:** `True`
**Note**: if you run your application in `debug`_
mode (and `USE_S3_DEBUG` is `False` - see next
item), `USE_S3` will be changed to `False`. This
allows the `USE_S3` config variable to be the
definitive check as to whether `flask_s3.url_for`
mode (and `FLASKS3_DEBUG` is `False` - see next
item), `FLASKS3_ACTIVE` will be changed to `False`.
This allows the `FLASKS3_ACTIVE` config variable to be the definitive check as to whether `flask_s3.url_for`
is overriding `flask.url_for`.
`FLASKS3_DEBUG` By default, Flask-S3 will be switched off when
running your application in `debug`_ mode, so that
your templates include static asset locations
specified by `flask.url_for`. If you wish to enable
Flask-S3 in debug mode, set this value to `True`.
**Note**: if `USE_S3` is set to `False` then
**Note**: if `FLASKS3_ACTIVE` is set to `False` then
templates will always include asset locations
specified by `flask.url_for`.
`FLASKS3_HEADERS` Sets custom headers to be sent with each file to S3.
Expand All @@ -228,10 +227,17 @@ uploading assets to S3.
`FLASKS3_ONLY_MODIFIED` Only upload files that have been modified since last
upload to S3. SHA-1 file hashes are used to compute
file changes. You can delete `.file-hashes` from
your S3 bucket to force all files to upload again.ad.
your S3 bucket to force all files to upload again.
`FLASKS3_GZIP` Compress all assets using GZIP and set the
corresponding Content-Type and Content-Encoding
headers on the S3 files.
`FLASKS3_GZIP_ONLY_EXTS` A list of file extensions that should be gzipped.
``FLASKS3_GZIP`` should be ``True`` for this to take effect.
If mentioned and non-empty, then only files with the
specified extensions are gzipped.
Defaults to empty list, meaning all files will be
gzipped.
Eg:- ``['.js', '.css']`` will gzip only js and css files.
`FLASKS3_FORCE_MIMETYPE` Always set the Content-Type header on the S3 files
irrespective of gzipping. Defaults to `False`.
=========================== ===================================================
Expand Down
16 changes: 12 additions & 4 deletions flask_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
'expires': 'Expires',
}

__version__ = (0, 2, 7, "post2")
__version__ = (0, 2, 8)

def split_metadata_params(headers):
"""
Expand Down Expand Up @@ -194,9 +194,11 @@ def _write_files(s3, app, static_url_loc, static_folder, files, bucket,
""" Writes all the files inside a static folder to S3. """
should_gzip = app.config.get('FLASKS3_GZIP')
add_mime = app.config.get('FLASKS3_FORCE_MIMETYPE')
gzip_include_only = app.config.get('FLASKS3_GZIP_ONLY_EXTS')
new_hashes = []
static_folder_rel = _path_to_relative_url(static_folder)
for file_path in files:
per_file_should_gzip = should_gzip
asset_loc = _path_to_relative_url(file_path)
full_key_name = _static_folder_path(static_url_loc, static_folder_rel,
asset_loc)
Expand Down Expand Up @@ -224,10 +226,15 @@ def _write_files(s3, app, static_url_loc, static_folder, files, bucket,
for header, value in headers.iteritems():
h[header] = value

if should_gzip:
# check for extension, only if there are extensions provided
if per_file_should_gzip and gzip_include_only:
if os.path.splitext(file_path)[1] not in gzip_include_only:
per_file_should_gzip = False

if per_file_should_gzip:
h["content-encoding"] = "gzip"

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

with open(file_path) as fp:
metadata, params = split_metadata_params(merge_two_dicts(app.config['FLASKS3_HEADERS'], h))
if should_gzip:
if per_file_should_gzip:
compressed = StringIO()
z = gzip.GzipFile(os.path.basename(file_path), 'wb', 9,
compressed)
Expand Down Expand Up @@ -464,6 +471,7 @@ def init_app(self, app):
('FLASKS3_ONLY_MODIFIED', False),
('FLASKS3_URL_STYLE', 'host'),
('FLASKS3_GZIP', False),
('FLASKS3_GZIP_ONLY_EXTS', []),
('FLASKS3_FORCE_MIMETYPE', False)]

for k, v in defaults:
Expand Down

1 comment on commit ea622d8

@Fuyukai
Copy link
Collaborator Author

@Fuyukai Fuyukai commented on ea622d8 Dec 3, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or not, merging is hard.

Please sign in to comment.