From 131f0548a7127ff5f9954b03680057d76b483f4b Mon Sep 17 00:00:00 2001 From: "bool.dev" Date: Thu, 3 Dec 2015 13:55:46 +0530 Subject: [PATCH 1/3] Proposed solution for #55, only certain files benefit from gzipping. --- docs/index.rst | 7 +++++++ flask_s3.py | 14 +++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 64bbebf..6d60f22 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -232,6 +232,13 @@ uploading assets to S3. `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`. =========================== =================================================== diff --git a/flask_s3.py b/flask_s3.py index 4d22864..db683a1 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -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) @@ -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, @@ -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) @@ -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: From 9d6ff57bd4b00c0117deb1539f0c6eb80d06f3a1 Mon Sep 17 00:00:00 2001 From: Isaac Dickinson Date: Thu, 3 Dec 2015 13:03:44 +0000 Subject: [PATCH 2/3] Bump version number --- flask_s3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_s3.py b/flask_s3.py index db683a1..9000b16 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -37,7 +37,7 @@ 'expires': 'Expires', } -__version__ = (0, 2, 7, "post2") +__version__ = (0, 2, 8) def split_metadata_params(headers): """ From bc94843e6d6b70857779d9f133685900d7f05b61 Mon Sep 17 00:00:00 2001 From: "bool.dev" Date: Thu, 3 Dec 2015 19:51:22 +0530 Subject: [PATCH 3/3] Fixed documentation error introduced in pr #56. Fixed minor typo in doc from before. Added documentation changes pending after change to new config var naming structure. --- docs/index.rst | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 6d60f22..5e936ef 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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 @@ -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', } @@ -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. @@ -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. @@ -228,17 +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. + ``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. + 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`. =========================== ===================================================