django-summernote
django-summernote copied to clipboard
GCS links expire
Hi folks, I'm using django-summernote with a storage using the unified ACL. The generated links eventually expire so the images end up not being displayable.
To workaround the issue I've written a filter:
from urllib.parse import urlparse
import pathlib
from django import template
from django.core.files.storage import DefaultStorage
from bs4 import BeautifulSoup
from django.conf import settings
from django.template.defaultfilters import stringfilter
register = template.Library()
storage = DefaultStorage()
root = pathlib.Path("/", settings.GS_BUCKET_NAME)
def process_url(url_raw):
url = urlparse(url_raw)
if url.netloc != "storage.googleapis.com":
return url_raw
return storage.url(str(pathlib.Path(url.path).relative_to(root)))
@register.filter("refresh_images")
@stringfilter
def refresh_images(source):
soup = BeautifulSoup(source)
for img in soup.findChildren("img"):
url = img.attrs["src"]
img.attrs["src"] = process_url(url)
return str(soup)
I am not too happy with this solution and was wondering if you had a suggestion?
Cheers,