From ce34cf1bafc99e6c90bbc3d9d12347f3b3537c48 Mon Sep 17 00:00:00 2001 From: Josh Schneier Date: Fri, 15 Mar 2024 19:16:51 -0400 Subject: [PATCH] [gcloud] Fix saving already gzipped files & tests (#1366) --- storages/backends/gcloud.py | 7 +++++-- tests/test_gcloud.py | 16 ++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/storages/backends/gcloud.py b/storages/backends/gcloud.py index ace69244..4c582b69 100644 --- a/storages/backends/gcloud.py +++ b/storages/backends/gcloud.py @@ -39,7 +39,7 @@ class GoogleCloudFile(CompressedFileMixin, File): def __init__(self, name, mode, storage): self.name = name - self.mime_type = mimetypes.guess_type(name)[0] + self.mime_type, self.mime_encoding = mimetypes.guess_type(name) self._mode = mode self._storage = storage self.blob = storage.bucket.get_blob(name, chunk_size=storage.blob_chunk_size) @@ -190,8 +190,11 @@ def _save(self, name, content): content.name = cleaned_name file_object = GoogleCloudFile(name, "rw", self) - upload_params = {} blob_params = self.get_object_parameters(name) + if file_object.mime_encoding and CONTENT_ENCODING not in blob_params: + blob_params[CONTENT_ENCODING] = file_object.mime_encoding + + upload_params = {} upload_params["predefined_acl"] = blob_params.pop("acl", self.default_acl) upload_params[CONTENT_TYPE] = blob_params.pop( CONTENT_TYPE, file_object.mime_type diff --git a/tests/test_gcloud.py b/tests/test_gcloud.py index 78792ede..20cd8c6b 100644 --- a/tests/test_gcloud.py +++ b/tests/test_gcloud.py @@ -21,17 +21,15 @@ def setUp(self): self.bucket_name = "test_bucket" self.filename = "test_file.txt" self.storage = gcloud.GoogleCloudStorage(bucket_name=self.bucket_name) - - -class GCloudStorageTests(GCloudTestCase): - def setUp(self): - super().setUp() self.client_patcher = mock.patch("storages.backends.gcloud.Client") self.client_patcher.start() def tearDown(self): + super().tearDown() self.client_patcher.stop() + +class GCloudStorageTests(GCloudTestCase): def test_open_read(self): """ Test opening a file and reading from it @@ -507,7 +505,6 @@ def setUp(self): self.storage.gzip = True @mock.patch("google.cloud.storage.blob.Blob._do_upload") - @mock.patch("google.auth.default", return_value=["foo", None]) def test_storage_save_gzipped(self, *args): """ Test saving a gzipped file @@ -521,11 +518,12 @@ def test_storage_save_gzipped(self, *args): try: patcher.start() self.storage.save(name, content) - blob.upload_from_file.assert_called_with( + obj = self.storage._bucket.get_blob() + obj.upload_from_file.assert_called_with( mock.ANY, rewind=True, retry=DEFAULT_RETRY, - size=None, + size=11, predefined_acl=None, content_type="application/javascript", ) @@ -533,12 +531,10 @@ def test_storage_save_gzipped(self, *args): patcher.stop() @mock.patch("google.cloud.storage.blob.Blob._do_upload") - @mock.patch("google.auth.default", return_value=["foo", None]) def test_storage_save_gzip(self, *args): """ Test saving a file with gzip enabled. """ - self.storage.gzip = True name = "test_storage_save.css" content = ContentFile("I should be gzip'd")