diff --git a/app/models/communication/website/imported/page.rb b/app/models/communication/website/imported/page.rb index 827fa184fca2c20313002e40066dac5e7f06ee89..1ddc8417ae8b4cdd89a711e56da04329e20eaeca 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 9ff448c557eae487a2d2e478666853089c32a004..148aefae3ba7f4348a83a4d58b2681203e6ba828 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