diff --git a/app/models/communication/website/imported/medium.rb b/app/models/communication/website/imported/medium.rb index 8dbd6853a3e245e4d8a89b6edef893ee894bc4e8..80801c768b903857709b2dac731c050a9fb44ee4 100644 --- a/app/models/communication/website/imported/medium.rb +++ b/app/models/communication/website/imported/medium.rb @@ -53,13 +53,7 @@ class Communication::Website::Imported::Medium < ApplicationRecord end def load_remote_file! - uri = URI(file_url) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true - # IUT Bordeaux Montaigne pb with certificate - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Get.new(uri.request_uri) - response = http.request(request) - file.attach(io: StringIO.new(response.body), filename: filename, content_type: mime_type) + download_service = DownloadService.download(file_url) + file.attach(download_service.attachable_data) end end diff --git a/app/models/communication/website/imported/post.rb b/app/models/communication/website/imported/post.rb index 616580fa0ec8628840516b44a66835790291f6d1..ee3b9c086e1104a929e13a2277b5a409ff29cb39 100644 --- a/app/models/communication/website/imported/post.rb +++ b/app/models/communication/website/imported/post.rb @@ -35,6 +35,8 @@ # fk_rails_... (website_id => communication_website_imported_websites.id) # class Communication::Website::Imported::Post < ApplicationRecord + include Communication::Website::Imported::WithRichText + belongs_to :university belongs_to :website, class_name: 'Communication::Website::Imported::Website' @@ -93,28 +95,21 @@ class Communication::Website::Imported::Post < ApplicationRecord post.published = true post.save download_first_image_as_featured_image if featured_medium.nil? + post.update(text: rich_text_with_attachments(post.text.to_s)) end - # Please refactor me i'm ugly def download_first_image_as_featured_image fragment = Nokogiri::HTML.fragment(post.text.to_s) - images = fragment.css('img') - if images.any? - begin - image = images.first - url = image.attr('src') - uri = URI(url) - filename = File.basename url - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Get.new(uri.request_uri) - response = http.request(request) - post.featured_image.attach(io: StringIO.new(response.body), filename: filename, content_type: response['Content-Type']) - image.remove - post.update(text: fragment.to_html) - rescue - end + image = fragment.css('img').first + return unless image.present? + begin + url = image.attr('src') + puts "Set featured image: #{url}" + download_service = DownloadService.download(url) + post.featured_image.attach(download_service.attachable_data) + image.remove + post.update(text: fragment.to_html) + rescue end end end diff --git a/app/models/communication/website/imported/with_rich_text.rb b/app/models/communication/website/imported/with_rich_text.rb new file mode 100644 index 0000000000000000000000000000000000000000..7d770026a7914ad98edea9c1bed487fb70327da9 --- /dev/null +++ b/app/models/communication/website/imported/with_rich_text.rb @@ -0,0 +1,41 @@ +module Communication::Website::Imported::WithRichText + extend ActiveSupport::Concern + + protected + + def rich_text_with_attachments(text) + fragment = Nokogiri::HTML.fragment(text) + images = fragment.css("img[src*=\"#{website.website.domain}\"]") + images.each do |image| + begin + url = image.attr('src') + puts "Replacing #{url}" + blob = load_blob_from_url(url) + image.replace ActionText::Attachment.from_attachable(blob).node.to_s + rescue + end + end + fragment.to_html + end + + def load_blob_from_url(url) + medium = website.media.for_variant_url(url).first + if medium.present? + medium.load_remote_file! unless medium.file.attached? + # Currently a copy, should we link the medium blob instead? + blob = medium.file.blob.open do |tempfile| + ActiveStorage::Blob.create_and_upload!( + io: tempfile, + filename: medium.file.blob.filename, + content_type: medium.file.blob.content_type + ) + end + else + download_service = DownloadService.download(url) + blob = ActiveStorage::Blob.create_and_upload!(download_service.attachable_data) + end + blob.update_column(:university_id, self.university_id) + blob.analyze_later + blob + end +end diff --git a/app/services/download_service.rb b/app/services/download_service.rb new file mode 100644 index 0000000000000000000000000000000000000000..e0469ccfce588827aca48a6ea842a22bc1953497 --- /dev/null +++ b/app/services/download_service.rb @@ -0,0 +1,40 @@ +class DownloadService + attr_reader :response + + def self.download(url) + new(url) + end + + def initialize(url) + @url = url + process! + end + + def attachable_data + { io: io, filename: filename, content_type: content_type } + end + + def io + @io ||= StringIO.new(@response.body) + end + + def filename + @filename ||= File.basename(@url) + end + + def content_type + @content_type ||= @response['Content-Type'] + end + + protected + + def process! + uri = URI(@url) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + + request = Net::HTTP::Get.new(uri.request_uri) + @response = http.request(request) + end +end \ No newline at end of file diff --git a/app/services/wordpress.rb b/app/services/wordpress.rb index f591219fb58a0518988105d47dba7f2067d4b1a4..927d5844416fcf2bfd27f3c986731b6172da6b2d 100644 --- a/app/services/wordpress.rb +++ b/app/services/wordpress.rb @@ -77,13 +77,7 @@ class Wordpress end def load_url(url) - uri = URI(url) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true - # IUT Bordeaux Montaigne pb with certificate - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Get.new(uri.request_uri) - response = http.request(request) - JSON.parse(response.body) + download_service = DownloadService.download(url) + JSON.parse(download_service.response.body) end end diff --git a/app/views/active_storage/blobs/_blob.html.erb b/app/views/active_storage/blobs/_blob.html.erb index 49ba357dd1d4afc63ac0bae78dbcd1a882df9e9e..3daa58bc67b70fe52af1b1f38a6711885bc044bd 100644 --- a/app/views/active_storage/blobs/_blob.html.erb +++ b/app/views/active_storage/blobs/_blob.html.erb @@ -1,14 +1,16 @@ <figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>"> <% 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 %> - <figcaption class="attachment__caption"> - <% if caption = blob.try(:caption) %> + <% if caption = blob.try(:caption) %> + <figcaption class="attachment__caption"> <%= caption %> - <% else %> - <span class="attachment__name"><%= blob.filename %></span> - <span class="attachment__size"><%= number_to_human_size blob.byte_size %></span> - <% end %> - </figcaption> + </figcaption> + <% end %> </figure>