Created
April 23, 2015 01:22
-
-
Save mrcoles/33bbc2cdbc6f82d65c02 to your computer and use it in GitHub Desktop.
StaticCachedS3BotoStorage - that reads its manifest.json locally
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
from django.conf import settings | |
from django.core.files.storage import get_storage_class | |
from compressor.cache import get_offline_manifest_filename | |
from storages.backends.s3boto import S3BotoStorage | |
class StaticCachedS3BotoStorage(S3BotoStorage): | |
""" | |
S3 storage backend that also saves files locally for django-compressor | |
and also reads the manifest file from the local file-system instead of | |
S3, so packaged deploys also point to their own manifest. | |
""" | |
bucket_name = settings.AWS_BUCKET_NAME_STATIC | |
# reduced_redundancy = True | |
# headers = { | |
# 'Cache-Control': 'max-age=2592000', | |
# } | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.local_storage = get_storage_class( | |
'compressor.storage.CompressorFileStorage' | |
)() | |
self.offline_manifest_filename = get_offline_manifest_filename() | |
def save(self, name, content): | |
name = super().save(name, content) | |
self.local_storage._save(name, content) | |
return name | |
def _is_manifest(self, name): | |
return self._normalize_name(self._clean_name(name)) == self.offline_manifest_filename | |
def exists(self, name): | |
if self._is_manifest(name): | |
return self.local_storage.exists(name) | |
else: | |
return super().exists(name) | |
def _open(self, name, mode='rb'): | |
if self._is_manifest(name): | |
return self.local_storage._open(name, mode) | |
else: | |
return super()._open(name, mode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment