diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb index 84a2eb91ce7c32c8cb416ccadf1aeb93dc4319f8..66cff8d0661bb0171f51bf64488eea16c95d8b35 100644 --- a/app/controllers/admin/communication/websites_controller.rb +++ b/app/controllers/admin/communication/websites_controller.rb @@ -22,8 +22,8 @@ class Admin::Communication::WebsitesController < Admin::Communication::Applicati @imported_website = @website.imported_website @imported_pages = @imported_website.pages.page params[:pages_page] @imported_posts = @imported_website.posts.page params[:posts_page] - @imported_media = @imported_website.media.includes(medium: { file_attachment: :blob }).page params[:media_page] - @imported_media_total_size = @imported_website.media.joins(medium: { file_attachment: :blob }).sum(:byte_size) + @imported_media = @imported_website.media.includes(file_attachment: :blob ).page params[:media_page] + @imported_media_total_size = @imported_website.media.joins(file_attachment: :blob).sum(:byte_size) breadcrumb add_breadcrumb Communication::Website::Imported::Website.model_name.human end diff --git a/app/models/communication/website/imported/medium.rb b/app/models/communication/website/imported/medium.rb index 9211ae7245554c64943b8c7e4770b12e7c4ce01b..4a1347d8c5654a00d7effc573ff85f9bd3f7478b 100644 --- a/app/models/communication/website/imported/medium.rb +++ b/app/models/communication/website/imported/medium.rb @@ -10,19 +10,16 @@ # mime_type :string # created_at :datetime # updated_at :datetime -# medium_id :uuid # university_id :uuid not null # website_id :uuid not null # # Indexes # -# index_communication_website_imported_media_on_medium_id (medium_id) # index_communication_website_imported_media_on_university_id (university_id) # index_communication_website_imported_media_on_website_id (website_id) # # Foreign Keys # -# fk_rails_... (medium_id => communication_website_media.id) # fk_rails_... (university_id => universities.id) # fk_rails_... (website_id => communication_website_imported_websites.id) # @@ -30,9 +27,6 @@ class Communication::Website::Imported::Medium < ApplicationRecord belongs_to :university belongs_to :website, class_name: 'Communication::Website::Imported::Website' - belongs_to :medium, - class_name: 'Communication::Website::Medium', - optional: true has_many :pages, class_name: 'Communication::Website::Imported::Page', foreign_key: :featured_medium_id @@ -40,7 +34,9 @@ class Communication::Website::Imported::Medium < ApplicationRecord class_name: 'Communication::Website::Imported::Post', foreign_key: :featured_medium_id - before_validation :sync + has_one_attached_deletable :file + + after_commit :download_file_from_file_url, on: [:create, :update], if: :saved_change_to_file_url def data=(value) super value @@ -54,23 +50,15 @@ class Communication::Website::Imported::Medium < ApplicationRecord protected - def sync - if medium.nil? - self.medium = Communication::Website::Medium.new university: university, - website: website.website # Real website, not imported website - self.medium.save - else - # Continue only if there are remote changes - # Don't touch if there are local changes (page.updated_at > updated_at) - # Don't touch if there are no remote changes (page.updated_at == updated_at) - return unless updated_at > medium.updated_at - end - puts "Update medium #{medium.id}" - medium.file_url = Addressable::URI.parse(file_url).display_uri.to_s # ASCII-only for ActiveStorage - medium.filename = File.basename(URI(medium.file_url).path) - medium.mime_type = mime_type - medium.created_at = created_at - medium.updated_at = updated_at - medium.save + def download_file_from_file_url + 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) end + handle_asynchronously :download_file_from_file_url, queue: 'default' end diff --git a/app/models/communication/website/imported/post.rb b/app/models/communication/website/imported/post.rb index 16a318248c5bf234cb1731fad5561633805ed045..b390c1503aaa0d0a5aa0cbfaba1870d48198fe92 100644 --- a/app/models/communication/website/imported/post.rb +++ b/app/models/communication/website/imported/post.rb @@ -97,19 +97,22 @@ class Communication::Website::Imported::Post < ApplicationRecord # Please refactor me i'm ugly def download_first_image_as_featured_image - doc = Nokogiri::HTML(post.text.to_s) - images = doc.css('img') + fragment = Nokogiri::HTML.fragment(post.text.to_s) + images = fragment.css('img') if images.any? begin - url = images.first.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: 'image/jpeg') + 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 end diff --git a/app/models/communication/website/imported/website.rb b/app/models/communication/website/imported/website.rb index 6765db2e49018fa64d0bfbf61f9ab91cd8929ea4..4ddbcc00111d2b7f17084ad915344309c4b9625b 100644 --- a/app/models/communication/website/imported/website.rb +++ b/app/models/communication/website/imported/website.rb @@ -69,12 +69,14 @@ class Communication::Website::Imported::Website < ApplicationRecord end # Batch update all changes (1 query only, good for github API limits) github = Github.with_site website - website.pages.find_each do |page| - github.add_to_batch path: page.github_path_generated, - previous_path: page.github_path, - data: page.to_jekyll + if github.valid? + website.pages.find_each do |page| + github.add_to_batch path: page.github_path_generated, + previous_path: page.github_path, + data: page.to_jekyll + end + github.commit_batch '[Page] Batch update from import' end - github.commit_batch '[Page] Batch update from import' Communication::Website::Page.set_callback(:save, :after, :publish_to_github) end @@ -86,11 +88,13 @@ class Communication::Website::Imported::Website < ApplicationRecord post.data = data post.save generated_post = post.post - github.add_to_batch path: generated_post.github_path_generated, - previous_path: generated_post.github_path, - data: generated_post.to_jekyll + if github.valid? + github.add_to_batch path: generated_post.github_path_generated, + previous_path: generated_post.github_path, + data: generated_post.to_jekyll + end end - github.commit_batch '[Post] Batch update from import' + github.commit_batch '[Post] Batch update from import' if github.valid? Communication::Website::Post.set_callback(:save, :after, :publish_to_github) end end diff --git a/app/models/communication/website/medium.rb b/app/models/communication/website/medium.rb deleted file mode 100644 index b9fd23689920c422d9da5264eb6d76743b6f6d2c..0000000000000000000000000000000000000000 --- a/app/models/communication/website/medium.rb +++ /dev/null @@ -1,68 +0,0 @@ -# == Schema Information -# -# Table name: communication_website_media -# -# id :uuid not null, primary key -# file_url :text -# filename :string -# identifier :string -# mime_type :string -# created_at :datetime not null -# updated_at :datetime not null -# university_id :uuid not null -# website_id :uuid not null -# -# Indexes -# -# index_communication_website_media_on_university_id (university_id) -# index_communication_website_media_on_website_id (website_id) -# -# Foreign Keys -# -# fk_rails_... (university_id => universities.id) -# fk_rails_... (website_id => communication_websites.id) -# -class Communication::Website::Medium < ApplicationRecord - belongs_to :university - belongs_to :website - has_one :imported_medium, - class_name: 'Communication::Website::Imported::Medium', - foreign_key: :medium_id, - dependent: :destroy - - has_one_attached_deletable :file - - after_commit :download_file_from_file_url, on: [:create, :update], if: :saved_change_to_file_url - - protected - - def download_file_from_file_url - 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) - tempfile = Tempfile.open("Osuny-CommunicationWebsiteMedium-#{SecureRandom.hex}", Dir.tmpdir) - begin - tempfile.binmode - tempfile.write(response.body) - tempfile.flush - tempfile.rewind - file.attach(io: tempfile, filename: filename, content_type: mime_type) - set_featured_images - ensure - tempfile.close! - end - end - handle_asynchronously :download_file_from_file_url, queue: 'default' - - def set_featured_images - posts = Communication::Website::Post.joins(:imported_post) - .where(communication_website_imported_posts: { featured_medium_id: imported_medium.id }) - posts.each do |post| - post.featured_image.attach(io: URI.open(file.url), filename: filename, content_type: mime_type) - end - end -end diff --git a/app/models/concerns/with_github.rb b/app/models/concerns/with_github.rb index 7aa18027ef16845453541998558a364a90ae4d4a..906ad62e5bb8bcd2a906f45559b80f50e337a3af 100644 --- a/app/models/concerns/with_github.rb +++ b/app/models/concerns/with_github.rb @@ -28,6 +28,7 @@ module WithGithub end def publish_to_github + return unless github.valid? if github.publish(path: github_path_generated, previous_path: github_path, commit: github_commit_message, diff --git a/app/models/research/researcher.rb b/app/models/research/researcher.rb index 3ff523338bc2cb62b8b605e98763ad3412e69f30..ff77867859bf3e087872113b89da2c4f14b4518d 100644 --- a/app/models/research/researcher.rb +++ b/app/models/research/researcher.rb @@ -32,6 +32,7 @@ class Research::Researcher < ApplicationRecord def publish_to_website(website) github = Github.new website.access_token, website.repository + return unless github.valid? github.publish path: "_authors/#{ id }.md", data: to_jekyll, commit: "[Researcher] Save #{to_s}" diff --git a/app/services/github.rb b/app/services/github.rb index dd068ed3f12775948a9e880e9a81c24455a08fa6..e47b9b4ec0403cd6bff32d4daff5d766070e95cf 100644 --- a/app/services/github.rb +++ b/app/services/github.rb @@ -10,6 +10,10 @@ class Github @repository = repository end + def valid? + repository.present? && access_token.present? + end + def publish(path: nil, previous_path: nil, commit: nil, diff --git a/app/services/wordpress.rb b/app/services/wordpress.rb index d1f21571d7b20a2b4aa23ff22b30c375cfaf79d9..a34aaf0522cc0c82e076246b4d17078e88acc9ab 100644 --- a/app/services/wordpress.rb +++ b/app/services/wordpress.rb @@ -2,6 +2,7 @@ class Wordpress attr_reader :domain def self.clean(html) + # Relaxed config : https://github.com/rgrove/sanitize/blob/main/lib/sanitize/config/relaxed.rb fragment = Sanitize.fragment(html, Sanitize::Config.merge(Sanitize::Config::RELAXED, attributes: Sanitize::Config::RELAXED[:attributes].merge({ all: Sanitize::Config::RELAXED[:attributes][:all].dup.delete('class'), diff --git a/app/views/admin/communication/websites/import.html.erb b/app/views/admin/communication/websites/import.html.erb index 4b8432431f279e8391313a70d18aec98adfbbe4c..0274d1e7abccbe17f75cb18e6154133d6327e82f 100644 --- a/app/views/admin/communication/websites/import.html.erb +++ b/app/views/admin/communication/websites/import.html.erb @@ -84,11 +84,11 @@ <% @imported_media.each do |medium| %> <tr> <td><%= medium.filename %></td> - <td><%= number_to_human_size(medium.medium.file.blob.byte_size) if medium.medium&.file&.attached? %></td> + <td><%= number_to_human_size(medium.file.blob.byte_size) if medium&.file&.attached? %></td> <td class="text-end"> - <% if medium.medium&.file&.attached? %> + <% if medium&.file&.attached? %> <%= link_to t('show'), - url_for(medium.medium.file), + url_for(medium.file), class: button_classes, target: :blank %> <% else %> diff --git a/db/migrate/20211025124617_remove_communication_website_media.rb b/db/migrate/20211025124617_remove_communication_website_media.rb new file mode 100644 index 0000000000000000000000000000000000000000..688ee3ce1d3ff8dacd83fe1adc7755d21a9738dc --- /dev/null +++ b/db/migrate/20211025124617_remove_communication_website_media.rb @@ -0,0 +1,6 @@ +class RemoveCommunicationWebsiteMedia < ActiveRecord::Migration[6.1] + def change + remove_column :communication_website_imported_media, :medium_id + drop_table :communication_website_media + end +end diff --git a/db/schema.rb b/db/schema.rb index 963a4246c73a26a8ae5452fd6240155553e9462e..1eb10b1a5faa686ad8f52b58b0538cd1e6b6eee1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_10_25_102046) do +ActiveRecord::Schema.define(version: 2021_10_25_124617) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -87,9 +87,7 @@ ActiveRecord::Schema.define(version: 2021_10_25_102046) do t.uuid "university_id", null: false t.uuid "website_id", null: false t.string "filename" - t.uuid "medium_id" t.string "mime_type" - t.index ["medium_id"], name: "index_communication_website_imported_media_on_medium_id" t.index ["university_id"], name: "index_communication_website_imported_media_on_university_id" t.index ["website_id"], name: "index_communication_website_imported_media_on_website_id" end @@ -151,19 +149,6 @@ ActiveRecord::Schema.define(version: 2021_10_25_102046) do t.index ["website_id"], name: "index_communication_website_imported_websites_on_website_id" end - create_table "communication_website_media", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| - t.string "identifier" - t.string "filename" - t.string "mime_type" - t.text "file_url" - t.uuid "university_id", null: false - t.uuid "website_id", null: false - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["university_id"], name: "index_communication_website_media_on_university_id" - t.index ["website_id"], name: "index_communication_website_media_on_website_id" - end - create_table "communication_website_pages", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.uuid "university_id", null: false t.uuid "communication_website_id", null: false @@ -387,7 +372,6 @@ ActiveRecord::Schema.define(version: 2021_10_25_102046) do add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "administration_qualiopi_indicators", "administration_qualiopi_criterions", column: "criterion_id" add_foreign_key "communication_website_imported_media", "communication_website_imported_websites", column: "website_id" - add_foreign_key "communication_website_imported_media", "communication_website_media", column: "medium_id" add_foreign_key "communication_website_imported_media", "universities" add_foreign_key "communication_website_imported_pages", "communication_website_imported_media", column: "featured_medium_id" add_foreign_key "communication_website_imported_pages", "communication_website_imported_websites", column: "website_id" @@ -399,8 +383,6 @@ ActiveRecord::Schema.define(version: 2021_10_25_102046) do add_foreign_key "communication_website_imported_posts", "universities" add_foreign_key "communication_website_imported_websites", "communication_websites", column: "website_id" add_foreign_key "communication_website_imported_websites", "universities" - add_foreign_key "communication_website_media", "communication_websites", column: "website_id" - add_foreign_key "communication_website_media", "universities" add_foreign_key "communication_website_pages", "communication_website_pages", column: "parent_id" add_foreign_key "communication_website_pages", "communication_websites" add_foreign_key "communication_website_pages", "universities" diff --git a/test/models/communication/website/imported/medium_test.rb b/test/models/communication/website/imported/medium_test.rb index e20280d2ac70763aeb5aa83f87fc30cc341ac786..5add977a92c6d842975348429524d1ddfbfec935 100644 --- a/test/models/communication/website/imported/medium_test.rb +++ b/test/models/communication/website/imported/medium_test.rb @@ -10,19 +10,16 @@ # mime_type :string # created_at :datetime # updated_at :datetime -# medium_id :uuid # university_id :uuid not null # website_id :uuid not null # # Indexes # -# index_communication_website_imported_media_on_medium_id (medium_id) # index_communication_website_imported_media_on_university_id (university_id) # index_communication_website_imported_media_on_website_id (website_id) # # Foreign Keys # -# fk_rails_... (medium_id => communication_website_media.id) # fk_rails_... (university_id => universities.id) # fk_rails_... (website_id => communication_website_imported_websites.id) # diff --git a/test/models/communication/website/medium_test.rb b/test/models/communication/website/medium_test.rb deleted file mode 100644 index 216a43e11591b9fbfddb3429bca2636fbb99b0b8..0000000000000000000000000000000000000000 --- a/test/models/communication/website/medium_test.rb +++ /dev/null @@ -1,31 +0,0 @@ -# == Schema Information -# -# Table name: communication_website_media -# -# id :uuid not null, primary key -# file_url :text -# filename :string -# identifier :string -# mime_type :string -# created_at :datetime not null -# updated_at :datetime not null -# university_id :uuid not null -# website_id :uuid not null -# -# Indexes -# -# index_communication_website_media_on_university_id (university_id) -# index_communication_website_media_on_website_id (website_id) -# -# Foreign Keys -# -# fk_rails_... (university_id => universities.id) -# fk_rails_... (website_id => communication_websites.id) -# -require "test_helper" - -class Communication::Website::MediumTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end