mod: django-filer настройка (05) преобразование в webp

This commit is contained in:
2026-06-07 21:24:00 +03:00
parent f99bbb5c6b
commit 6a1f923fe4

View File

@@ -1,11 +1,15 @@
import os import os
import hashlib import hashlib
import logging
from io import BytesIO from io import BytesIO
from django.apps import AppConfig from django.apps import AppConfig
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from PIL import Image as PILImage from PIL import Image as PILImage
from lpon_site.settings import DEBUG, THUMBNAIL_WEBP_QUALITY from lpon_site.settings import THUMBNAIL_WEBP_QUALITY
# Получаем логгер для текущего модуля
logger = logging.getLogger(__name__)
class FrontendConfig(AppConfig): class FrontendConfig(AppConfig):
@@ -49,20 +53,19 @@ class CustomFilerConfig(AppConfig):
img.save(buffer, format="WEBP", quality=THUMBNAIL_WEBP_QUALITY) img.save(buffer, format="WEBP", quality=THUMBNAIL_WEBP_QUALITY)
buffer.seek(0) buffer.seek(0)
new_name = name.rsplit(original_ext, 1)[0] + ".webp" new_name = name.rsplit(original_ext, 1)[0] + ".webp"
logger.info(f"Successfully converted '{name}' to '{new_name}' (WebP).")
return ContentFile(buffer.read()), new_name, True return ContentFile(buffer.read()), new_name, True
except Exception as e: except Exception:
import sys logger.error(f"Error converting '{name}' to WebP.", exc_info=True)
print(f"[WebPConverter] ERROR converting {name}: {e}", file=sys.stderr)
content.seek(0) content.seek(0)
return content, name, False return content, name, False
content.seek(0) content.seek(0)
return content, name, False return content, name, False
def ready(self): def ready(self):
import sys
from filer.fields.multistorage_file import MultiStorageFieldFile from filer.fields.multistorage_file import MultiStorageFieldFile
print("[CustomFilerConfig.ready] Patching MultiStorageFieldFile.save()...", file=sys.stderr) logger.info("Patching MultiStorageFieldFile.save() for WebP conversion...")
original_save = MultiStorageFieldFile.save original_save = MultiStorageFieldFile.save
webp_converter = self.WebPConverter() webp_converter = self.WebPConverter()
@@ -81,7 +84,7 @@ class CustomFilerConfig(AppConfig):
return original_save(self_instance, new_name, new_content, save) return original_save(self_instance, new_name, new_content, save)
MultiStorageFieldFile.save = patched_save MultiStorageFieldFile.save = patched_save
print("[CustomFilerConfig.ready] MultiStorageFieldFile.save() patched successfully", file=sys.stderr) logger.info("MultiStorageFieldFile.save() patched successfully.")
# Создаем псевдонимы на уровне модуля для функций, чтобы их мог найти Django # Создаем псевдонимы на уровне модуля для функций, чтобы их мог найти Django
generate_upload_path_flr = CustomFilerConfig.generate_upload_path_flr generate_upload_path_flr = CustomFilerConfig.generate_upload_path_flr