diff --git a/app/models/communication/website/imported/page.rb b/app/models/communication/website/imported/page.rb index a60d45c9a329cd512c670000f98d6662d63cb30f..72565fbd8ed1a58308219d29830f0819ef0e7b3f 100644 --- a/app/models/communication/website/imported/page.rb +++ b/app/models/communication/website/imported/page.rb @@ -49,7 +49,7 @@ class Communication::Website::Imported::Page < ApplicationRecord optional: true before_validation :sync - after_commit :sync_attachments, on: [:create, :update] + after_commit :sync_featured_image, on: [:create, :update] default_scope { order(:path) } @@ -102,36 +102,45 @@ class Communication::Website::Imported::Page < ApplicationRecord chapter.save end - def sync_attachments + def sync_featured_image return unless ENV['APPLICATION_ENV'] == 'development' || updated_at > page.updated_at - chapter = page.blocks.where(university: website.university, template_kind: :chapter).first_or_create - chapter_data = chapter.data.deep_dup if featured_medium.present? - unless featured_medium.file.attached? - featured_medium.load_remote_file! - featured_medium.save - end - 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 - ) + sync_featured_image_from_featured_medium else - fragment = Nokogiri::HTML.fragment(chapter_data['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 - chapter_data['text'] = fragment.to_html - chapter.data = chapter_data - chapter.save - rescue - end + sync_featured_image_from_content + end + end + + def sync_featured_image_from_featured_medium + unless featured_medium.file.attached? + featured_medium.load_remote_file! + featured_medium.save + end + 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 + ) + end + + def sync_featured_image_from_content + chapter = page.blocks.where(university: website.university, template_kind: :chapter).first_or_create + chapter_data = chapter.data.deep_dup + fragment = Nokogiri::HTML.fragment(chapter_data['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 + chapter_data['text'] = fragment.to_html + chapter.data = chapter_data + chapter.save + rescue end end end + end diff --git a/app/models/communication/website/imported/post.rb b/app/models/communication/website/imported/post.rb index d2750d98d51466804074aeee14583419bd8673e1..2316a0783a4f65a465e5089220f24d2ee734e74e 100644 --- a/app/models/communication/website/imported/post.rb +++ b/app/models/communication/website/imported/post.rb @@ -49,7 +49,7 @@ class Communication::Website::Imported::Post < ApplicationRecord optional: true before_validation :sync - after_commit :sync_attachments, on: [:create, :update] + after_commit :sync_featured_image, on: [:create, :update] default_scope { order(path: :desc) } @@ -114,36 +114,46 @@ class Communication::Website::Imported::Post < ApplicationRecord chapter.save end - def sync_attachments + def sync_featured_image return unless ENV['APPLICATION_ENV'] == 'development' || updated_at > post.updated_at - chapter = post.blocks.where(university: website.university, template_kind: :chapter).first_or_create - chapter_data = chapter.data.deep_dup if featured_medium.present? - unless featured_medium.file.attached? - featured_medium.load_remote_file! - featured_medium.save - end - 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 - ) + sync_featured_image_from_featured_medium else - fragment = Nokogiri::HTML.fragment(chapter_data['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 - chapter_data['text'] = fragment.to_html - chapter.data = chapter_data - chapter.save - rescue - end + sync_featured_image_from_content + end + end + + def sync_featured_image_from_featured_medium + unless featured_medium.file.attached? + featured_medium.load_remote_file! + featured_medium.save + end + 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 + ) + end + + def sync_featured_image_from_content + chapter = post.blocks.where(university: website.university, template_kind: :chapter).first_or_create + chapter_data = chapter.data.deep_dup + fragment = Nokogiri::HTML.fragment(chapter_data['text'].to_s) + image = fragment.css('img').first + # If content has an image, we extract it as the featured image + if image.present? + begin + url = image.attr('src') + download_service = DownloadService.download(url) + post.featured_image.attach(download_service.attachable_data) + image.remove + chapter_data['text'] = fragment.to_html + chapter.data = chapter_data + chapter.save + rescue end end end + end