Remove duplicated attachments

This commit is contained in:
Paul Chavard 2019-08-28 14:27:41 +02:00
parent a3a47b8959
commit 8fcf1353f3
15 changed files with 59 additions and 173 deletions

View file

@ -40,33 +40,20 @@ class Admin::AttestationTemplatesController < AdminController
end end
def preview def preview
@title = activated_attestation_params[:title] @attestation = (@procedure.attestation_template || AttestationTemplate.new).render_attributes_for(activated_attestation_params)
@body = activated_attestation_params[:body]
@footer = activated_attestation_params[:footer]
@created_at = Time.zone.now
# In a case of a preview, when the user does not change its images,
# the images are not uploaded and thus should be retrieved from previous
# attestation_template
@logo = activated_attestation_params[:logo] || @procedure.attestation_template&.proxy_logo
@signature = activated_attestation_params[:signature] || @procedure.attestation_template&.proxy_signature
render 'admin/attestation_templates/show', formats: [:pdf] render 'admin/attestation_templates/show', formats: [:pdf]
end end
def delete_logo def delete_logo
attestation_template = @procedure.attestation_template @procedure.attestation_template.logo.purge_later
attestation_template.logo.purge_later
attestation_template.logo_active_storage.purge_later
flash.notice = 'le logo a bien été supprimée' flash.notice = 'le logo a bien été supprimée'
redirect_to edit_admin_procedure_attestation_template_path(@procedure) redirect_to edit_admin_procedure_attestation_template_path(@procedure)
end end
def delete_signature def delete_signature
attestation_template = @procedure.attestation_template @procedure.attestation_template.signature.purge_later
attestation_template.signature.purge_later
attestation_template.signature_active_storage.purge_later
flash.notice = 'la signature a bien été supprimée' flash.notice = 'la signature a bien été supprimée'
redirect_to edit_admin_procedure_attestation_template_path(@procedure) redirect_to edit_admin_procedure_attestation_template_path(@procedure)

View file

@ -208,7 +208,6 @@ class Admin::ProceduresController < AdminController
def delete_logo def delete_logo
@procedure.logo.purge_later @procedure.logo.purge_later
@procedure.logo_active_storage.purge_later
flash.notice = 'le logo a bien été supprimé' flash.notice = 'le logo a bien été supprimé'
redirect_to edit_admin_procedure_path(@procedure) redirect_to edit_admin_procedure_path(@procedure)

View file

@ -16,18 +16,11 @@ module Instructeurs
def attestation def attestation
if dossier.attestation.pdf.attached? if dossier.attestation.pdf.attached?
redirect_to url_for(dossier.attestation.pdf) redirect_to url_for(dossier.attestation.pdf)
elsif dossier.attestation.pdf_active_storage.attached?
redirect_to url_for(dossier.attestation.pdf_active_storage)
end end
end end
def apercu_attestation def apercu_attestation
@title = dossier.procedure.attestation_template.title_for_dossier(dossier) @attestation = dossier.procedure.attestation_template.render_attributes_for(dossier: dossier)
@body = dossier.procedure.attestation_template.body_for_dossier(dossier)
@footer = dossier.procedure.attestation_template.footer
@created_at = Time.zone.now
@logo = dossier.procedure.attestation_template&.proxy_logo
@signature = dossier.procedure.attestation_template&.proxy_signature
render 'admin/attestation_templates/show', formats: [:pdf] render 'admin/attestation_templates/show', formats: [:pdf]
end end

View file

@ -50,8 +50,6 @@ module Users
def attestation def attestation
if dossier.attestation.pdf.attached? if dossier.attestation.pdf.attached?
redirect_to url_for(dossier.attestation.pdf) redirect_to url_for(dossier.attestation.pdf)
elsif dossier.attestation.pdf_active_storage.attached?
redirect_to url_for(dossier.attestation.pdf_active_storage)
end end
end end

View file

