From ac72eeac2624703c18fc1b512db431a659bbab5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com> Date: Wed, 27 Oct 2021 15:20:31 +0200 Subject: [PATCH] replace links wp-uploads --- .../website/imported/with_rich_text.rb | 16 ++++++++++----- app/views/active_storage/blobs/_blob.html.erb | 20 ++++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/models/communication/website/imported/with_rich_text.rb b/app/models/communication/website/imported/with_rich_text.rb index a1ac94cda..711138344 100644 --- a/app/models/communication/website/imported/with_rich_text.rb +++ b/app/models/communication/website/imported/with_rich_text.rb @@ -5,16 +5,22 @@ module Communication::Website::Imported::WithRichText def rich_text_with_attachments(text) fragment = Nokogiri::HTML.fragment(text) - images = fragment.css("img[src*=\"#{website.website.uploads_url}\"]") - images.each do |image| + fragment = replace_tags_with_attachments(fragment, 'a', 'href') + fragment = replace_tags_with_attachments(fragment, 'img', 'src') + fragment.to_html + end + + def replace_tags_with_attachments(fragment, tag_name, attribute_name) + nodes = fragment.css("#{tag_name}[#{attribute_name}*=\"#{website.website.uploads_url}\"]") + nodes.each do |node| begin - url = image.attr('src') + url = node.attr(attribute_name) blob = load_blob_from_url(url) - image.replace ActionText::Attachment.from_attachable(blob).node.to_s + node.replace ActionText::Attachment.from_attachable(blob).node.to_s rescue end end - fragment.to_html + fragment end def load_blob_from_url(url) diff --git a/app/views/active_storage/blobs/_blob.html.erb b/app/views/active_storage/blobs/_blob.html.erb index 3daa58bc6..6ad1c8ba6 100644 --- a/app/views/active_storage/blobs/_blob.html.erb +++ b/app/views/active_storage/blobs/_blob.html.erb @@ -1,11 +1,21 @@ <figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>"> - <% if blob.representable? %> + <% if blob.image? %> <%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %> + <% elsif blob.video? %> + <video> + <source src="<%= rails_blob_path(blob) %>" type="<%= blob.content_type %>"> + </video> <% else %> - <p> - <span class="attachment__name"><%= blob.filename %></span> - <span class="attachment__size"><%= number_to_human_size blob.byte_size %></span> - </p> + <%= link_to blob, target: :blank do %> + <% if blob.representable? %> + <%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %> + <% else %> + <p> + <span class="attachment__name"><%= blob.filename %></span> + <span class="attachment__size"><%= number_to_human_size blob.byte_size %></span> + </p> + <% end %> + <% end %> <% end %> <% if caption = blob.try(:caption) %> -- GitLab