--- /dev/null
+# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+"""Replacing relative addresses of illustrations with absolute URLs."""
+import os.path
+import re
+from urllib.parse import quote, unquote, urljoin
+from django.conf import settings
+
+
+SRC_RE = re.compile(r'''(<ilustr\b[^>]*?\bsrc\s*=\s*)(["'])(.*?)\2''', re.S)
+
+
+def is_absolute(src):
+ return src.startswith(('http://', 'https://'))
+
+
+def absolutize(text, gallery_url, gallery_dir):
+ """Replaces relative `src` of illustrations in text with absolute URLs.
+
+ Only replaces addresses of files which exist in the gallery.
+ Returns the new text, a list of replaced and a list of unresolved addresses.
+ """
+ fixed = []
+ unresolved = []
+
+ def repl(m):
+ src = m.group(3)
+ if is_absolute(src):
+ return m.group(0)
+ path = os.path.normpath(os.path.join(gallery_dir, unquote(src)))
+ if not src or not os.path.isfile(path):
+ unresolved.append(src)
+ return m.group(0)
+ fixed.append(src)
+ url = urljoin(gallery_url, quote(src, safe='/%'))
+ return m.group(1) + m.group(2) + url + m.group(2)
+
+ return SRC_RE.sub(repl, text), fixed, unresolved
+
+
+class IlustrFixer:
+ def __init__(self, base_url, **author):
+ self.images_url = urljoin(base_url, settings.MEDIA_URL + settings.IMAGE_DIR)
+ self.images_dir = os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR)
+ self.author = author
+ self.stats = dict.fromkeys(['books', 'fixed', 'unresolved', 'conflicts'], 0)
+
+ def fix_book(self, book):
+ if not book.gallery:
+ gallery_url = gallery_dir = None
+ else:
+ gallery_url = urljoin(self.images_url, quote(book.gallery) + '/')
+ gallery_dir = os.path.join(self.images_dir, book.gallery)
+
+ for chunk in book.chunk_set.all():
+ head = chunk.head
+ if head is None:
+ continue
+ head_text = head.materialize()
+ if gallery_url is None:
+ unresolved = [m.group(3) for m in SRC_RE.finditer(head_text)
+ if not is_absolute(m.group(3))]
+ if unresolved:
+ self.stats['unresolved'] += len(unresolved)
+ continue
+
+ new_text, fixed, unresolved = absolutize(head_text, gallery_url, gallery_dir)
+ self.stats['fixed'] += len(fixed)
+ self.stats['unresolved'] += len(unresolved)
+
+ if fixed:
+ chunk.commit(
+ new_text, description='Pełne adresy ilustracji.',
+ publishable=head.publishable,
+ **self.author)
valid_about = self.correct_about()
assert bi.about == valid_about, _("rdf:about is not") + " " + valid_about
+ srcs = self.relative_ilustr_srcs(changes=changes)
+ assert not srcs, 'Ilustracje bez pełnego adresu: ' + ', '.join(
+ src or '(pusty)' for src in srcs)
+
+ def relative_ilustr_srcs(self, changes=None):
+ """Returns `src` of illustrations which aren't absolute URLs.
+
+ Those only work relative to the gallery, so they break
+ outside of it. Checks the current text, unless `changes` are given.
+ """
+ from lxml import etree
+ try:
+ tree = etree.fromstring(self.materialize(changes=changes).encode('utf-8'))
+ except (self.NoTextError, etree.XMLSyntaxError):
+ return []
+ return [
+ elem.get('src', '') for elem in tree.iter('ilustr')
+ if not elem.get('src', '').startswith(('http://', 'https://'))
+ ]
+
def publishable_error(self):
try:
return self.assert_publishable()
</nav>
<div class="container mt-4 mb-4">
+ {% if messages %}
+ {% for message in messages %}
+ <div class="alert alert-{% if message.level_tag == 'error' %}danger{% else %}{{ message.level_tag|default:'info' }}{% endif %} alert-dismissible" role="alert">
+ {{ message }}
+ <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
+ </div>
+ {% endfor %}
+ {% endif %}
{% block content %}
<div class="row">
<div class="col-lg-8">
{% endif %}
</p>
+ {% with srcs=book.relative_ilustr_srcs %}
+ {% if srcs %}
+ <form class="alert alert-warning" method="POST" action="{% url 'documents_fix_ilustr' book.slug %}">
+ {% csrf_token %}
+ Ilustracje bez pełnego adresu:
+ {% for src in srcs %}<tt>{{ src|default:"(pusty)" }}</tt>{% if not forloop.last %}, {% endif %}{% endfor %}
+ {% if user.is_authenticated %}
+ <button type="submit" class="btn btn-sm btn-warning">Napraw</button>
+ {% endif %}
+ </form>
+ {% endif %}
+ {% endwith %}
+
{% if publishable %}
<p>
<a href="{% url 'documents_book_xml' book.slug %}" rel="nofollow">{% trans "Full XML" %}</a><br/>
path('create/', views.create_missing, name='documents_create_missing'),
path('book/<slug:slug>/publish', views.publish, name="documents_publish"),
+ path('book/<slug:slug>/fix-ilustr', views.fix_ilustr, name="documents_fix_ilustr"),
path('book/<slug:slug>/', views.book, name="documents_book"),
path('book/<slug:slug>/gallery/',
from urllib.parse import quote_plus, unquote, urlsplit, urlunsplit
from django.conf import settings
-from django.contrib import auth
+from django.contrib import auth, messages
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required, permission_required
from django.urls import reverse
return http.HttpResponseRedirect(book.get_absolute_url())
+@require_POST
+@login_required
+def fix_ilustr(request, slug):
+ from .ilustr import IlustrFixer
+ book = get_object_or_404(Book, slug=slug)
+ protocol = 'https://' if request.is_secure() else 'http://'
+ fixer = IlustrFixer(protocol + request.get_host(), author=request.user)
+ with transaction.atomic():
+ fixer.fix_book(book)
+ if fixer.stats['unresolved'] or fixer.stats['conflicts']:
+ messages.warning(request, 'Nie wszystkie ilustracje udało się naprawić.')
+ elif fixer.stats['fixed']:
+ messages.success(request, 'Naprawiono adresy ilustracji.')
+ return http.HttpResponseRedirect(book.get_absolute_url())
+
+
@require_POST
@login_required
def publish_image(request, slug):
self.doc.refreshImageGallery({
success: function(galleryImages) {
- $.each(self.doc.galleryImages, (i, imgItem) => {
+ $.each(galleryImages, (i, imgItem) => {
let img = $("<img>").attr("src", imgItem.thumb).attr('title', imgItem.url).data('url', imgItem.url).on('click', function() {
imglist.find('img').removeClass('active');
$(this).addClass('active');
font-size: 1rem;
line-height: 1.5;
}
+
+ // Images require absolute src.
+ [x-node="ilustr"]:not([x-a-wl-src^="http://"]):not([x-a-wl-src^="https://"]) {
+ outline: 3px solid #dc3545;
+
+ &::before {
+ content: "Ilustracja bez pełnego adresu";
+ display: block;
+ color: #dc3545;
+ font-family: sans-serif;
+ font-size: .8rem;
+ }
+ }
}