@ -6,17 +6,12 @@ class ApplicationMailer < ActionMailer::Base
# Attach the procedure logo to the email (if any). # Attach the procedure logo to the email (if any).
# Returns the attachment url. # Returns the attachment url.
def attach_logo(procedure) def attach_logo(procedure)
return nil if !procedure.logo?
if procedure.logo.attached? if procedure.logo.attached?
logo_filename = procedure.logo.filename.to_s logo_filename = procedure.logo.filename.to_s
attachments.inline[logo_filename] = procedure.logo.download attachments.inline[logo_filename] = procedure.logo.download
elsif procedure.logo_active_storage.attached? attachments[logo_filename].url
logo_filename = procedure.logo_active_storage.filename.to_s
attachments.inline[logo_filename] = procedure.logo_active_storage.download
end end
attachments[logo_filename].url
rescue StandardError => e rescue StandardError => e
# A problem occured when reading logo, maybe the logo is missing and we should clean the procedure to remove logo reference ? # A problem occured when reading logo, maybe the logo is missing and we should clean the procedure to remove logo reference ?
Raven.extra_context(procedure_id: procedure.id) Raven.extra_context(procedure_id: procedure.id)

View file

@ -4,13 +4,10 @@ class Attestation < ApplicationRecord
belongs_to :dossier belongs_to :dossier
has_one_attached :pdf has_one_attached :pdf
has_one_attached :pdf_active_storage
def pdf_url def pdf_url
if pdf.attached? if pdf.attached?
Rails.application.routes.url_helpers.url_for(pdf) Rails.application.routes.url_helpers.url_for(pdf)
elsif pdf_active_storage.attached?
Rails.application.routes.url_helpers.url_for(pdf_active_storage)
end end
end end
end end

View file

