-
Notifications
You must be signed in to change notification settings - Fork 1.8k
FileUploads: Clean up media when related objects are deleted
#13028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c977d63
FileUploads: Clean up `media` when related objects are deleted
Maffooch 3841442
Update dojo/db_migrations/0241_file_upload_cleanup.py
Maffooch d27106f
Handle case where the UI will attempt to delete files as well
Maffooch 58c1b81
Merge branch 'file-upload-cleanup' of github.com:DefectDojo/django-De…
Maffooch 3c15fd7
Merge branch 'dev' into file-upload-cleanup
Maffooch 7c06f5c
Update migrations
Maffooch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| from django.db import migrations | ||
| from django.db.models import Q | ||
|
|
||
|
|
||
| def delete_unreferenced_file_uploads(apps, schema_editor): | ||
| FileUpload = apps.get_model("dojo", "FileUpload") | ||
| # Filter files that have no relations to Finding, Test, or Engagement | ||
| unused_files = FileUpload.objects.filter( | ||
| Q(finding__isnull=True) & | ||
| Q(test__isnull=True) & | ||
| Q(engagement__isnull=True) | ||
| ).distinct() | ||
| # Delete the files from disk first, then delete the FileUpload object | ||
| for file_upload in unused_files: | ||
| if file_upload.file: | ||
| storage = file_upload.file.storage | ||
| path = file_upload.file.path | ||
| if path and storage.exists(path): | ||
| storage.delete(path) | ||
| file_upload.delete() | ||
|
|
||
|
|
||
| def cannot_turn_back_time(apps, schema_editor): | ||
Maffooch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| We cannot possibly return to the original state without knowing | ||
| the original value at the time the migration is revoked. Instead | ||
| we will do nothing. | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("dojo", "0240_jira_instance_password_help_text_fix"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(delete_unreferenced_file_uploads, cannot_turn_back_time), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import logging | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def delete_related_files(obj): | ||
| if not hasattr(obj, "files"): | ||
| logger.warning(f"Attempted to delete files from object type {type(obj)} without 'files' attribute.") | ||
| return | ||
| logger.debug(f"Deleting {obj.files.count()} files for {type(obj).__name__} {obj.id}") | ||
| obj.files.all().delete() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from django.db.models.signals import post_delete, pre_save | ||
| from django.dispatch import receiver | ||
|
|
||
| from dojo.models import FileUpload | ||
|
|
||
|
|
||
| @receiver(post_delete, sender=FileUpload) | ||
| def delete_file_on_object_delete(sender, instance, **kwargs): | ||
| """ | ||
| Deletes file from filesystem when corresponding `FileUpload` is deleted. | ||
| Mostly used as a backup in case the FileUpload.delete() function fails | ||
| for whatever reason | ||
| """ | ||
| if instance.file: | ||
| storage = instance.file.storage | ||
| path = instance.file.path | ||
| if path and storage.exists(path): | ||
| storage.delete(path) | ||
|
|
||
|
|
||
| @receiver(pre_save, sender=FileUpload) | ||
| def delete_old_file_on_change(sender, instance, **kwargs): | ||
| """Deletes old file when a new file is uploaded to the same record.""" | ||
| if not instance.pk: | ||
| return # Skip new objects | ||
| try: | ||
| old_file = FileUpload.objects.get(pk=instance.pk).file | ||
| except FileUpload.DoesNotExist: | ||
| return | ||
| new_file = instance.file | ||
| if old_file and old_file != new_file: | ||
| storage = old_file.storage | ||
| path = old_file.path | ||
| if path and storage.exists(path): | ||
| storage.delete(path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.