diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index f864a21a6..7cacb6c37 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -249,8 +249,11 @@ class Admin::ProceduresController < AdminController end def delete_logo - @procedure.remove_logo! - @procedure.save + if @procedure.logo + @procedure.remove_logo! + @procedure.save + end + @procedure.logo_active_storage.purge_later flash.notice = 'le logo a bien été supprimé' redirect_to edit_admin_procedure_path(@procedure) @@ -278,10 +281,12 @@ class Admin::ProceduresController < AdminController def procedure_params editable_params = [:libelle, :description, :organisation, :direction, :lien_site_web, :cadre_juridique, :deliberation, :notice, :web_hook_url, :euro_flag, :logo, :auto_archive_on, :monavis_embed] - if @procedure&.locked? + permited_params = if @procedure&.locked? params.require(:procedure).permit(*editable_params) else params.require(:procedure).permit(*editable_params, :duree_conservation_dossiers_dans_ds, :duree_conservation_dossiers_hors_ds, :for_individual, :ask_birthday, :path) end + permited_params[:logo_active_storage] = permited_params.delete(:logo) + permited_params end end diff --git a/app/controllers/new_administrateur/mail_templates_controller.rb b/app/controllers/new_administrateur/mail_templates_controller.rb index b747f9681..7902a3ea6 100644 --- a/app/controllers/new_administrateur/mail_templates_controller.rb +++ b/app/controllers/new_administrateur/mail_templates_controller.rb @@ -7,7 +7,7 @@ module NewAdministrateur dossier = Dossier.new(id: '1', procedure: procedure) @dossier = dossier - @logo_url = procedure.logo.url + @logo_url = procedure.logo_url @service = procedure.service @rendered_template = sanitize(mail_template.body) @actions = mail_template.actions_for_dossier(dossier) diff --git a/app/helpers/procedure_helper.rb b/app/helpers/procedure_helper.rb index 92bae1271..76ddd6760 100644 --- a/app/helpers/procedure_helper.rb +++ b/app/helpers/procedure_helper.rb @@ -20,20 +20,6 @@ module ProcedureHelper t(action, scope: [:modal, :publish, key]) end - def logo_img(procedure) - logo = procedure.logo - - if logo.blank? - ActionController::Base.helpers.image_url("marianne.svg") - else - if Rails.application.secrets.fog[:enabled] - RemoteDownloader.new(logo.filename).url - else - LocalDownloader.new(logo.path, 'logo').url - end - end - end - def types_de_champ_data(procedure) { isAnnotation: false, diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 88f1a3353..e80ed82af 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -9,8 +9,13 @@ class ApplicationMailer < ActionMailer::Base return nil if !procedure.logo? begin - logo_filename = procedure.logo.filename - attachments.inline[logo_filename] = procedure.logo.read + if procedure.logo_active_storage.attached? + logo_filename = procedure.logo_active_storage.filename + attachments.inline[logo_filename] = procedure.logo_active_storage.download + else + logo_filename = procedure.logo.filename + attachments.inline[logo_filename] = procedure.logo.read + end attachments[logo_filename].url rescue StandardError => e diff --git a/app/models/procedure.rb b/app/models/procedure.rb index b669122a0..52d029d0a 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -25,6 +25,7 @@ class Procedure < ApplicationRecord has_one :refused_mail, class_name: "Mails::RefusedMail", dependent: :destroy has_one :without_continuation_mail, class_name: "Mails::WithoutContinuationMail", dependent: :destroy + has_one_attached :logo_active_storage has_one_attached :notice has_one_attached :deliberation @@ -215,7 +216,9 @@ class Procedure < ApplicationRecord procedure.archived_at = nil procedure.published_at = nil procedure.logo_secure_token = nil - procedure.remote_logo_url = self.logo_url + if logo.present? + procedure.remote_logo_url = self.logo_url + end procedure.lien_notice = nil if is_different_admin || from_library @@ -252,6 +255,7 @@ class Procedure < ApplicationRecord if original.is_a?(TypeDeChamp) clone_attachment(:piece_justificative_template, original, kopy) elsif original.is_a?(Procedure) + clone_attachment(:logo_active_storage, original, kopy) clone_attachment(:notice, original, kopy) clone_attachment(:deliberation, original, kopy) end @@ -452,6 +456,24 @@ class Procedure < ApplicationRecord end end + def logo? + logo.present? || logo_active_storage.attached? + end + + def logo_url + if !logo? + ActionController::Base.helpers.image_url("marianne.svg") + elsif logo_active_storage.attached? + Rails.application.routes.url_helpers.url_for(logo_active_storage) + else + if Rails.application.secrets.fog[:enabled] + RemoteDownloader.new(logo.filename).url + else + LocalDownloader.new(logo.path, 'logo').url + end + end + end + private def move_type_de_champ_attributes(types_de_champ, type_de_champ, new_index) diff --git a/app/views/admin/procedures/_informations.html.haml b/app/views/admin/procedures/_informations.html.haml index 2ff2e84b2..29b6d0a45 100644 --- a/app/views/admin/procedures/_informations.html.haml +++ b/app/views/admin/procedures/_informations.html.haml @@ -77,13 +77,16 @@ .row .col-md-6 %h4 Logo de la démarche - - if @procedure.logo.present? - = image_tag logo_img(@procedure), { style: 'height: 40px; display: inline; margin-right: 6px;', id: 'preview_procedure_logo' } + - if @procedure.logo? + = image_tag @procedure.logo_url, { style: 'height: 40px; display: inline; margin-right: 6px;', id: 'preview_procedure_logo' } \- - if @procedure.persisted? = link_to 'supprimer', delete_logo_admin_procedure_path(@procedure), method: :delete - = f.file_field :logo, accept: 'image/png, image/jpg, image/jpeg', style: 'display: inline' + = f.file_field :logo, + direct_upload: true, + accept: 'image/png, image/jpg, image/jpeg', + style: 'display: inline' %div{ style: 'margin-top: 5px;' } %i diff --git a/app/views/instructeurs/procedures/show.html.haml b/app/views/instructeurs/procedures/show.html.haml index f36e0c324..8c2b404ec 100644 --- a/app/views/instructeurs/procedures/show.html.haml +++ b/app/views/instructeurs/procedures/show.html.haml @@ -4,7 +4,7 @@ .sub-header .container.flex - .procedure-logo{ style: @procedure.logo.present? ? "background-image: url(#{@procedure.logo.url})" : nil, + .procedure-logo{ style: @procedure.logo? ? "background-image: url(#{@procedure.logo_url})" : nil, role: 'img', 'aria-label': "logo de la démarche #{@procedure.libelle}" } .procedure-header diff --git a/app/views/layouts/commencer/_procedure_description.html.haml b/app/views/layouts/commencer/_procedure_description.html.haml index 04a05ffac..56409a384 100644 --- a/app/views/layouts/commencer/_procedure_description.html.haml +++ b/app/views/layouts/commencer/_procedure_description.html.haml @@ -1,5 +1,5 @@ .procedure-logos - = image_tag logo_img(procedure) + = image_tag procedure.logo_url - if procedure.euro_flag = image_tag "flag_of_europe.svg" %h2.procedure-title diff --git a/app/views/layouts/left_panels/_left_panel_admin_procedurescontroller_navbar.html.haml b/app/views/layouts/left_panels/_left_panel_admin_procedurescontroller_navbar.html.haml index 97c44d546..dd28f4914 100644 --- a/app/views/layouts/left_panels/_left_panel_admin_procedurescontroller_navbar.html.haml +++ b/app/views/layouts/left_panels/_left_panel_admin_procedurescontroller_navbar.html.haml @@ -1,7 +1,7 @@ #first-block .en-cours - - if @procedure.logo.present? - = image_tag logo_img(@procedure), style: 'width: 30px;' + - if @procedure.logo? + = image_tag @procedure.logo_url, style: 'width: 30px;' %b = @procedure.libelle diff --git a/app/views/users/sessions/_resume_procedure.html.haml b/app/views/users/sessions/_resume_procedure.html.haml index 895730eef..effb02c79 100644 --- a/app/views/users/sessions/_resume_procedure.html.haml +++ b/app/views/users/sessions/_resume_procedure.html.haml @@ -8,7 +8,7 @@ = image_tag('drapeau_europe.png') #logo_procedure.flag - = image_tag(logo_img(dossier.procedure)) + = image_tag(dossier.procedure.logo_url) %h2#titre-procedure.text-info = @dossier.procedure.libelle diff --git a/lib/tasks/2019_08_20_migrate_procedure_logo.rake b/lib/tasks/2019_08_20_migrate_procedure_logo.rake new file mode 100644 index 000000000..f8f7c5c1e --- /dev/null +++ b/lib/tasks/2019_08_20_migrate_procedure_logo.rake @@ -0,0 +1,33 @@ +namespace :'2019_08_20_migrate_procedure_logo' do + task run: :environment do + procedures = Procedure.where + .not(logo: nil) + .left_joins(:logo_new_attachment) + .where('active_storage_attachments.id IS NULL') + .order(:created_at) + + limit = ENV['LIMIT'] + if limit + procedures.limit!(limit.to_i) + end + + progress = ProgressReport.new(procedures.count) + procedures.find_each do |procedure| + if procedure.logo.present? + uri = URI.parse(URI.escape(procedure.logo_url)) + response = Typhoeus.get(uri) + if response.success? + filename = procedure.logo.filename || procedure.logo_identifier + procedure.logo_new.attach( + io: StringIO.new(response.body), + filename: filename, + content_type: procedure.logo.content_type, + metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE } + ) + end + end + progress.inc + end + progress.finish + end +end diff --git a/spec/controllers/new_administrateur/mail_templates_controller_spec.rb b/spec/controllers/new_administrateur/mail_templates_controller_spec.rb index 11ed74f81..b93d4857c 100644 --- a/spec/controllers/new_administrateur/mail_templates_controller_spec.rb +++ b/spec/controllers/new_administrateur/mail_templates_controller_spec.rb @@ -14,7 +14,7 @@ describe NewAdministrateur::MailTemplatesController, type: :controller do it { expect(response).to have_http_status(:ok) } it 'displays the procedure logo' do - expect(response.body).to have_css("img[src*='#{procedure.logo.filename}']") + expect(response.body).to have_css("img[src*='#{procedure.logo_url}']") end it 'displays the action buttons' do diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 2c79126a1..3628f8d21 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -43,7 +43,7 @@ FactoryBot.define do end trait :with_logo do - logo { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') } + logo_active_storage { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') } end trait :with_path do diff --git a/spec/helpers/procedure_helper_spec.rb b/spec/helpers/procedure_helper_spec.rb deleted file mode 100644 index 838e05e1f..000000000 --- a/spec/helpers/procedure_helper_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -RSpec.describe ProcedureHelper, type: :helper do - let(:procedure) { create(:procedure) } - - describe ".logo_img" do - subject { logo_img(procedure) } - - it { is_expected.to match(/#{ActionController::Base.helpers.image_url("marianne.svg")}/) } - end -end