@ -7,9 +7,7 @@ class AttestationTemplate < ApplicationRecord
belongs_to :procedure belongs_to :procedure
has_one_attached :logo has_one_attached :logo
has_one_attached :logo_active_storage
has_one_attached :signature has_one_attached :signature
has_one_attached :signature_active_storage
validates :footer, length: { maximum: 190 } validates :footer, length: { maximum: 190 }
@ -54,14 +52,6 @@ class AttestationTemplate < ApplicationRecord
# we don't want to run virus scanner on duplicated file # we don't want to run virus scanner on duplicated file
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE } metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
) )
elsif logo_active_storage.attached?
attestation_template.logo.attach(
io: StringIO.new(logo_active_storage.download),
filename: logo_active_storage.filename.to_s,
content_type: logo_active_storage.content_type,
# we don't want to run virus scanner on duplicated file
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
end end
if signature.attached? if signature.attached?
@ -72,65 +62,34 @@ class AttestationTemplate < ApplicationRecord
# we don't want to run virus scanner on duplicated file # we don't want to run virus scanner on duplicated file
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE } metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
) )
elsif signature_active_storage.attached?
attestation_template.signature.attach(
io: StringIO.new(signature_active_storage.download),
filename: signature_active_storage.filename.to_s,
content_type: signature_active_storage.content_type,
# we don't want to run virus scanner on duplicated file
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
end end
attestation_template attestation_template
end end
def logo?
logo.attached? || logo_active_storage.attached?
end
def signature?
signature.attached? || signature_active_storage.attached?
end
def logo_url def logo_url
if logo.attached? if logo.attached?
Rails.application.routes.url_helpers.url_for(logo) Rails.application.routes.url_helpers.url_for(logo)
elsif logo_active_storage.attached?
Rails.application.routes.url_helpers.url_for(logo_active_storage)
end end
end end
def signature_url def signature_url
if signature.attached? if signature.attached?
Rails.application.routes.url_helpers.url_for(signature) Rails.application.routes.url_helpers.url_for(signature)
elsif signature_active_storage.attached?
Rails.application.routes.url_helpers.url_for(signature_active_storage)
end end
end end
def proxy_logo def render_attributes_for(params = {})
if logo.attached? dossier = params.fetch(:dossier, false)
logo
elsif logo_active_storage.attached?
logo_active_storage
end
end
def proxy_signature {
if signature.attached? created_at: Time.zone.now,
signature title: dossier ? replace_tags(title, dossier) : params.fetch(:title, ''),
elsif signature_active_storage.attached? body: dossier ? replace_tags(body, dossier) : params.fetch(:body, ''),
signature_active_storage footer: params.fetch(:footer, footer),
end logo: params.fetch(:logo, logo.attached? ? logo : nil),
end signature: params.fetch(:signature, signature.attached? ? signature : nil)
}
def title_for_dossier(dossier)
replace_tags(title, dossier)
end
def body_for_dossier(dossier)
replace_tags(body, dossier)
end end
private private
@ -147,14 +106,8 @@ class AttestationTemplate < ApplicationRecord
end end
def build_pdf(dossier) def build_pdf(dossier)
action_view = ActionView::Base.new(ActionController::Base.view_paths, attestation = render_attributes_for(dossier: dossier)
logo: proxy_logo, action_view = ActionView::Base.new(ActionController::Base.view_paths, attestation: attestation)
title: title_for_dossier(dossier),
body: body_for_dossier(dossier),
signature: proxy_signature,
footer: footer,
created_at: Time.zone.now)
attestation_view = action_view.render(file: 'admin/attestation_templates/show', formats: [:pdf]) attestation_view = action_view.render(file: 'admin/attestation_templates/show', formats: [:pdf])
StringIO.new(attestation_view) StringIO.new(attestation_view)

View file

@ -32,7 +32,6 @@ class Procedure < ApplicationRecord
has_one :defaut_groupe_instructeur, -> { where(label: GroupeInstructeur::DEFAULT_LABEL) }, class_name: 'GroupeInstructeur', inverse_of: :procedure has_one :defaut_groupe_instructeur, -> { where(label: GroupeInstructeur::DEFAULT_LABEL) }, class_name: 'GroupeInstructeur', inverse_of: :procedure
has_one_attached :logo has_one_attached :logo
has_one_attached :logo_active_storage
has_one_attached :notice has_one_attached :notice
has_one_attached :deliberation has_one_attached :deliberation
@ -304,7 +303,6 @@ class Procedure < ApplicationRecord
clone_attachment(:piece_justificative_template, original, kopy) clone_attachment(:piece_justificative_template, original, kopy)
elsif original.is_a?(Procedure) elsif original.is_a?(Procedure)
clone_attachment(:logo, original, kopy) clone_attachment(:logo, original, kopy)
clone_attachment(:logo_active_storage, original, kopy)
clone_attachment(:notice, original, kopy) clone_attachment(:notice, original, kopy)
clone_attachment(:deliberation, original, kopy) clone_attachment(:deliberation, original, kopy)
end end
@ -471,15 +469,9 @@ class Procedure < ApplicationRecord
end end
end end
def logo?
logo.attached? || logo_active_storage.attached?
end
def logo_url def logo_url
if logo.attached? if logo.attached?
Rails.application.routes.url_helpers.url_for(logo) Rails.application.routes.url_helpers.url_for(logo)
elsif logo_active_storage.attached?
Rails.application.routes.url_helpers.url_for(logo_active_storage)
else else
ActionController::Base.helpers.image_url("marianne.svg") ActionController::Base.helpers.image_url("marianne.svg")
end end

View file

@ -17,11 +17,11 @@
celle-ci est également disponible au téléchargement depuis lespace personnel de lusager. celle-ci est également disponible au téléchargement depuis lespace personnel de lusager.
.image-upload .image-upload
- if @attestation_template.logo? - if @attestation_template.logo.attached?
= image_tag @attestation_template.logo_url, class: 'thumbnail' = image_tag @attestation_template.logo_url, class: 'thumbnail'
.form-group .form-group
= f.label :logo, "Logo de l'attestation" = f.label :logo, "Logo de l'attestation"
- if @attestation_template.logo? - if @attestation_template.logo.attached?
= link_to 'Supprimer le logo', admin_procedure_attestation_template_logo_path(@procedure), method: :delete = link_to 'Supprimer le logo', admin_procedure_attestation_template_logo_path(@procedure), method: :delete
= f.file_field :logo, accept: 'image/png, image/jpg, image/jpeg' = f.file_field :logo, accept: 'image/png, image/jpg, image/jpeg'
%p.help-block %p.help-block
@ -54,11 +54,11 @@
= tag[:description] = tag[:description]
.image-upload .image-upload
- if @attestation_template.signature? - if @attestation_template.signature.attached?
= image_tag @attestation_template.signature_url, class: 'thumbnail' = image_tag @attestation_template.signature_url, class: 'thumbnail'
.form-group .form-group
= f.label :signature, "Tampon de l'attestation" = f.label :signature, "Tampon de l'attestation"
- if @attestation_template.signature? - if @attestation_template.signature.attached?
= link_to 'Supprimer le tampon', admin_procedure_attestation_template_signature_path(@procedure), method: :delete = link_to 'Supprimer le tampon', admin_procedure_attestation_template_signature_path(@procedure), method: :delete
= f.file_field :signature, accept: 'image/png, image/jpg, image/jpeg' = f.file_field :signature, accept: 'image/png, image/jpg, image/jpeg'
%p.help-block %p.help-block

View file

@ -19,6 +19,14 @@ max_logo_width = body_width
max_logo_height = 50.mm max_logo_height = 50.mm
max_signature_size = 50.mm max_signature_size = 50.mm
title = @attestation.fetch(:title)
body = @attestation.fetch(:body)
footer = @attestation.fetch(:footer)
created_at = @attestation.fetch(:created_at)
logo = @attestation[:logo]
signature = @attestation[:signature]
prawn_document(margin: [top_margin, right_margin, bottom_margin, left_margin], page_size: page_size) do |pdf| prawn_document(margin: [top_margin, right_margin, bottom_margin, left_margin], page_size: page_size) do |pdf|
pdf.font_families.update( 'liberation serif' => { normal: Rails.root.join('lib/prawn/fonts/liberation_serif/LiberationSerif-Regular.ttf' )}) pdf.font_families.update( 'liberation serif' => { normal: Rails.root.join('lib/prawn/fonts/liberation_serif/LiberationSerif-Regular.ttf' )})
pdf.font 'liberation serif' pdf.font 'liberation serif'
@ -29,30 +37,30 @@ prawn_document(margin: [top_margin, right_margin, bottom_margin, left_margin], p
body_height = pdf.cursor - footer_height body_height = pdf.cursor - footer_height
pdf.bounding_box([0, pdf.cursor], width: body_width, height: body_height) do pdf.bounding_box([0, pdf.cursor], width: body_width, height: body_height) do
if @logo.present? if logo.present?
logo_file = if @logo.is_a?(ActiveStorage::Attached::One) logo_file = if logo.is_a?(ActiveStorage::Attached::One)
@logo.download logo.download
else else
@logo.read logo.read
end end
pdf.image StringIO.new(logo_file), fit: [max_logo_width , max_logo_height], position: :center pdf.image StringIO.new(logo_file), fit: [max_logo_width , max_logo_height], position: :center
end end
pdf.fill_color grey pdf.fill_color grey
pdf.pad_top(40) { pdf.text "le #{l(@created_at, format: '%e %B %Y')}", size: 10, align: :right, character_spacing: -0.5 } pdf.pad_top(40) { pdf.text "le #{l(created_at, format: '%e %B %Y')}", size: 10, align: :right, character_spacing: -0.5 }
pdf.fill_color black pdf.fill_color black
pdf.pad_top(40) { pdf.text @title, size: 18, character_spacing: -0.2 } pdf.pad_top(40) { pdf.text title, size: 18, character_spacing: -0.2 }
pdf.fill_color grey pdf.fill_color grey
pdf.pad_top(30) { pdf.text @body, size: 10, character_spacing: -0.2, align: :justify } pdf.pad_top(30) { pdf.text body, size: 10, character_spacing: -0.2, align: :justify }
if @signature.present? if signature.present?
pdf.pad_top(40) do pdf.pad_top(40) do
signature_file = if @signature.is_a?(ActiveStorage::Attached::One) signature_file = if signature.is_a?(ActiveStorage::Attached::One)
@signature.download signature.download
else else
@signature.read signature.read
end end
pdf.image StringIO.new(signature_file), fit: [max_signature_size , max_signature_size], position: :right pdf.image StringIO.new(signature_file), fit: [max_signature_size , max_signature_size], position: :right
end end
@ -62,6 +70,6 @@ prawn_document(margin: [top_margin, right_margin, bottom_margin, left_margin], p
pdf.repeat(:all) do pdf.repeat(:all) do
pdf.move_cursor_to footer_height - 10 pdf.move_cursor_to footer_height - 10
pdf.fill_color grey pdf.fill_color grey
pdf.text @footer, align: :center, size: 8 pdf.text footer, align: :center, size: 8
end end
end end

View file

@ -77,7 +77,7 @@
.row .row
.col-md-6 .col-md-6
%h4 Logo de la démarche %h4 Logo de la démarche
- if @procedure.logo? - if @procedure.logo.attached?
= image_tag @procedure.logo_url, { style: 'height: 40px; display: inline; margin-right: 6px;', id: 'preview_procedure_logo' } = image_tag @procedure.logo_url, { style: 'height: 40px; display: inline; margin-right: 6px;', id: 'preview_procedure_logo' }
\- \-

View file

@ -5,7 +5,7 @@
%strong Logo %strong Logo
%p %p
- if field.data.logo? - if field.data.logo.attached?
= image_tag field.data.logo_url = image_tag field.data.logo_url
- else - else
Aucun Aucun
@ -20,7 +20,7 @@
%strong Signature %strong Signature
%p %p
- if field.data.signature? - if field.data.signature.attached?
= image_tag field.data.signature_url = image_tag field.data.signature_url
- else - else
Aucun Aucun

View file

@ -1,38 +0,0 @@
namespace :'2019_05_29_migrate_commentaire_pj' do
task run: :environment do
commentaires = Commentaire.where
.not(file: nil)
.left_joins(:piece_jointe_attachment)
.where('active_storage_attachments.id IS NULL')
.order(:created_at)
limit = ENV['LIMIT']
if limit
commentaires.limit!(limit.to_i)
end
progress = ProgressReport.new(commentaires.count)
commentaires.find_each do |commentaire|
if commentaire.file.present?
dossier = Dossier.unscope(where: :hidden_at).find(commentaire.dossier_id)
uri = URI.parse(URI.escape(commentaire.file_url))
response = Typhoeus.get(uri)
if response.success?
filename = commentaire.file.filename || commentaire.file_identifier
updated_at = commentaire.updated_at
dossier_updated_at = dossier.updated_at
commentaire.piece_jointe.attach(
io: StringIO.new(response.body),
filename: filename,
content_type: commentaire.file.content_type,
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
commentaire.update_column(:updated_at, updated_at)
dossier.update_column(:updated_at, dossier_updated_at)
end
end
progress.inc
end
progress.finish
end
end

View file

@ -30,13 +30,13 @@ describe Admin::AttestationTemplatesController, type: :controller do
context 'with an interlaced png' do context 'with an interlaced png' do
let(:upload_params) { { logo: interlaced_logo } } let(:upload_params) { { logo: interlaced_logo } }
it { expect(assigns(:logo).read).to eq(uninterlaced_logo.read) } it { expect(assigns(:attestation)[:logo].read).to eq(uninterlaced_logo.read) }
end end
context 'if an attestation template does not exist on the procedure' do context 'if an attestation template does not exist on the procedure' do
let(:attestation_template) { nil } let(:attestation_template) { nil }
it { expect(subject.status).to eq(200) } it { expect(subject.status).to eq(200) }
it { expect(assigns).to include(upload_params.stringify_keys) } it { expect(assigns(:attestation)).to include(upload_params) }
end end
context 'if an attestation template exists on the procedure' do context 'if an attestation template exists on the procedure' do
@ -48,18 +48,18 @@ describe Admin::AttestationTemplatesController, type: :controller do
end end
it { expect(subject.status).to eq(200) } it { expect(subject.status).to eq(200) }
it { expect(assigns).to include(upload_params.stringify_keys) } it { expect(assigns(:attestation)).to include(upload_params) }
it { expect(assigns[:created_at]).to eq(Time.zone.now) } it { expect(assigns(:attestation)[:created_at]).to eq(Time.zone.now) }
it { expect(assigns(:logo).download).to eq(logo2.read) } it { expect(assigns(:attestation)[:logo].download).to eq(logo2.read) }
it { expect(assigns(:signature).download).to eq(signature2.read) } it { expect(assigns(:attestation)[:signature].download).to eq(signature2.read) }
end end
context 'with empty logo' do context 'with empty logo' do
it { expect(subject.status).to eq(200) } it { expect(subject.status).to eq(200) }
it { expect(assigns).to include(upload_params.stringify_keys) } it { expect(assigns(:attestation)).to include(upload_params) }
it { expect(assigns[:created_at]).to eq(Time.zone.now) } it { expect(assigns(:attestation)[:created_at]).to eq(Time.zone.now) }
it { expect(assigns(:logo)).to eq(nil) } it { expect(assigns(:attestation)[:logo]).to eq(nil) }
it { expect(assigns(:signature)).to eq(nil) } it { expect(assigns(:attestation)[:signature]).to eq(nil) }
end end
end end
end end

View file

@ -159,9 +159,11 @@ describe AttestationTemplate, type: :model do
.update(value: 'libelle2') .update(value: 'libelle2')
end end
it { expect(view_args[:title]).to eq('title libelle1') } it do
it { expect(view_args[:body]).to eq('body libelle2') } expect(view_args[:attestation][:title]).to eq('title libelle1')
it { expect(attestation.title).to eq('title libelle1') } expect(view_args[:attestation][:body]).to eq('body libelle2')
expect(attestation.title).to eq('title libelle1')
end
end end
end end
end end