Skip to content
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

Delete image from storage when removing image within the widget #420

Open
Marchamp opened this issue Sep 5, 2020 · 3 comments
Open

Delete image from storage when removing image within the widget #420

Marchamp opened this issue Sep 5, 2020 · 3 comments

Comments

@Marchamp
Copy link

Marchamp commented Sep 5, 2020

I was wondering if it is possible to delete images from storage when they are removed within the summernote widget.
Whenever an image is dragged into the widget, it would automatically be uploaded even before 'save'.

I tried using django-cleanup but it only works on FileField and ImageField.

Is 'delete from storage' feature built into django-summernote?
If so, how may I proceed?

I guess workaround solution would be to upload images as ImageField and use the image in the widget as img src url after upload.
Thanks.

@Alexhaoge
Copy link

Alexhaoge commented Feb 2, 2021

I wish this feature be added too.
This is very helpful when a lot of objects with rich-text are added and removed frequently. If the image cannot be deleted with field in cascade, many unused files will remain on the server.
The difficulty of this feature is, currently SummernoteTextField is just a models.TextField with html code and images are display with url, no additional information about images is recorded outside the rich-text.

My tentative suggestion of 2 solution:

  1. Create a totally self-defined field extends from Field rather than TextField or CharField
  2. Append the file id or url to the end of the html code (one file per line) and making it a kind of new "format". Strip those when display

@andreccorrea
Copy link

That would be a cool feature to have.

@painkillergodsewostyanow
Copy link

painkillergodsewostyanow commented Aug 12, 2023

i'm do it by that demo way

import os

from django.dispatch import receiver
from django.db.models.signals import post_delete

from mindkeeper.settings import MEDIA_ROOT, MEDIA_URL
from .models import Cards


@receiver(post_delete, sender=Cards)
def on_delete(sender, **kwargs):
    images = []
    html_str = kwargs['instance'].content

    while "img" in html_str:
        left_enter = html_str.find('<img')
        html_str = html_str[left_enter:]
        right_enter = html_str.find('>') + 1
        images.append(html_str[:right_enter])
        html_str = html_str[right_enter:]

    for image in images:
        left_enter = image.find(MEDIA_URL)
        image = image[left_enter + len(MEDIA_URL):]
        right_enter = image.find('"')
        path = os.path.join(MEDIA_ROOT / image[:right_enter])
        os.remove(os.path.join(path))

where Cards it's something like post in blog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants