Fix: Auto-cleanup temp files after photo processing
- Added temp file deletion in Celery task after successful processing - Added temp file cleanup in sync fallback method - Added temp file removal in delete() if processing never completed - Prevents accumulation of orphaned files in media/<entity>/temp/ folders
This commit is contained in:
@@ -8,6 +8,7 @@ import logging
|
||||
from celery import shared_task
|
||||
from django.db import connection
|
||||
from django.apps import apps
|
||||
from django.core.files.storage import default_storage
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -53,6 +54,9 @@ def process_product_photo_async(self, photo_id, photo_model_class, schema_name):
|
||||
logger.warning(f"[Celery] Photo {photo_id} has no image file")
|
||||
return {'status': 'error', 'reason': 'no_image'}
|
||||
|
||||
# Сохраняем путь к временному файлу до перезаписи поля image
|
||||
temp_path = photo_obj.image.name
|
||||
|
||||
# Получаем entity type для правильного пути сохранения
|
||||
entity_type = photo_obj.get_entity_type()
|
||||
|
||||
@@ -73,6 +77,14 @@ def process_product_photo_async(self, photo_id, photo_model_class, schema_name):
|
||||
photo_obj.quality_warning = processed_paths.get('quality_warning', False)
|
||||
photo_obj.save(update_fields=['image', 'quality_level', 'quality_warning'])
|
||||
|
||||
# Удаляем временный файл из temp после успешной обработки
|
||||
try:
|
||||
if temp_path and default_storage.exists(temp_path):
|
||||
default_storage.delete(temp_path)
|
||||
logger.info(f"[Celery] Deleted temp file: {temp_path}")
|
||||
except Exception as del_exc:
|
||||
logger.warning(f"[Celery] Could not delete temp file {temp_path}: {del_exc}")
|
||||
|
||||
logger.info(f"[Celery] ✓ Photo {photo_id} processed successfully "
|
||||
f"(quality: {processed_paths.get('quality_level')})")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user