forked from antonagestam/collectfast
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new strategy for cloud-based hashing static files storages (in pa…
…rticular manifest files storage)
- Loading branch information
Showing
10 changed files
with
262 additions
and
45 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
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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "3.0.1" | ||
__version__ = "3.1.0" |
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,90 @@ | ||
from typing import Type | ||
|
||
from django.contrib.staticfiles.storage import HashedFilesMixin | ||
from django.contrib.staticfiles.storage import ManifestFilesMixin | ||
from django.core.files.storage import FileSystemStorage | ||
from django.core.files.storage import Storage | ||
from django.core.files.storage.memory import InMemoryStorage | ||
|
||
from .base import HashStrategy | ||
from .base import _RemoteStorage | ||
|
||
|
||
class InMemoryHashedFilesStorage(HashedFilesMixin, InMemoryStorage): | ||
pass | ||
|
||
|
||
class FileSystemHashedFilesStorage(HashedFilesMixin, FileSystemStorage): | ||
pass | ||
|
||
|
||
class InMemoryManifestFilesStorage(ManifestFilesMixin, InMemoryStorage): | ||
pass | ||
|
||
|
||
class FileSystemManifestFilesStorage(ManifestFilesMixin, FileSystemStorage): | ||
pass | ||
|
||
|
||
class HashingTwoPassStrategy(HashStrategy[HashedFilesMixin]): | ||
""" | ||
Hashing strategies interact a lot with the remote storage as a part of post-processing | ||
This strategy will instead run the hashing strategy using InMemoryStorage first, then copy | ||
the files to the remote storage | ||
""" | ||
|
||
first_hashed_storage: Type[ManifestFilesMixin] = None | ||
first_manifest_storage: Type[HashedFilesMixin] = None | ||
second_strategy = None | ||
|
||
def __init__(self, remote_storage: _RemoteStorage) -> None: | ||
self.first_pass = True | ||
self.original_storage = remote_storage | ||
self.memory_storage = self._get_tmp_storage() | ||
self.remote_storage = self.memory_storage | ||
super().__init__(self.memory_storage) | ||
|
||
def _get_tmp_storage(self) -> Storage: | ||
if isinstance(self.original_storage, ManifestFilesMixin): | ||
return self.first_manifest_storage(location=self.original_storage.location) | ||
elif isinstance(self.original_storage, HashedFilesMixin): | ||
return self.first_hashed_storage(location=self.original_storage.location) | ||
else: | ||
raise ValueError( | ||
"HashingMemoryStrategy can only be used with subclasses of HashedFilesMixin or ManifestFilesMixin" | ||
) | ||
|
||
def wrap_storage(self, remote_storage: _RemoteStorage) -> _RemoteStorage: | ||
return self.remote_storage | ||
|
||
def get_remote_file_hash(self, prefixed_path: str) -> str | None: | ||
try: | ||
super().get_local_file_hash(prefixed_path, self.remote_storage) | ||
except FileNotFoundError: | ||
return None | ||
|
||
def second_pass_strategy(self): | ||
""" | ||
Strategy that is used after the first pass of hashing is done - to copy the files | ||
to the remote destination. | ||
""" | ||
return self.second_strategy(self.original_storage) | ||
|
||
|
||
class WithoutPrefixMixin: | ||
def copy_args_hook(self, args): | ||
return ( | ||
args[0].replace(self.remote_storage.location, ""), | ||
args[1].replace(self.remote_storage.location, ""), | ||
args[2], | ||
) | ||
|
||
|
||
class TwoPassInMemoryStrategy(HashingTwoPassStrategy): | ||
first_hashed_storage = InMemoryHashedFilesStorage | ||
first_manifest_storage = InMemoryManifestFilesStorage | ||
|
||
|
||
class TwoPassFileSystemStrategy(HashingTwoPassStrategy): | ||
first_hashed_storage = FileSystemHashedFilesStorage | ||
first_manifest_storage = FileSystemManifestFilesStorage |
Oops, something went wrong.