Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Double Decompression When Using GCloud GZIP #1457

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
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: f.write(b"I should not be gzip'd")
blob.content_encoding = "gzip"
f = file._get_file()

f.read() # This should not fail