mod: django-filer настройка (06) преобразование в webp
This commit is contained in:
@@ -40,8 +40,8 @@ class CustomFilerConfig(AppConfig):
|
||||
base_path = randomized(instance, filename)
|
||||
return f'flrm/{base_path}'
|
||||
|
||||
class WebPConverter:
|
||||
def convert_to_webp_if_needed(self, name: str, content):
|
||||
@staticmethod
|
||||
def _convert_to_webp_if_needed(name: str, content):
|
||||
_, original_ext = os.path.splitext(name)
|
||||
if original_ext.lower() in [".jpg", ".jpeg", ".png", ".bmp", ".tiff"]:
|
||||
try:
|
||||
@@ -67,10 +67,9 @@ class CustomFilerConfig(AppConfig):
|
||||
|
||||
logger.info("Patching MultiStorageFieldFile.save() for WebP conversion...")
|
||||
original_save = MultiStorageFieldFile.save
|
||||
webp_converter = self.WebPConverter()
|
||||
|
||||
def patched_save(self_instance, name, content, save=True):
|
||||
new_content, new_name, converted = webp_converter.convert_to_webp_if_needed(name, content)
|
||||
new_content, new_name, converted = CustomFilerConfig._convert_to_webp_if_needed(name, content)
|
||||
if converted:
|
||||
self_instance.instance.mime_type = "image/webp"
|
||||
if hasattr(self_instance.instance, 'original_filename') and self_instance.instance.original_filename:
|
||||
|
||||
Reference in New Issue
Block a user