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
def preview
@title = activated_attestation_params[:title]
@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
@attestation = (@procedure.attestation_template || AttestationTemplate.new).render_attributes_for(activated_attestation_params)
render 'admin/attestation_templates/show', formats: [:pdf]
end
def delete_logo
attestation_template = @procedure.attestation_template
attestation_template.logo.purge_later
attestation_template.logo_active_storage.purge_later
@procedure.attestation_template.logo.purge_later
flash.notice = 'le logo a bien été supprimée'
redirect_to edit_admin_procedure_attestation_template_path(@procedure)
end
def delete_signature
attestation_template = @procedure.attestation_template
attestation_template.signature.purge_later
attestation_template.signature_active_storage.purge_later
@procedure.attestation_template.signature.purge_later
flash.notice = 'la signature a bien été supprimée'
redirect_to edit_admin_procedure_attestation_template_path(@procedure)

View file

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

View file

@ -16,18 +16,11 @@ module Instructeurs
def attestation
if dossier.attestation.pdf.attached?
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
def apercu_attestation
@title = dossier.procedure.attestation_template.title_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
@attestation = dossier.procedure.attestation_template.render_attributes_for(dossier: dossier)
render 'admin/attestation_templates/show', formats: [:pdf]
end

View file

@ -50,8 +50,6 @@ module Users
def attestation
if dossier.attestation.pdf.attached?
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

View file

@ -6,17 +6,12 @@ class ApplicationMailer < ActionMailer::Base
# Attach the procedure logo to the email (if any).
# Returns the attachment url.
def attach_logo(procedure)
return nil if !procedure.logo?
if procedure.logo.attached?
logo_filename = procedure.logo.filename.to_s
attachments.inline[logo_filename] = procedure.logo.download
elsif procedure.logo_active_storage.attached?
logo_filename = procedure.logo_active_storage.filename.to_s
attachments.inline[logo_filename] = procedure.logo_active_storage.download
attachments[logo_filename].url
end
attachments[logo_filename].url
rescue StandardError => e
# 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)

View file

@ -4,13 +4,10 @@ class Attestation < ApplicationRecord
belongs_to :dossier
has_one_attached :pdf
has_one_attached :pdf_active_storage
def pdf_url
if pdf.attached?
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

View file

@ -7,9 +7,7 @@ class AttestationTemplate < ApplicationRecord
belongs_to :procedure
has_one_attached :logo
has_one_attached :logo_active_storage
has_one_attached :signature
has_one_attached :signature_active_storage
validates :footer, length: { maximum: 190 }
@ -54,14 +52,6 @@ class AttestationTemplate < ApplicationRecord
# we don't want to run virus scanner on duplicated file
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
if signature.attached?
@ -72,65 +62,34 @@ class AttestationTemplate < ApplicationRecord
# we don't want to run virus scanner on duplicated file
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
attestation_template
end
def logo?
logo.attached? || logo_active_storage.attached?
end
def signature?
signature.attached? || signature_active_storage.attached?
end
def logo_url
if logo.attached?
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
def signature_url
if signature.attached?
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
def proxy_logo
if logo.attached?
logo
elsif logo_active_storage.attached?
logo_active_storage
end
end
def render_attributes_for(params = {})
dossier = params.fetch(:dossier, false)
def proxy_signature
if signature.attached?
signature
elsif signature_active_storage.attached?
signature_active_storage
end
end
def title_for_dossier(dossier)
replace_tags(title, dossier)
end
def body_for_dossier(dossier)
replace_tags(body, dossier)
{
created_at: Time.zone.now,
title: dossier ? replace_tags(title, dossier) : params.fetch(:title, ''),
body: dossier ? replace_tags(body, dossier) : params.fetch(:body, ''),
footer: params.fetch(:footer, footer),
logo: params.fetch(:logo, logo.attached? ? logo : nil),
signature: params.fetch(:signature, signature.attached? ? signature : nil)
}
end
private
@ -147,14 +106,8 @@ class AttestationTemplate < ApplicationRecord
end
def build_pdf(dossier)
action_view = ActionView::Base.new(ActionController::Base.view_paths,
logo: proxy_logo,
title: title_for_dossier(dossier),
body: body_for_dossier(dossier),
signature: proxy_signature,
footer: footer,
created_at: Time.zone.now)
attestation = render_attributes_for(dossier: dossier)
action_view = ActionView::Base.new(ActionController::Base.view_paths, attestation: attestation)
attestation_view = action_view.render(file: 'admin/attestation_templates/show', formats: [:pdf])
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_attached :logo
has_one_attached :logo_active_storage
has_one_attached :notice
has_one_attached :deliberation
@ -304,7 +303,6 @@ class Procedure < ApplicationRecord
clone_attachment(:piece_justificative_template, original, kopy)
elsif original.is_a?(Procedure)
clone_attachment(:logo, original, kopy)
clone_attachment(:logo_active_storage, original, kopy)
clone_attachment(:notice, original, kopy)
clone_attachment(:deliberation, original, kopy)
end
@ -471,15 +469,9 @@ class Procedure < ApplicationRecord
end
end
def logo?
logo.attached? || logo_active_storage.attached?
end
def logo_url
if logo.attached?
Rails.application.routes.url_helpers.url_for(logo)
elsif logo_active_storage.attached?
Rails.application.routes.url_helpers.url_for(logo_active_storage)
else
ActionController::Base.helpers.image_url("marianne.svg")
end

