-
-
Notifications
You must be signed in to change notification settings - Fork 865
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52a85a3
commit cf50a22
Showing
11 changed files
with
203 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,11 @@ __pycache__ | |
.cache | ||
|
||
.idea/ | ||
.vscode/ | ||
.pytest_cache/ | ||
venv/ | ||
|
||
dist/ | ||
docs/_build | ||
|
||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,8 @@ sftp = | |
[flake8] | ||
exclude = | ||
.tox, | ||
docs | ||
docs, | ||
venv | ||
max-line-length = 119 | ||
|
||
[isort] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import io | ||
import zlib | ||
from gzip import GzipFile | ||
from typing import Optional | ||
|
||
from storages.utils import to_bytes | ||
|
||
|
||
class GzipCompressionWrapper(io.RawIOBase): | ||
"""Wrapper for compressing file contents on the fly.""" | ||
|
||
def __init__(self, raw, level=zlib.Z_BEST_COMPRESSION): | ||
super().__init__() | ||
self.raw = raw | ||
self.compress = zlib.compressobj(level=level, wbits=31) | ||
self.leftover = bytearray() | ||
|
||
@staticmethod | ||
def readable(): | ||
return True | ||
|
||
def readinto(self, buf: bytearray) -> Optional[int]: | ||
size = len(buf) | ||
while len(self.leftover) < size: | ||
chunk = to_bytes(self.raw.read(size)) | ||
if not chunk: | ||
if self.compress: | ||
self.leftover += self.compress.flush(zlib.Z_FINISH) | ||
self.compress = None | ||
break | ||
self.leftover += self.compress.compress(chunk) | ||
if len(self.leftover) == 0: | ||
return 0 | ||
output = self.leftover[:size] | ||
size = len(output) | ||
buf[:size] = output | ||
self.leftover = self.leftover[size:] | ||
return size | ||
|
||
|
||
class CompressStorageMixin(): | ||
def _compress_content(self, content): | ||
"""Gzip a given string content.""" | ||
return GzipCompressionWrapper(content) | ||
|
||
|
||
class CompressedFileMixin(): | ||
def _decompress_file(self, mode, file, mtime=0.0): | ||
return GzipFile(mode=mode, fileobj=file, mtime=mtime) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.