Skip to content

Commit

Permalink
Validate attachment name before trying to delete it
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed Oct 4, 2023
1 parent 299fe25 commit 84a81ed
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion onadata/apps/logger/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ def pre_delete_attachment(instance, **kwargs):
attachment_storage_bytes=F('attachment_storage_bytes') - file_size
)

if not (media_file_name := str(attachment.media_file)):
return

# Clean-up storage
try:
# We do not want to call `attachment.media_file.delete()` because it calls
# `attachment.save()` behind the scene which would call again the `post_save`
# signal below. Bonus: it avoids an extra query 😎.
default_storage.delete(str(attachment.media_file))
default_storage.delete(media_file_name)
except Exception as e:
logging.error('Failed to delete attachment: ' + str(e), exc_info=True)

Expand Down

0 comments on commit 84a81ed

Please sign in to comment.