From 220e9d4941e156d72765feb40947bf13a9576162 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Wed, 27 Oct 2021 18:23:03 +0200
Subject: [PATCH] without conern

---
 .../communication/website/imported/page.rb    | 21 ++++++++++++++++---
 .../communication/website/imported/post.rb    | 21 ++++++++++++++++---
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/app/models/communication/website/imported/page.rb b/app/models/communication/website/imported/page.rb
index 827fa184f..1ddc8417a 100644
--- a/app/models/communication/website/imported/page.rb
+++ b/app/models/communication/website/imported/page.rb
@@ -36,7 +36,6 @@
 #  fk_rails_...  (website_id => communication_website_imported_websites.id)
 #
 class Communication::Website::Imported::Page < ApplicationRecord
-  include Communication::Website::Imported::WithFeaturedImage
   include Communication::Website::Imported::WithRichText
 
   belongs_to :university
@@ -94,9 +93,25 @@ class Communication::Website::Imported::Page < ApplicationRecord
     page.published = true
     page.save
     if featured_medium.present?
-      download_featured_medium_file_as_featured_image(page)
+      featured_medium.load_remote_file! unless featured_medium.file.attached?
+      page.featured_image.attach(
+        io: URI.open(featured_medium.file.blob.url),
+        filename: featured_medium.file.blob.filename,
+        content_type: featured_medium.file.blob.content_type
+      )
     else
-      download_first_image_in_text_as_featured_image(page)
+      fragment = Nokogiri::HTML.fragment(page.text.to_s)
+      image = fragment.css('img').first
+      if image.present?
+        begin
+          url = image.attr('src')
+          download_service = DownloadService.download(url)
+          page.featured_image.attach(download_service.attachable_data)
+          image.remove
+          page.update(text: fragment.to_html)
+        rescue
+        end
+      end
     end
     page.update(text: rich_text_with_attachments(page.text.to_s))
   end
diff --git a/app/models/communication/website/imported/post.rb b/app/models/communication/website/imported/post.rb
index 9ff448c55..148aefae3 100644
--- a/app/models/communication/website/imported/post.rb
+++ b/app/models/communication/website/imported/post.rb
@@ -35,7 +35,6 @@
 #  fk_rails_...  (website_id => communication_website_imported_websites.id)
 #
 class Communication::Website::Imported::Post < ApplicationRecord
-  include Communication::Website::Imported::WithFeaturedImage
   include Communication::Website::Imported::WithRichText
 
   belongs_to :university
@@ -96,9 +95,25 @@ class Communication::Website::Imported::Post < ApplicationRecord
     post.published = true
     post.save
     if featured_medium.present?
-      download_featured_medium_file_as_featured_image(post)
+      featured_medium.load_remote_file! unless featured_medium.file.attached?
+      post.featured_image.attach(
+        io: URI.open(featured_medium.file.blob.url),
+        filename: featured_medium.file.blob.filename,
+        content_type: featured_medium.file.blob.content_type
+      )
     else
-      download_first_image_in_text_as_featured_image(post)
+      fragment = Nokogiri::HTML.fragment(post.text.to_s)
+      image = fragment.css('img').first
+      if image.present?
+        begin
+          url = image.attr('src')
+          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
     post.update(text: rich_text_with_attachments(post.text.to_s))
   end
-- 
GitLab