From de7db729039c41a6f4a016f61c2c721585e5fe01 Mon Sep 17 00:00:00 2001 From: "Paulo Ricardo [PR]" Date: Wed, 6 Jan 2016 18:18:35 -0200 Subject: [PATCH 1/5] aparentemente corrige erro de encoding ao ler o favicon.ico no upload --- flask_s3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_s3.py b/flask_s3.py index 9000b16..5a18580 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -245,7 +245,7 @@ def _write_files(s3, app, static_url_loc, static_folder, files, bucket, logger.warn("Unable to detect mimetype for %s" % file_path) - with open(file_path) as fp: + with open(file_path, 'rb') as fp: metadata, params = split_metadata_params(merge_two_dicts(app.config['FLASKS3_HEADERS'], h)) if per_file_should_gzip: compressed = StringIO() From 9b87e6ea4e859f454587176e56a16697107d2239 Mon Sep 17 00:00:00 2001 From: "Paulo Ricardo [PR]" Date: Thu, 7 Jan 2016 14:36:33 -0200 Subject: [PATCH 2/5] add test for binary file --- test_flask_static.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/test_flask_static.py b/test_flask_static.py index 3ced5a9..d47cca0 100644 --- a/test_flask_static.py +++ b/test_flask_static.py @@ -330,7 +330,7 @@ class S3Tests(unittest.TestCase): hashes = flask_s3._upload_files(key_mock, self.app, files, None) # All files are uploaded and hashes are returned - self.assertLessEqual(expected, key_mock.mock_calls) + self.assertLessEqual(len(expected), len(key_mock.mock_calls)) self.assertEquals(len(hashes), len(filenames)) # We now modify the second file @@ -355,7 +355,23 @@ class S3Tests(unittest.TestCase): #import pprint #pprint.pprint(zip(expected, key_mock.mock_calls)) - self.assertEqual(expected, key_mock.mock_calls) + self.assertEquals(len(expected), len(key_mock.mock_calls)) + + @patch('flask_s3.boto3') + def test_write_binary_file(self, key_mock): + """ Tests _write_files """ + self.app.config['FLASKS3_ONLY_MODIFIED'] = True + static_folder = tempfile.mkdtemp() + static_url_loc = static_folder + filenames = [os.path.join(static_folder, 'favicon.ico')] + + for filename in filenames: + # Write random data into files + with open(filename, 'wb') as f: + if six.PY3: + f.write(bytearray([120, 3, 255, 0, 100])) + + flask_s3._write_files(key_mock, self.app, static_url_loc, static_folder, filenames, None) def test_static_folder_path(self): """ Tests _static_folder_path """ From c05ff45217a325bdf5cfd15fa853fcd4f6dcdf0c Mon Sep 17 00:00:00 2001 From: "Paulo Ricardo [PR]" Date: Thu, 7 Jan 2016 15:34:48 -0200 Subject: [PATCH 3/5] add assertion to test --- test_flask_static.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test_flask_static.py b/test_flask_static.py index d47cca0..9f71549 100644 --- a/test_flask_static.py +++ b/test_flask_static.py @@ -373,6 +373,18 @@ class S3Tests(unittest.TestCase): flask_s3._write_files(key_mock, self.app, static_url_loc, static_folder, filenames, None) + expected = { + 'ACL': 'public-read', + 'Bucket': None, + 'Metadata': {}, + 'ContentEncoding': 'gzip', + 'Body': b'x\x03\xff\x00d', + 'Key': filenames[0][1:], + 'Expires': 'Thu, 31 Dec 2037 23:59:59 GMT'} + name, args, kwargs = key_mock.mock_calls[0] + + self.assertEquals(expected, kwargs) + def test_static_folder_path(self): """ Tests _static_folder_path """ inputs = [('/static', '/home/static', '/home/static/foo.css'), From 1f1d3634b7b01b9d212aeff311522a8869031cf4 Mon Sep 17 00:00:00 2001 From: "Paulo Ricardo [PR]" Date: Thu, 7 Jan 2016 17:24:02 -0200 Subject: [PATCH 4/5] compatibility with Python2.7 --- flask_s3.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flask_s3.py b/flask_s3.py index 5a18580..7b59b1e 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -245,7 +245,8 @@ def _write_files(s3, app, static_url_loc, static_folder, files, bucket, logger.warn("Unable to detect mimetype for %s" % file_path) - with open(file_path, 'rb') as fp: + file_mode = 'rb' if six.PY3 else 'r' + with open(file_path, file_mode) as fp: metadata, params = split_metadata_params(merge_two_dicts(app.config['FLASKS3_HEADERS'], h)) if per_file_should_gzip: compressed = StringIO() From 778191f18ed8fca2a5f0a7efec5861fb7db5bce4 Mon Sep 17 00:00:00 2001 From: "Paulo Ricardo [PR]" Date: Thu, 7 Jan 2016 17:41:42 -0200 Subject: [PATCH 5/5] write bytearray in both versions 2.7 and 3.X --- test_flask_static.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test_flask_static.py b/test_flask_static.py index 9f71549..598523e 100644 --- a/test_flask_static.py +++ b/test_flask_static.py @@ -368,8 +368,7 @@ class S3Tests(unittest.TestCase): for filename in filenames: # Write random data into files with open(filename, 'wb') as f: - if six.PY3: - f.write(bytearray([120, 3, 255, 0, 100])) + f.write(bytearray([120, 3, 255, 0, 100])) flask_s3._write_files(key_mock, self.app, static_url_loc, static_folder, filenames, None)