From aa36cace7517b65e57f9afdba3ea6f9a1a0c05e9 Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 13 Jul 2024 15:48:16 +0800 Subject: [PATCH] Fix #509. Allow semi easy override of filtering logic with custom python code so it can be extended by end users --- tubesync/sync/filtering.py | 5 +++++ tubesync/sync/overrides/custom_filter.py | 27 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tubesync/sync/overrides/custom_filter.py diff --git a/tubesync/sync/filtering.py b/tubesync/sync/filtering.py index 2e1eb1eb..730a5d88 100644 --- a/tubesync/sync/filtering.py +++ b/tubesync/sync/filtering.py @@ -6,6 +6,7 @@ from common.logger import log from .models import Media from datetime import datetime, timedelta from django.utils import timezone +from .overrides.custom_filter import filter_custom # Check the filter conditions for instance, return is if the Skip property has changed so we can do other things @@ -33,6 +34,10 @@ def filter_media(instance: Media): if filter_duration(instance): skip = True + # If we aren't already skipping the file, call our custom function that can be overridden + if not skip and filter_custom(instance): + skip = True + # Check if skipping if instance.skip != skip: instance.skip = skip diff --git a/tubesync/sync/overrides/custom_filter.py b/tubesync/sync/overrides/custom_filter.py new file mode 100644 index 00000000..c7c22130 --- /dev/null +++ b/tubesync/sync/overrides/custom_filter.py @@ -0,0 +1,27 @@ +""" + This file can be overridden with a docker volume to allow specifying a custom filter function to call. This allows + for higher order filtering for those that really want advanced controls, without exposing the web interface to + potential RCE issues. + + You are simply provided with an instance of Media, and need to return True to skip it, or False to allow it to be + downloaded. + + To use this custom file, download this file and modify the function to do your check for skipping a media item. + Then use docker volumes to override /app/sync/overrides/ with your custom file (it must be called + `custom_filter.py`) + e.g. your `docker run` could have `-v /some/directory/tubesync-overrides:/app/sync/overrides` + or docker-compose could have + volumes: + - /some/directory/tubesync-overrides:/app/sync/overrides + + + The logic is that if any condition marks an item to be skipped, it will be skipped. To save resources, this + custom filter won't be called if any other filter as already marked it to be skipped +""" + +from ..models import Media + + +def filter_custom(instance: Media) -> bool: + # Return True to skip, or False to allow the media item to be downloaded + return False