diff --git a/tubesync/sync/migrations/0021_add_delete_files_on_disk.py b/tubesync/sync/migrations/0021_add_delete_files_on_disk.py new file mode 100644 index 0000000..5745d47 --- /dev/null +++ b/tubesync/sync/migrations/0021_add_delete_files_on_disk.py @@ -0,0 +1,17 @@ +# Generated by pac + +from django.db import migrations, models + +class Migration(migrations.Migration): + + dependencies = [ + ('sync', '0020_auto_20231024_1825'), + ] + + operations = [ + migrations.AddField( + model_name='source', + name='delete_files_on_disk', + field=models.BooleanField(default=False, help_text='Delete files on disk when they are removed from TubeSync', verbose_name='delete files on disk'), + ), + ] \ No newline at end of file diff --git a/tubesync/sync/models.py b/tubesync/sync/models.py index 356e779..d9cb8a5 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -296,6 +296,11 @@ class Source(models.Model): default=False, help_text=_('Delete media that is no longer on this playlist') ) + delete_files_on_disk = models.BooleanField( + _('delete files on disk'), + default=False, + help_text=_('Delete files on disk when they are removed from TubeSync') + ) source_resolution = models.CharField( _('source resolution'), max_length=8, diff --git a/tubesync/sync/signals.py b/tubesync/sync/signals.py index b92390e..b9f8835 100644 --- a/tubesync/sync/signals.py +++ b/tubesync/sync/signals.py @@ -1,4 +1,5 @@ import os +import glob from django.conf import settings from django.db.models.signals import pre_save, post_save, pre_delete, post_delete from django.dispatch import receiver @@ -74,6 +75,7 @@ def source_pre_delete(sender, instance, **kwargs): media.delete() + @receiver(post_delete, sender=Source) def source_post_delete(sender, instance, **kwargs): # Triggered after a source is deleted @@ -222,6 +224,16 @@ def media_pre_delete(sender, instance, **kwargs): if thumbnail_url: delete_task_by_media('sync.tasks.download_media_thumbnail', (str(instance.pk), thumbnail_url)) + if instance.source.delete_files_on_disk and (instance.media_file or instance.thumb): + # Delete all media files if it contains filename + filepath = instance.media_file.path if instance.media_file else instance.thumb.path + barefilepath, fileext = os.path.splitext(filepath) + # Get all files that start with the bare file path + all_related_files = glob.glob(f'{barefilepath}.*') + for file in all_related_files: + log.info(f'Deleting file for: {instance} path: {file}') + delete_file(file) + @receiver(post_delete, sender=Media) diff --git a/tubesync/sync/templates/sync/source-delete.html b/tubesync/sync/templates/sync/source-delete.html index bdc9520..ff4ef3b 100644 --- a/tubesync/sync/templates/sync/source-delete.html +++ b/tubesync/sync/templates/sync/source-delete.html @@ -9,8 +9,8 @@
Are you sure you want to delete this source? Deleting a source is permanent. By default, deleting a source does not delete any saved media files. You can - tick the "also delete downloaded media" checkbox to also remove save - media when you delete the source. Deleting a source cannot be undone. + tick the "also delete downloaded media" checkbox to also remove directory {{ source.directory_path }} + when you delete the source. Deleting a source cannot be undone.
diff --git a/tubesync/sync/templates/sync/source.html b/tubesync/sync/templates/sync/source.html index c5812b2..12b083e 100644 --- a/tubesync/sync/templates/sync/source.html +++ b/tubesync/sync/templates/sync/source.html @@ -122,6 +122,10 @@