Skip to content

Commit

Permalink
[gcloud] Fix double decompression when using gzip (#1457)
Browse files Browse the repository at this point in the history
  • Loading branch information
thuibr authored Jan 11, 2025
1 parent b3513ec commit b79ea31
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 1 addition & 2 deletions storages/backends/gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ def _get_file(self):
)
if "r" in self._mode:
self._is_dirty = False
# This automatically decompresses the file
self.blob.download_to_file(self._file, checksum="crc32c")
self._file.seek(0)
if self._storage.gzip and self.blob.content_encoding == "gzip":
self._file = self._decompress_file(mode=self._mode, file=self._file)
return self._file

def _set_file(self, value):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from google.cloud.storage.retry import DEFAULT_RETRY

from storages.backends import gcloud
from storages.backends.gcloud import GoogleCloudFile


class GCloudTestCase(TestCase):
Expand Down Expand Up @@ -571,3 +572,17 @@ def test_storage_save_gzip(self, *args):
self.assertEqual(zfile.read(), b"I should be gzip'd")
finally:
patcher.stop()

def test_storage_read_gzip(self, *args):
"""
Test reading a gzipped file decompresses content only once.
"""
name = "test_storage_save.css"
file = GoogleCloudFile(name, "rb", self.storage)
blob = mock.MagicMock()
file.blob = blob
blob.download_to_file = lambda f, checksum=None: f.write(b"No gzip")
blob.content_encoding = "gzip"
f = file._get_file()

f.read() # This should not fail

0 comments on commit b79ea31

Please sign in to comment.