From 131f0548a7127ff5f9954b03680057d76b483f4b Mon Sep 17 00:00:00 2001 From: "bool.dev" Date: Thu, 3 Dec 2015 13:55:46 +0530 Subject: [PATCH] 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 @@ class FlaskS3(object): ('FLASKS3_ONLY_MODIFIED', False), ('FLASKS3_URL_STYLE', 'host'), ('FLASKS3_GZIP', False), + ('FLASKS3_GZIP_ONLY_EXTS', []), ('FLASKS3_FORCE_MIMETYPE', False)] for k, v in defaults: