mirror of
https://github.com/Dispatcharr/Dispatcharr.git
synced 2026-01-23 02:35:14 +00:00
Delete cached files when deleting epg account.
This commit is contained in:
parent
5d2c604a4a
commit
45239b744c
2 changed files with 57 additions and 0 deletions
|
|
@ -3,8 +3,10 @@ from django.dispatch import receiver
|
|||
from .models import EPGSource
|
||||
from .tasks import refresh_epg_data, delete_epg_refresh_task_by_id
|
||||
from django_celery_beat.models import PeriodicTask, IntervalSchedule
|
||||
from core.utils import is_protected_path
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -95,3 +97,31 @@ def update_status_on_active_change(sender, instance, **kwargs):
|
|||
except EPGSource.DoesNotExist:
|
||||
# New record, will use default status
|
||||
pass
|
||||
|
||||
@receiver(post_delete, sender=EPGSource)
|
||||
def delete_cached_files(sender, instance, **kwargs):
|
||||
"""
|
||||
Delete cached files associated with an EPGSource when it's deleted.
|
||||
Only deletes files that aren't in protected directories.
|
||||
"""
|
||||
# Check and delete the main file path if not protected
|
||||
if instance.file_path and os.path.exists(instance.file_path):
|
||||
if is_protected_path(instance.file_path):
|
||||
logger.info(f"Skipping deletion of protected file: {instance.file_path}")
|
||||
else:
|
||||
try:
|
||||
os.remove(instance.file_path)
|
||||
logger.info(f"Deleted cached file: {instance.file_path}")
|
||||
except OSError as e:
|
||||
logger.error(f"Error deleting cached file {instance.file_path}: {e}")
|
||||
|
||||
# Check and delete the extracted file path if it exists, is different from main path, and not protected
|
||||
if instance.extracted_file_path and os.path.exists(instance.extracted_file_path) and instance.extracted_file_path != instance.file_path:
|
||||
if is_protected_path(instance.extracted_file_path):
|
||||
logger.info(f"Skipping deletion of protected extracted file: {instance.extracted_file_path}")
|
||||
else:
|
||||
try:
|
||||
os.remove(instance.extracted_file_path)
|
||||
logger.info(f"Deleted extracted file: {instance.extracted_file_path}")
|
||||
except OSError as e:
|
||||
logger.error(f"Error deleting extracted file {instance.extracted_file_path}: {e}")
|
||||
|
|
|
|||
|
|
@ -303,3 +303,30 @@ def cleanup_memory(log_usage=False, force_collection=True):
|
|||
except (ImportError, Exception):
|
||||
pass
|
||||
logger.trace("Memory cleanup complete for django")
|
||||
|
||||
def is_protected_path(file_path):
|
||||
"""
|
||||
Determine if a file path is in a protected directory that shouldn't be deleted.
|
||||
|
||||
Args:
|
||||
file_path (str): The file path to check
|
||||
|
||||
Returns:
|
||||
bool: True if the path is protected, False otherwise
|
||||
"""
|
||||
if not file_path:
|
||||
return False
|
||||
|
||||
# List of protected directory prefixes
|
||||
protected_dirs = [
|
||||
'/data/epgs', # EPG files mapped from host
|
||||
'/data/uploads', # User uploaded files
|
||||
'/data/m3us' # M3U files mapped from host
|
||||
]
|
||||
|
||||
# Check if the path starts with any protected directory
|
||||
for protected_dir in protected_dirs:
|
||||
if file_path.startswith(protected_dir):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue