Skip to content

Commit

Permalink
Expand DEFAULT_NOCOMPRESS, fix issue with needs_rotation, write CHANG…
Browse files Browse the repository at this point in the history
…ELOG
  • Loading branch information
dcwatson committed Nov 8, 2024
1 parent 2a602f8 commit 4274f64
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ __pycache__
.coverage
/htmlcov
.nova
.python-version
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## 1.2.0

*Note we skipped a version to align with `pzip`*

* Drop support for Django 3.x and Python 3.8
* Test on Django 5.x and Python 3.13
* Fixed an issue where the `needs_rotation` signal could be sent multiple times, and for
invalid keys
* Require `pzip>=1.2.0` to address an issue on Python 3.13
* Expanded `PZipStorage.DEFAULT_NOCOMPRESS` to include more already-compressed filetypes
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ You may also use `PZipStorage` as a custom storage backend anywhere Django allow

* `PZIP_STORAGE_EXTENSION` - the extension to append to any file saved with `PZipStorage`. Defaults to `.pz`.
* `PZIP_STORAGE_NOCOMPRESS` - a set of file extensions (with leading period) which should not be compressed when
saving. Defaults to:
`[".z", ".gz", ".zip", ".tgz", ".jpg", ".jpeg", ".png", ".gif", ".sit", ".sitx", ".7z", ".pz", ".bz2", ".xz"]`
saving. See `PZipStorage.DEFAULT_NOCOMPRESS` for the default list.
* `PZIP_STORAGE_KEYS` - an iterable (or callable returning an iterable) of keys to use. The first key on the list will
be used for encrypting files. Defaults to `PZipStorage.default_keys`, which yields `SECRET_KEY`.

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ classifiers = [
"Programming Language :: Python :: 3",
]
dependencies = [
"pzip>=0.9.9"
"pzip>=1.2.0"
]

[dependency-groups]
Expand Down
23 changes: 17 additions & 6 deletions pzip_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.core.files.storage import FileSystemStorage
from django.utils.encoding import force_bytes

__version__ = "1.1.0"
__version__ = "1.2.0"
__version_info__ = tuple(int(num) for num in __version__.split("."))


Expand All @@ -33,6 +33,7 @@ class PZipStorage(FileSystemStorage):
".z",
".gz",
".zip",
".zipx",
".tgz",
".jpg",
".jpeg",
Expand All @@ -44,6 +45,16 @@ class PZipStorage(FileSystemStorage):
".pz",
".bz2",
".xz",
".rar",
".cab",
".zst",
".lz",
".lzma",
".lz4",
".jar",
".heic",
".avif",
".webp",
]
)

Expand Down Expand Up @@ -90,17 +101,17 @@ def _open(self, name, mode="rb"):
for idx, key in enumerate(self.iter_keys()):
try:
f = super()._open(name, mode)
return pzip.open(f, mode, key=key, peek=True)
except pzip.InvalidFile:
# Close the underlying fileobj if PZip fails to decode.
f.close()
finally:
pz = pzip.open(f, mode, key=key, peek=True)
if idx > 0:
# If we opened this file with an old key, broadcast a signal for
# callers to do rotation.
needs_rotation.send(
sender=self.__class__, storage=self, name=name, key=key
)
return pz
except pzip.InvalidFile:
# Close the underlying fileobj if PZip fails to decode.
f.close()
# If we tried all the keys and haven't returned yet for a PZip file, send a
# bad_keys signal.
bad_keys.send(sender=self.__class__, storage=self, name=name)
Expand Down
1 change: 1 addition & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_multiple_keys(self):
storage = PZipStorage(keys=lambda: keys)
name = storage.save("testfile", ContentFile(plaintext))
keys.insert(0, b"second")
keys.insert(0, b"third")
with storage.open(name) as f:
self.assertEqual(plaintext, f.read())
handler.assert_called_once_with(
Expand Down
10 changes: 5 additions & 5 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4274f64

Please sign in to comment.