View file

@ -17,11 +17,11 @@
celle-ci est également disponible au téléchargement depuis lespace personnel de lusager.
.image-upload
- if @attestation_template.logo?
- if @attestation_template.logo.attached?
= image_tag @attestation_template.logo_url, class: 'thumbnail'
.form-group
= 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
= f.file_field :logo, accept: 'image/png, image/jpg, image/jpeg'
%p.help-block
@ -54,11 +54,11 @@
= tag[:description]
.image-upload
- if @attestation_template.signature?
- if @attestation_template.signature.attached?
= image_tag @attestation_template.signature_url, class: 'thumbnail'
.form-group
= 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
= f.file_field :signature, accept: 'image/png, image/jpg, image/jpeg'
%p.help-block

View file

@ -19,6 +19,14 @@ max_logo_width = body_width
max_logo_height = 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|
pdf.font_families.update( 'liberation serif' => { normal: Rails.root.join('lib/prawn/fonts/liberation_serif/LiberationSerif-Regular.ttf' )})
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
pdf.bounding_box([0, pdf.cursor], width: body_width, height: body_height) do
if @logo.present?
logo_file = if @logo.is_a?(ActiveStorage::Attached::One)
@logo.download
if logo.present?
logo_file = if logo.is_a?(ActiveStorage::Attached::One)
logo.download
else
@logo.read
logo.read
end
pdf.image StringIO.new(logo_file), fit: [max_logo_width , max_logo_height], position: :center
end
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.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.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
signature_file = if @signature.is_a?(ActiveStorage::Attached::One)
@signature.download
signature_file = if signature.is_a?(ActiveStorage::Attached::One)
signature.download
else
@signature.read
signature.read
end
pdf.image StringIO.new(signature_file), fit: [max_signature_size , max_signature_size], position: :right
end
@ -62,6 +70,6 @@ prawn_document(margin: [top_margin, right_margin, bottom_margin, left_margin], p
pdf.repeat(:all) do
pdf.move_cursor_to footer_height - 10
pdf.fill_color grey
pdf.text @footer, align: :center, size: 8
pdf.text footer, align: :center, size: 8
end
end

View file

@ -77,7 +77,7 @@
.row
.col-md-6
%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' }
\-

View file

@ -5,7 +5,7 @@
%strong Logo
%p
- if field.data.logo?
- if field.data.logo.attached?
= image_tag field.data.logo_url
- else
Aucun
@ -20,7 +20,7 @@
%strong Signature
%p
- if field.data.signature?
- if field.data.signature.attached?
= image_tag field.data.signature_url
- else
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
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
context 'if an attestation template does not exist on the procedure' do
let(:attestation_template) { nil }
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
context 'if an attestation template exists on the procedure' do
@ -48,18 +48,18 @@ describe Admin::AttestationTemplatesController, type: :controller do
end
it { expect(subject.status).to eq(200) }
it { expect(assigns).to include(upload_params.stringify_keys) }
it { expect(assigns[:created_at]).to eq(Time.zone.now) }
it { expect(assigns(:logo).download).to eq(logo2.read) }
it { expect(assigns(:signature).download).to eq(signature2.read) }
it { expect(assigns(:attestation)).to include(upload_params) }
it { expect(assigns(:attestation)[:created_at]).to eq(Time.zone.now) }
it { expect(assigns(:attestation)[:logo].download).to eq(logo2.read) }
it { expect(assigns(:attestation)[:signature].download).to eq(signature2.read) }
end
context 'with empty logo' do
it { expect(subject.status).to eq(200) }
it { expect(assigns).to include(upload_params.stringify_keys) }
it { expect(assigns[:created_at]).to eq(Time.zone.now) }
it { expect(assigns(:logo)).to eq(nil) }
it { expect(assigns(:signature)).to eq(nil) }
it { expect(assigns(:attestation)).to include(upload_params) }
it { expect(assigns(:attestation)[:created_at]).to eq(Time.zone.now) }
it { expect(assigns(:attestation)[:logo]).to eq(nil) }
it { expect(assigns(:attestation)[:signature]).to eq(nil) }
end
end
end

View file

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