From aa335301f3e7a7d92ead38140a39ca18eb2454be Mon Sep 17 00:00:00 2001 From: Andrew Snowden Date: Tue, 27 Oct 2015 10:15:48 +0200 Subject: [PATCH] Fix for Python 3 and warning for unknown mimetype --- flask_s3.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/flask_s3.py b/flask_s3.py index 573cc9d..a4e80b9 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -4,7 +4,10 @@ import logging import os import re import gzip -import cStringIO +try: + from cStringIO import StringIO +except ImportError: + from io import StringIO import mimetypes from collections import defaultdict @@ -216,12 +219,16 @@ def _write_files(s3, app, static_url_loc, static_folder, files, bucket, # When we use GZIP we have to explicitly set the content type (mimetype, encoding) = mimetypes.guess_type(file_path, False) - h["content-type"] = mimetype + if mimetype: + h["content-type"] = mimetype + else: + logger.warn("Unable to detect mimetype for %s" % + file_path) with open(file_path) as fp: metadata, params = split_metadata_params(merge_two_dicts(app.config['S3_HEADERS'], h)) if should_gzip: - compressed = cStringIO.StringIO() + compressed = StringIO() z = gzip.GzipFile(os.path.basename(file_path), 'wb', 9, compressed) z.write(fp.read())