From cd68d14a2f48d6b80a10cea1be740be2132978f9 Mon Sep 17 00:00:00 2001 From: Tobias Henkel Date: Mon, 30 Jul 2018 14:46:25 +0200 Subject: [PATCH] Also compress json files Json files typically get the mimetime application/json and thus are not a sub type of text. However we want to compress them too. Change-Id: I4f0532c6fe595dcebe2c21d8634691a2b1ceb6ed --- .../library/test-fixtures/logs/job-output.json | 1 + .../library/test_zuul_swift_upload.py | 8 ++++++-- .../library/zuul_swift_upload.py | 17 ++++++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 roles/wip-upload-logs-swift/library/test-fixtures/logs/job-output.json diff --git a/roles/wip-upload-logs-swift/library/test-fixtures/logs/job-output.json b/roles/wip-upload-logs-swift/library/test-fixtures/logs/job-output.json new file mode 100644 index 000000000..c8cd7e92d --- /dev/null +++ b/roles/wip-upload-logs-swift/library/test-fixtures/logs/job-output.json @@ -0,0 +1 @@ +{"test": "foo"} diff --git a/roles/wip-upload-logs-swift/library/test_zuul_swift_upload.py b/roles/wip-upload-logs-swift/library/test_zuul_swift_upload.py index e44d599ea..ddda40c42 100644 --- a/roles/wip-upload-logs-swift/library/test_zuul_swift_upload.py +++ b/roles/wip-upload-logs-swift/library/test_zuul_swift_upload.py @@ -57,6 +57,7 @@ class TestFileList(unittest.TestCase): ('', 'application/directory', None), ('controller', 'application/directory', None), ('zuul-info', 'application/directory', None), + ('job-output.json', 'application/json', None), ('controller/subdir', 'application/directory', None), ('controller/compressed.gz', 'text/plain', 'gzip'), ('controller/journal.xz', 'text/plain', 'xz'), @@ -76,6 +77,7 @@ class TestFileList(unittest.TestCase): ('logs', 'application/directory', None), ('logs/controller', 'application/directory', None), ('logs/zuul-info', 'application/directory', None), + ('logs/job-output.json', 'application/json', None), ('logs/controller/subdir', 'application/directory', None), ('logs/controller/compressed.gz', 'text/plain', 'gzip'), ('logs/controller/journal.xz', 'text/plain', 'xz'), @@ -107,9 +109,10 @@ class TestFileList(unittest.TestCase): ('', 'application/directory', None), ('index.html', 'text/html', None), ('logs', 'application/directory', None), - ('logs/index.html', 'text/html', None), ('logs/controller', 'application/directory', None), ('logs/zuul-info', 'application/directory', None), + ('logs/job-output.json', 'application/json', None), + ('logs/index.html', 'text/html', None), ('logs/controller/subdir', 'application/directory', None), ('logs/controller/compressed.gz', 'text/plain', 'gzip'), ('logs/controller/journal.xz', 'text/plain', 'xz'), @@ -154,9 +157,10 @@ class TestFileList(unittest.TestCase): self.assert_files(fl, [ ('', 'application/directory', None), - ('index.html', 'text/html', None), ('controller', 'application/directory', None), ('zuul-info', 'application/directory', None), + ('job-output.json', 'application/json', None), + ('index.html', 'text/html', None), ('controller/subdir', 'application/directory', None), ('controller/compressed.gz', 'text/plain', 'gzip'), ('controller/journal.xz', 'text/plain', 'xz'), diff --git a/roles/wip-upload-logs-swift/library/zuul_swift_upload.py b/roles/wip-upload-logs-swift/library/zuul_swift_upload.py index 561e51d76..83d887975 100755 --- a/roles/wip-upload-logs-swift/library/zuul_swift_upload.py +++ b/roles/wip-upload-logs-swift/library/zuul_swift_upload.py @@ -392,6 +392,21 @@ class Uploader(object): # No more work to do return + @staticmethod + def _is_text_type(mimetype): + # We want to compress all text types. + if mimetype.startswith('text/'): + return True + + # Further compress types that typically contain text but are no + # text sub type. + compress_types = [ + 'application/json', + ] + if mimetype in compress_types: + return True + return False + def _post_file(self, fd): relative_path = os.path.join(self.prefix, fd.relative_path) headers = {} @@ -404,7 +419,7 @@ class Uploader(object): for attempt in range(3): try: if not fd.folder: - if fd.encoding is None and fd.mimetype.startswith('text/'): + if fd.encoding is None and self._is_text_type(fd.mimetype): headers['content-encoding'] = 'deflate' data = DeflateFilter(open(fd.full_path, 'rb')) else: