From 43cf5329039d435780f502dc9226a2b9c00f3335 Mon Sep 17 00:00:00 2001 From: sparklesmcfadden Date: Sat, 21 Oct 2023 20:54:21 -0500 Subject: [PATCH 1/6] Adds option to delete files on disk when removing a media item from TubeSync --- .../migrations/0020_add_delete_files_on_disk.py | 17 +++++++++++++++++ tubesync/sync/models.py | 5 +++++ tubesync/sync/signals.py | 14 ++++++++++++++ tubesync/sync/templates/sync/source.html | 4 ++++ tubesync/sync/views.py | 6 +++--- 5 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tubesync/sync/migrations/0020_add_delete_files_on_disk.py diff --git a/tubesync/sync/migrations/0020_add_delete_files_on_disk.py b/tubesync/sync/migrations/0020_add_delete_files_on_disk.py new file mode 100644 index 0000000..4d33148 --- /dev/null +++ b/tubesync/sync/migrations/0020_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', '0019_add_delete_removed_media'), + ] + + 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 bb8c723..05ca6fc 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -292,6 +292,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 f27b452..0c7a7a3 100644 --- a/tubesync/sync/signals.py +++ b/tubesync/sync/signals.py @@ -203,6 +203,20 @@ 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: + if instance.thumb: + log.info(f'Deleting thumbnail for: {instance} path: {instance.thumb.path}') + delete_file(instance.thumb.path) + # Delete the media file if it exists + if instance.media_file: + filepath = instance.media_file.path + log.info(f'Deleting media for: {instance} path: {filepath}') + delete_file(filepath) + # Delete thumbnail copy if it exists + barefilepath, fileext = os.path.splitext(filepath) + thumbpath = f'{barefilepath}.jpg' + log.info(f'Deleting thumbnail for: {instance} path: {thumbpath}') + delete_file(thumbpath) @receiver(post_delete, sender=Media) diff --git a/tubesync/sync/templates/sync/source.html b/tubesync/sync/templates/sync/source.html index 22122e2..c2a2cd4 100644 --- a/tubesync/sync/templates/sync/source.html +++ b/tubesync/sync/templates/sync/source.html @@ -118,6 +118,10 @@ Delete removed media Delete removed media
{% if source.delete_removed_media %}{% else %}{% endif %} + + + Delete files on disk + Delete files on disk
{% if source.delete_files_on_disk %}{% else %}{% endif %} {% if source.delete_old_media and source.days_to_keep > 0 %} diff --git a/tubesync/sync/views.py b/tubesync/sync/views.py index e187cd8..f747a37 100644 --- a/tubesync/sync/views.py +++ b/tubesync/sync/views.py @@ -296,9 +296,9 @@ class EditSourceMixin: model = Source fields = ('source_type', 'key', 'name', 'directory', 'media_format', 'index_schedule', 'download_media', 'download_cap', 'delete_old_media', - 'delete_removed_media', 'days_to_keep', 'source_resolution', 'source_vcodec', - 'source_acodec', 'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', - 'write_nfo', 'write_json', 'embed_metadata', 'embed_thumbnail', + 'delete_removed_media', 'delete_files_on_disk', 'days_to_keep', 'source_resolution', + 'source_vcodec', 'source_acodec', 'prefer_60fps', 'prefer_hdr', 'fallback', + 'copy_thumbnails', 'write_nfo', 'write_json', 'embed_metadata', 'embed_thumbnail', 'enable_sponsorblock', 'sponsorblock_categories', 'write_subtitles', 'auto_subtitles', 'sub_langs') errors = { From 46a43b968acb74964209a5417854e9fc11711556 Mon Sep 17 00:00:00 2001 From: administrator <7dn1yh5j@debauchez.fr> Date: Mon, 11 Dec 2023 02:29:57 +0100 Subject: [PATCH 2/6] Rework delete method to delete all files matching filename Remove Source folder if checkbox 'remove media' is checked --- ...sk.py => 0021_add_delete_files_on_disk.py} | 4 +-- tubesync/sync/signals.py | 26 ++++++++-------- .../sync/templates/sync/source-delete.html | 4 +-- tubesync/sync/views.py | 30 ++++++++++--------- 4 files changed, 32 insertions(+), 32 deletions(-) rename tubesync/sync/migrations/{0020_add_delete_files_on_disk.py => 0021_add_delete_files_on_disk.py} (84%) diff --git a/tubesync/sync/migrations/0020_add_delete_files_on_disk.py b/tubesync/sync/migrations/0021_add_delete_files_on_disk.py similarity index 84% rename from tubesync/sync/migrations/0020_add_delete_files_on_disk.py rename to tubesync/sync/migrations/0021_add_delete_files_on_disk.py index 4d33148..5745d47 100644 --- a/tubesync/sync/migrations/0020_add_delete_files_on_disk.py +++ b/tubesync/sync/migrations/0021_add_delete_files_on_disk.py @@ -1,11 +1,11 @@ - Generated by pac +# Generated by pac from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ - ('sync', '0019_add_delete_removed_media'), + ('sync', '0020_auto_20231024_1825'), ] operations = [ diff --git a/tubesync/sync/signals.py b/tubesync/sync/signals.py index 3d60424..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,20 +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: - if instance.thumb: - log.info(f'Deleting thumbnail for: {instance} path: {instance.thumb.path}') - delete_file(instance.thumb.path) - # Delete the media file if it exists - if instance.media_file: - filepath = instance.media_file.path - log.info(f'Deleting media for: {instance} path: {filepath}') - delete_file(filepath) - # Delete thumbnail copy if it exists - barefilepath, fileext = os.path.splitext(filepath) - thumbpath = f'{barefilepath}.jpg' - log.info(f'Deleting thumbnail for: {instance} path: {thumbpath}') - delete_file(thumbpath) + 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/views.py b/tubesync/sync/views.py index 1eb5423..ac65d5c 100644 --- a/tubesync/sync/views.py +++ b/tubesync/sync/views.py @@ -1,7 +1,9 @@ +import glob import os import json from base64 import b64decode import pathlib +import shutil import sys from django.conf import settings from django.http import FileResponse, Http404, HttpResponseNotFound, HttpResponseRedirect @@ -435,14 +437,13 @@ class DeleteSourceView(DeleteView, FormMixin): source = self.get_object() for media in Media.objects.filter(source=source): if media.media_file: - # Delete the media file - delete_file(media.media_file.path) - # Delete thumbnail copy if it exists - delete_file(media.thumbpath) - # Delete NFO file if it exists - delete_file(media.nfopath) - # Delete JSON file if it exists - delete_file(media.jsonpath) + file_path = media.media_file.path + matching_files = glob.glob(os.path.splitext(file_path)[0] + '.*') + for file in matching_files: + delete_file(file) + directory_path = source.directory_path + if os.path.exists(directory_path): + shutil.rmtree(directory_path, True) return super().post(request, *args, **kwargs) def get_success_url(self): @@ -653,12 +654,13 @@ class MediaSkipView(FormView, SingleObjectMixin): delete_task_by_media('sync.tasks.download_media', (str(self.object.pk),)) # If the media file exists on disk, delete it if self.object.media_file_exists: - delete_file(self.object.media_file.path) - self.object.media_file = None - # If the media has an associated thumbnail copied, also delete it - delete_file(self.object.thumbpath) - # If the media has an associated NFO file with it, also delete it - delete_file(self.object.nfopath) + # Delete all files which contains filename + filepath = self.object.media_file.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: + delete_file(file) # Reset all download data self.object.metadata = None self.object.downloaded = False From 46ba2593a2d7d756676d6e57e0924326ee1bb74e Mon Sep 17 00:00:00 2001 From: administrator <7dn1yh5j@debauchez.fr> Date: Mon, 11 Dec 2023 03:19:43 +0100 Subject: [PATCH 3/6] Removed non-pertinent source option --- .../migrations/0021_add_delete_files_on_disk.py | 17 ----------------- tubesync/sync/models.py | 5 ----- tubesync/sync/signals.py | 2 +- tubesync/sync/views.py | 2 +- 4 files changed, 2 insertions(+), 24 deletions(-) delete mode 100644 tubesync/sync/migrations/0021_add_delete_files_on_disk.py diff --git a/tubesync/sync/migrations/0021_add_delete_files_on_disk.py b/tubesync/sync/migrations/0021_add_delete_files_on_disk.py deleted file mode 100644 index 5745d47..0000000 --- a/tubesync/sync/migrations/0021_add_delete_files_on_disk.py +++ /dev/null @@ -1,17 +0,0 @@ -# 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 a63c57e..dff8063 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -300,11 +300,6 @@ 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 b9f8835..6600b71 100644 --- a/tubesync/sync/signals.py +++ b/tubesync/sync/signals.py @@ -224,7 +224,7 @@ 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): + if 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) diff --git a/tubesync/sync/views.py b/tubesync/sync/views.py index ac65d5c..ba319c0 100644 --- a/tubesync/sync/views.py +++ b/tubesync/sync/views.py @@ -298,7 +298,7 @@ class EditSourceMixin: model = Source fields = ('source_type', 'key', 'name', 'directory', 'filter_text', 'media_format', 'index_schedule', 'download_media', 'download_cap', 'delete_old_media', - 'delete_removed_media', 'delete_files_on_disk', 'days_to_keep', 'source_resolution', + 'delete_removed_media', 'days_to_keep', 'source_resolution', 'source_vcodec', 'source_acodec', 'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo', 'write_json', 'embed_metadata', 'embed_thumbnail', 'enable_sponsorblock', 'sponsorblock_categories', 'write_subtitles', From ad1d49a8359e9bfc70eeabeea72803c18e4a71bb Mon Sep 17 00:00:00 2001 From: administrator <7dn1yh5j@debauchez.fr> Date: Mon, 11 Dec 2023 03:25:17 +0100 Subject: [PATCH 4/6] Removed non-pertinent source option --- tubesync/sync/templates/sync/source.html | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tubesync/sync/templates/sync/source.html b/tubesync/sync/templates/sync/source.html index 12b083e..b950838 100644 --- a/tubesync/sync/templates/sync/source.html +++ b/tubesync/sync/templates/sync/source.html @@ -122,11 +122,6 @@ Delete removed media Delete removed media
{% if source.delete_removed_media %}{% else %}{% endif %} - - - Delete files on disk - Delete files on disk
{% if source.delete_files_on_disk %}{% else %}{% endif %} - {% if source.delete_old_media and source.days_to_keep > 0 %} Delete old media From af94b37ee62c17adade34be2633dd2484a31455d Mon Sep 17 00:00:00 2001 From: administrator <7dn1yh5j@debauchez.fr> Date: Mon, 11 Dec 2023 03:38:14 +0100 Subject: [PATCH 5/6] Revert "Removed non-pertinent source option" This reverts commit ad1d49a8359e9bfc70eeabeea72803c18e4a71bb. t cherry-pick 46a43b9 Restore option --- tubesync/sync/templates/sync/source.html | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tubesync/sync/templates/sync/source.html b/tubesync/sync/templates/sync/source.html index b950838..12b083e 100644 --- a/tubesync/sync/templates/sync/source.html +++ b/tubesync/sync/templates/sync/source.html @@ -122,6 +122,11 @@ Delete removed media Delete removed media
{% if source.delete_removed_media %}{% else %}{% endif %} + + + Delete files on disk + Delete files on disk
{% if source.delete_files_on_disk %}{% else %}{% endif %} + {% if source.delete_old_media and source.days_to_keep > 0 %} Delete old media From ed07073cf431b7de57c289873867ea0cda9bdad0 Mon Sep 17 00:00:00 2001 From: administrator <7dn1yh5j@debauchez.fr> Date: Mon, 11 Dec 2023 03:38:37 +0100 Subject: [PATCH 6/6] Revert "Removed non-pertinent source option" This reverts commit 46ba2593a2d7d756676d6e57e0924326ee1bb74e. Restore option --- .../migrations/0021_add_delete_files_on_disk.py | 17 +++++++++++++++++ tubesync/sync/models.py | 5 +++++ tubesync/sync/signals.py | 2 +- tubesync/sync/views.py | 2 +- 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tubesync/sync/migrations/0021_add_delete_files_on_disk.py 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 dff8063..a63c57e 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -300,6 +300,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 6600b71..b9f8835 100644 --- a/tubesync/sync/signals.py +++ b/tubesync/sync/signals.py @@ -224,7 +224,7 @@ 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.media_file or instance.thumb: + 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) diff --git a/tubesync/sync/views.py b/tubesync/sync/views.py index ba319c0..ac65d5c 100644 --- a/tubesync/sync/views.py +++ b/tubesync/sync/views.py @@ -298,7 +298,7 @@ class EditSourceMixin: model = Source fields = ('source_type', 'key', 'name', 'directory', 'filter_text', 'media_format', 'index_schedule', 'download_media', 'download_cap', 'delete_old_media', - 'delete_removed_media', 'days_to_keep', 'source_resolution', + 'delete_removed_media', 'delete_files_on_disk', 'days_to_keep', 'source_resolution', 'source_vcodec', 'source_acodec', 'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo', 'write_json', 'embed_metadata', 'embed_thumbnail', 'enable_sponsorblock', 'sponsorblock_categories', 'write_subtitles',