Merge pull request #4256 from tchak/rename-active-storage-files-setep-1

Remove carrierwave uploaders (Step 1)
This commit is contained in:
Paul Chavard 2019-09-10 10:56:09 +02:00 committed by GitHub
commit a32ac46492
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 111 additions and 764 deletions

View file

@ -48,19 +48,15 @@ class Admin::AttestationTemplatesController < AdminController
# In a case of a preview, when the user does not change its images, # 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 # the images are not uploaded and thus should be retrieved from previous
# attestation_template # attestation_template
@logo = activated_attestation_params[:logo_active_storage] || @procedure.attestation_template&.proxy_logo @logo = activated_attestation_params[:logo] || @procedure.attestation_template&.proxy_logo
@signature = activated_attestation_params[:signature_active_storage] || @procedure.attestation_template&.proxy_signature @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 attestation_template = @procedure.attestation_template
attestation_template.logo.purge_later
if attestation_template.logo.present?
attestation_template.remove_logo!
attestation_template.save
end
attestation_template.logo_active_storage.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'
@ -69,11 +65,7 @@ class Admin::AttestationTemplatesController < AdminController
def delete_signature def delete_signature
attestation_template = @procedure.attestation_template attestation_template = @procedure.attestation_template
attestation_template.signature.purge_later
if attestation_template.signature.present?
attestation_template.remove_signature!
attestation_template.save
end
attestation_template.signature_active_storage.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'
@ -93,10 +85,10 @@ class Admin::AttestationTemplatesController < AdminController
signature_file = params['attestation_template'].delete('signature') signature_file = params['attestation_template'].delete('signature')
if logo_file.present? if logo_file.present?
@activated_attestation_params[:logo_active_storage] = uninterlaced_png(logo_file) @activated_attestation_params[:logo] = uninterlaced_png(logo_file)
end end
if signature_file.present? if signature_file.present?
@activated_attestation_params[:signature_active_storage] = uninterlaced_png(signature_file) @activated_attestation_params[:signature] = uninterlaced_png(signature_file)
end end
end end

View file

@ -247,10 +247,7 @@ class Admin::ProceduresController < AdminController
end end
def delete_logo def delete_logo
if @procedure.logo @procedure.logo.purge_later
@procedure.remove_logo!
@procedure.save
end
@procedure.logo_active_storage.purge_later @procedure.logo_active_storage.purge_later
flash.notice = 'le logo a bien été supprimé' flash.notice = 'le logo a bien été supprimé'
@ -284,9 +281,6 @@ class Admin::ProceduresController < AdminController
else else
params.require(:procedure).permit(*editable_params, :duree_conservation_dossiers_dans_ds, :duree_conservation_dossiers_hors_ds, :for_individual, :path) params.require(:procedure).permit(*editable_params, :duree_conservation_dossiers_dans_ds, :duree_conservation_dossiers_hors_ds, :for_individual, :path)
end end
if permited_params[:logo]
permited_params[:logo_active_storage] = permited_params.delete(:logo)
end
permited_params permited_params
end end
end end

View file

@ -8,21 +8,19 @@ class ApplicationMailer < ActionMailer::Base
def attach_logo(procedure) def attach_logo(procedure)
return nil if !procedure.logo? return nil if !procedure.logo?
begin if procedure.logo.attached?
if procedure.logo_active_storage.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 logo_filename = procedure.logo_active_storage.filename.to_s
attachments.inline[logo_filename] = procedure.logo_active_storage.download attachments.inline[logo_filename] = procedure.logo_active_storage.download
else
logo_filename = procedure.logo.filename
attachments.inline[logo_filename] = procedure.logo.read
end end
attachments[logo_filename].url
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)
Raven.capture_exception(e) Raven.capture_exception(e)
nil nil
end end
end
end end

View file

@ -1,18 +1,16 @@
class Attestation < ApplicationRecord class Attestation < ApplicationRecord
self.ignored_columns = ['pdf']
belongs_to :dossier belongs_to :dossier
mount_uploader :pdf, AttestationUploader has_one_attached :pdf
has_one_attached :pdf_active_storage has_one_attached :pdf_active_storage
def pdf_url def pdf_url
if pdf_active_storage.attached? 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) Rails.application.routes.url_helpers.url_for(pdf_active_storage)
elsif Rails.application.secrets.fog[:enabled]
RemoteDownloader.new(pdf.path).url
elsif pdf&.url
# FIXME: this is horrible but used only in dev and will be removed after migration
File.join(LOCAL_DOWNLOAD_URL, pdf.url)
end end
end end
end end

View file

@ -1,13 +1,14 @@
class AttestationTemplate < ApplicationRecord class AttestationTemplate < ApplicationRecord
self.ignored_columns = ['logo', 'signature']
include ActionView::Helpers::NumberHelper include ActionView::Helpers::NumberHelper
include TagsSubstitutionConcern include TagsSubstitutionConcern
belongs_to :procedure belongs_to :procedure
mount_uploader :logo, AttestationTemplateLogoUploader has_one_attached :logo
mount_uploader :signature, AttestationTemplateSignatureUploader
has_one_attached :logo_active_storage has_one_attached :logo_active_storage
has_one_attached :signature
has_one_attached :signature_active_storage has_one_attached :signature_active_storage
validates :footer, length: { maximum: 190 } validates :footer, length: { maximum: 190 }
@ -16,7 +17,7 @@ class AttestationTemplate < ApplicationRecord
def attestation_for(dossier) def attestation_for(dossier)
attestation = Attestation.new(title: replace_tags(title, dossier)) attestation = Attestation.new(title: replace_tags(title, dossier))
attestation.pdf_active_storage.attach( attestation.pdf.attach(
io: build_pdf(dossier), io: build_pdf(dossier),
filename: "attestation-dossier-#{dossier.id}.pdf", filename: "attestation-dossier-#{dossier.id}.pdf",
content_type: 'application/pdf', content_type: 'application/pdf',
@ -45,80 +46,82 @@ class AttestationTemplate < ApplicationRecord
def dup def dup
attestation_template = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated) attestation_template = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated)
if logo_active_storage.attached? if logo.attached?
attestation_template.logo_active_storage.attach( attestation_template.logo.attach(
io: StringIO.new(logo.download),
filename: logo.filename.to_s,
content_type: logo.content_type,
# 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), io: StringIO.new(logo_active_storage.download),
filename: logo_active_storage.filename, filename: logo_active_storage.filename.to_s,
content_type: logo_active_storage.content_type, content_type: logo_active_storage.content_type,
# 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.present?
CopyCarrierwaveFile::CopyFileService.new(self, attestation_template, :logo).set_file
end end
if signature_active_storage.attached? if signature.attached?
attestation_template.signature_active_storage.attach( attestation_template.signature.attach(
io: StringIO.new(signature.download),
filename: signature.filename.to_s,
content_type: signature.content_type,
# 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), io: StringIO.new(signature_active_storage.download),
filename: signature_active_storage.filename, filename: signature_active_storage.filename.to_s,
content_type: signature_active_storage.content_type, content_type: signature_active_storage.content_type,
# 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.present?
CopyCarrierwaveFile::CopyFileService.new(self, attestation_template, :signature).set_file
end end
attestation_template attestation_template
end end
def logo? def logo?
logo_active_storage.attached? || logo.present? logo.attached? || logo_active_storage.attached?
end end
def signature? def signature?
signature_active_storage.attached? || signature.present? signature.attached? || signature_active_storage.attached?
end end
def logo_url def logo_url
if logo_active_storage.attached? 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) Rails.application.routes.url_helpers.url_for(logo_active_storage)
elsif logo.present?
if Rails.application.secrets.fog[:enabled]
RemoteDownloader.new(logo.path).url
elsif logo&.url
# FIXME: this is horrible but used only in dev and will be removed after migration
File.join(LOCAL_DOWNLOAD_URL, logo.url)
end
end end
end end
def signature_url def signature_url
if signature_active_storage.attached? 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) Rails.application.routes.url_helpers.url_for(signature_active_storage)
elsif signature.present?
if Rails.application.secrets.fog[:enabled]
RemoteDownloader.new(signature.path).url
elsif signature&.url
# FIXME: this is horrible but used only in dev and will be removed after migration
File.join(LOCAL_DOWNLOAD_URL, signature.url)
end
end end
end end
def proxy_logo def proxy_logo
if logo_active_storage.attached? if logo.attached?
logo_active_storage
else
logo logo
elsif logo_active_storage.attached?
logo_active_storage
end end
end end
def proxy_signature def proxy_signature
if signature_active_storage.attached? if signature.attached?
signature_active_storage
else
signature signature
elsif signature_active_storage.attached?
signature_active_storage
end end
end end

View file

@ -1,4 +1,6 @@
class Commentaire < ApplicationRecord class Commentaire < ApplicationRecord
self.ignored_columns = ['file']
belongs_to :dossier, inverse_of: :commentaires, touch: true belongs_to :dossier, inverse_of: :commentaires, touch: true
belongs_to :user belongs_to :user

View file

@ -1,6 +1,8 @@
require Rails.root.join('lib', 'percentile') require Rails.root.join('lib', 'percentile')
class Procedure < ApplicationRecord class Procedure < ApplicationRecord
self.ignored_columns = ['logo']
MAX_DUREE_CONSERVATION = 36 MAX_DUREE_CONSERVATION = 36
has_many :types_de_champ, -> { root.public_only.ordered }, inverse_of: :procedure, dependent: :destroy has_many :types_de_champ, -> { root.public_only.ordered }, inverse_of: :procedure, dependent: :destroy
@ -25,6 +27,7 @@ class Procedure < ApplicationRecord
has_one :refused_mail, class_name: "Mails::RefusedMail", dependent: :destroy has_one :refused_mail, class_name: "Mails::RefusedMail", dependent: :destroy
has_one :without_continuation_mail, class_name: "Mails::WithoutContinuationMail", dependent: :destroy has_one :without_continuation_mail, class_name: "Mails::WithoutContinuationMail", dependent: :destroy
has_one_attached :logo
has_one_attached :logo_active_storage has_one_attached :logo_active_storage
has_one_attached :notice has_one_attached :notice
has_one_attached :deliberation has_one_attached :deliberation
@ -32,8 +35,6 @@ class Procedure < ApplicationRecord
accepts_nested_attributes_for :types_de_champ, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true accepts_nested_attributes_for :types_de_champ, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
accepts_nested_attributes_for :types_de_champ_private, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true accepts_nested_attributes_for :types_de_champ_private, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
mount_uploader :logo, ProcedureLogoUploader
default_scope { where(hidden_at: nil) } default_scope { where(hidden_at: nil) }
scope :brouillons, -> { where(aasm_state: :brouillon) } scope :brouillons, -> { where(aasm_state: :brouillon) }
scope :publiees, -> { where(aasm_state: :publiee) } scope :publiees, -> { where(aasm_state: :publiee) }
@ -221,10 +222,6 @@ class Procedure < ApplicationRecord
procedure.test_started_at = nil procedure.test_started_at = nil
procedure.archived_at = nil procedure.archived_at = nil
procedure.published_at = nil procedure.published_at = nil
procedure.logo_secure_token = nil
if logo.present?
procedure.remote_logo_url = self.logo_url
end
procedure.lien_notice = nil procedure.lien_notice = nil
if is_different_admin || from_library if is_different_admin || from_library
@ -264,6 +261,7 @@ class Procedure < ApplicationRecord
if original.is_a?(TypeDeChamp) if original.is_a?(TypeDeChamp)
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_active_storage, 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)
@ -466,21 +464,16 @@ class Procedure < ApplicationRecord
end end
def logo? def logo?
logo.present? || logo_active_storage.attached? logo.attached? || logo_active_storage.attached?
end end
def logo_url def logo_url
if !logo? if logo.attached?
ActionController::Base.helpers.image_url("marianne.svg") Rails.application.routes.url_helpers.url_for(logo)
elsif logo_active_storage.attached? elsif logo_active_storage.attached?
Rails.application.routes.url_helpers.url_for(logo_active_storage) Rails.application.routes.url_helpers.url_for(logo_active_storage)
else else
if Rails.application.secrets.fog[:enabled] ActionController::Base.helpers.image_url("marianne.svg")
RemoteDownloader.new(logo.filename).url
else
# FIXME: this is horrible but used only in dev and will be removed after migration
File.join(LOCAL_DOWNLOAD_URL, logo.url)
end
end end
end end

View file

@ -1,38 +0,0 @@
class AttestationTemplateLogoUploader < BaseUploader
def root
Rails.root.join("public")
end
# Choose what kind of storage to use for this uploader:
if Rails.application.secrets.fog[:enabled]
storage :fog
else
storage :file
end
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
if !Rails.application.secrets.fog[:enabled]
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_whitelist
['jpg', 'jpeg', 'png']
end
def filename
if file.present?
"attestation-template-logo-#{secure_token}.#{file.extension.downcase}"
end
end
private
def secure_token
model.logo_secure_token ||= SecureRandom.uuid
end
end

View file

@ -1,38 +0,0 @@
class AttestationTemplateSignatureUploader < BaseUploader
def root
Rails.root.join("public")
end
# Choose what kind of storage to use for this uploader:
if Rails.application.secrets.fog[:enabled]
storage :fog
else
storage :file
end
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
if !Rails.application.secrets.fog[:enabled]
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_whitelist
['jpg', 'jpeg', 'png']
end
def filename
if file.present?
"attestation-template-signature-#{secure_token}.#{file.extension.downcase}"
end
end
private
def secure_token
model.signature_secure_token ||= SecureRandom.uuid
end
end

View file

@ -1,32 +0,0 @@
class AttestationUploader < BaseUploader
def root
Rails.root.join("public")
end
# Choose what kind of storage to use for this uploader:
if Rails.application.secrets.fog[:enabled]
storage :fog
else
storage :file
end
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
if !Rails.application.secrets.fog[:enabled]
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
def filename
if file.present?
"attestation-#{secure_token}.pdf"
end
end
private
def secure_token
model.content_secure_token ||= SecureRandom.uuid
end
end

View file

@ -1,13 +0,0 @@
class BaseUploader < CarrierWave::Uploader::Base
def cache_dir
Rails.application.secrets.carrierwave[:cache_dir]
end
# https://github.com/carrierwaveuploader/carrierwave/wiki/how-to:-silently-ignore-missing-files-on-destroy-or-overwrite
def remove!
begin
super
rescue Fog::OpenStack::Storage::NotFound
end
end
end

View file

@ -1,41 +0,0 @@
require 'securerandom'
class LocalDownloader
BASE_PATH_DISK = Rails.root.join("public", "downloads")
def initialize(filename, filename_suffix = '')
@filename = filename.to_s
@filename_suffix = filename_suffix.empty? ? '' : "_#{filename_suffix}"
@extension = @filename.split(/[.]/).last
generate_random_base_path!
FileUtils.ln_s @filename, "#{@base_path}/#{@filename_suffix}.#{@extension}"
end
def url
@url ||= File.join(LOCAL_DOWNLOAD_URL, 'downloads', random_folder_name, "#{@filename_suffix}.#{@extension}")
end
protected
attr_accessor :random_folder_name
def generate_random_base_path!
@base_path ||= begin
loop do
self.random_folder_name = SecureRandom.hex
base_path = File.join(BASE_PATH_DISK, self.random_folder_name)
if !File.directory?(BASE_PATH_DISK)
Dir.mkdir(BASE_PATH_DISK)
end
if !File.directory?(base_path)
Dir.mkdir(base_path)
break base_path
end
end
end
end
end

View file

@ -1,49 +0,0 @@
class ProcedureLogoUploader < BaseUploader
def root
Rails.root.join("public")
end
# Choose what kind of storage to use for this uploader:
if Rails.application.secrets.fog[:enabled]
storage :fog
else
storage :file
end
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
if !Rails.application.secrets.fog[:enabled]
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_whitelist
['jpg', 'jpeg', 'png']
end
def filename
if file.present?
if original_filename.present? || model.logo_secure_token
if Rails.application.secrets.fog[:enabled]
filename = "#{model.class.to_s.underscore}-#{secure_token}.#{file.extension&.downcase}"
else
filename = "logo-#{secure_token}.#{file.extension&.downcase}"
end
end
filename
end
end
private
def secure_token
model.logo_secure_token ||= generate_secure_token
end
def generate_secure_token
SecureRandom.uuid
end
end

View file

@ -1,17 +0,0 @@
class RemoteDownloader
def initialize(filename)
@filename = filename
end
def url
if @filename.present?
@url ||= File.join(base_url, CarrierWave::Uploader::Base.fog_directory, @filename)
end
end
protected
def base_url
FOG_BASE_URL
end
end

View file

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

View file

@ -9,7 +9,7 @@
= link_to(instructeur_procedure_path(p)) do = link_to(instructeur_procedure_path(p)) do
.flex .flex
.procedure-logo{ style: p.logo.present? ? "background-image: url(#{p.logo.url})" : nil } .procedure-logo{ style: "background-image: url(#{p.logo_url})" }
.procedure-details .procedure-details
%p.procedure-title %p.procedure-title

View file

@ -4,7 +4,7 @@
.sub-header .sub-header
.container.flex .container.flex
.procedure-logo{ style: @procedure.logo? ? "background-image: url(#{@procedure.logo_url})" : nil, .procedure-logo{ style: "background-image: url(#{@procedure.logo_url})",
role: 'img', 'aria-label': "logo de la démarche #{@procedure.libelle}" } role: 'img', 'aria-label': "logo de la démarche #{@procedure.libelle}" }
.procedure-header .procedure-header

View file

@ -1,6 +1,5 @@
#first-block #first-block
.en-cours .en-cours
- if @procedure.logo?
= image_tag @procedure.logo_url, style: 'width: 30px;' = image_tag @procedure.logo_url, style: 'width: 30px;'
%b %b
= @procedure.libelle = @procedure.libelle

View file

@ -14,11 +14,6 @@
- if commentaire.piece_jointe.attached? - if commentaire.piece_jointe.attached?
.attachment-link .attachment-link
= render partial: "shared/attachment/show", locals: { attachment: commentaire.piece_jointe.attachment } = render partial: "shared/attachment/show", locals: { attachment: commentaire.piece_jointe.attachment }
- elsif commentaire.file.present?
.attachment-link
= link_to commentaire.file_url, class: "button", target: "_blank", rel: "noopener", title: "Télécharger" do
%span.icon.attachment
= commentaire.file_identifier
- if show_reply_button - if show_reply_button
= button_tag type: 'button', class: 'button small message-answer-button', onclick: 'document.querySelector("#commentaire_body").focus()' do = button_tag type: 'button', class: 'button small message-answer-button', onclick: 'document.querySelector("#commentaire_body").focus()' do

View file

@ -1,47 +0,0 @@
module Fog
module ServicesMixin
private
def require_service_provider_library(service, provider)
# Monkey patch to fix https://github.com/fog/fog/issues/4014
# This method exists in fog to load legacy providers that have not yet been extracted to
# their own gem.
# fog-openstack has been extracted to its own gem and does not need this method.
# Furthermore, fog-openstack has recently been refactored in a way that breaks this method.
#
# Therefore, until either fog or fog-openstack fixes the problem, we have to neuter the method.
end
end
end
CarrierWave.configure do |config|
# These permissions will make dir and files available only to the user running
# the servers
config.permissions = 0664
config.directory_permissions = 0775
config.fog_provider = 'fog/openstack'
if ENV['FOG_ENABLED'] == 'enabled'
config.fog_credentials = {
provider: 'OpenStack',
openstack_tenant: Rails.application.secrets.fog[:openstack_tenant],
openstack_api_key: Rails.application.secrets.fog[:openstack_api_key],
openstack_username: Rails.application.secrets.fog[:openstack_username],
openstack_auth_url: Rails.application.secrets.fog[:openstack_auth_url],
openstack_region: Rails.application.secrets.fog[:openstack_region],
openstack_identity_api_version: Rails.application.secrets.fog[:openstack_identity_api_version]
}
end
# This avoids uploaded files from saving to public/ and so
# they will not be available for public (non-authenticated) downloading
config.root = Rails.root
config.cache_dir = Rails.root.join("uploads")
config.fog_public = true
config.fog_directory = Rails.application.secrets.fog[:directory]
end

View file

@ -28,6 +28,3 @@ FAQ_ADMIN_URL = "https://faq.demarches-simplifiees.fr/collection/1-administrateu
COMMENT_TROUVER_MA_DEMARCHE_URL = [FAQ_URL, "article", "59-comment-trouver-ma-demarche"].join("/") COMMENT_TROUVER_MA_DEMARCHE_URL = [FAQ_URL, "article", "59-comment-trouver-ma-demarche"].join("/")
STATUS_PAGE_URL = "https://status.demarches-simplifiees.fr" STATUS_PAGE_URL = "https://status.demarches-simplifiees.fr"
MATOMO_IFRAME_URL = "https://stats.data.gouv.fr/index.php?module=CoreAdminHome&action=optOut&language=fr&&fontColor=333333&fontSize=16px&fontFamily=Muli" MATOMO_IFRAME_URL = "https://stats.data.gouv.fr/index.php?module=CoreAdminHome&action=optOut&language=fr&&fontColor=333333&fontSize=16px&fontFamily=Muli"
# FIXME: This is only used in dev in couple of places and should be removed after PJ migration
LOCAL_DOWNLOAD_URL = "http://#{ENV.fetch('APP_HOST', 'localhost:3000')}"

View file

@ -1,33 +0,0 @@
namespace :'2019_08_20_migrate_procedure_logo' do
task run: :environment do
procedures = Procedure.unscope(where: :hidden_at)
.where.not(logo: nil)
.left_joins(:logo_active_storage_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_active_storage.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

View file

@ -1,97 +0,0 @@
namespace :'2019_08_22_migrate_attestation_files' do
task migrate_attestation_pdf: :environment do
attestations = Attestation.where
.not(pdf: nil)
.left_joins(:pdf_active_storage_attachment)
.where('active_storage_attachments.id IS NULL')
.order(:created_at)
limit = ENV['LIMIT']
if limit
attestations.limit!(limit.to_i)
end
progress = ProgressReport.new(attestations.count)
attestations.find_each do |attestation|
if attestation.pdf.present?
uri = URI.parse(URI.escape(attestation.pdf_url))
response = Typhoeus.get(uri)
if response.success?
filename = attestation.pdf.filename || attestation.pdf_identifier
attestation.pdf_active_storage.attach(
io: StringIO.new(response.body),
filename: filename,
content_type: attestation.pdf.content_type,
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
end
end
progress.inc
end
progress.finish
end
task migrate_attestation_template_logo: :environment do
attestation_templates = AttestationTemplate.where
.not(logo: nil)
.left_joins(:logo_active_storage_attachment)
.where('active_storage_attachments.id IS NULL')
.order(:created_at)
limit = ENV['LIMIT']
if limit
attestation_templates.limit!(limit.to_i)
end
progress = ProgressReport.new(attestation_templates.count)
attestation_templates.find_each do |attestation_template|
if attestation_template.logo.present?
uri = URI.parse(URI.escape(attestation_template.logo_url))
response = Typhoeus.get(uri)
if response.success?
filename = attestation_template.logo.filename || attestation_template.logo_identifier
attestation_template.logo_active_storage.attach(
io: StringIO.new(response.body),
filename: filename,
content_type: attestation_template.logo.content_type,
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
end
end
progress.inc
end
progress.finish
end
task migrate_attestation_template_signature: :environment do
attestation_templates = AttestationTemplate.where
.not(signature: nil)
.left_joins(:signature_active_storage_attachment)
.where('active_storage_attachments.id IS NULL')
.order(:created_at)
limit = ENV['LIMIT']
if limit
attestation_templates.limit!(limit.to_i)
end
progress = ProgressReport.new(attestation_templates.count)
attestation_templates.find_each do |attestation_template|
if attestation_template.signature.present?
uri = URI.parse(URI.escape(attestation_template.signature_url))
response = Typhoeus.get(uri)
if response.success?
filename = attestation_template.signature.filename || attestation_template.signature_identifier
attestation_template.signature_active_storage.attach(
io: StringIO.new(response.body),
filename: filename,
content_type: attestation_template.signature.content_type,
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
end
end
progress.inc
end
progress.finish
end
end

View file

@ -0,0 +1,14 @@
namespace :after_party do
desc 'Deployment task: rename_active_storage_attachments'
task rename_active_storage_attachments: :environment do
puts "Running deploy task 'rename_active_storage_attachments'"
ActiveStorage::Attachment.where(name: 'logo_active_storage').update_all(name: 'logo')
ActiveStorage::Attachment.where(name: 'signature_active_storage').update_all(name: 'signature')
ActiveStorage::Attachment.where(name: 'pdf_active_storage').update_all(name: 'pdf')
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord.create version: '20190828130014'
end
end

View file

@ -48,8 +48,8 @@ describe Admin::AttestationTemplatesController, type: :controller 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).to include(upload_params.stringify_keys) }
it { expect(assigns[:created_at]).to eq(Time.zone.now) } it { expect(assigns[:created_at]).to eq(Time.zone.now) }
it { expect(assigns(:logo).read).to eq(logo.read) } it { expect(assigns(:logo).download).to eq(logo2.read) }
it { expect(assigns(:signature).read).to eq(signature.read) } it { expect(assigns(:signature).download).to eq(signature2.read) }
after { procedure.attestation_template.destroy } after { procedure.attestation_template.destroy }
end end
end end
@ -87,8 +87,8 @@ describe Admin::AttestationTemplatesController, type: :controller do
it { expect(procedure.attestation_template).to have_attributes(attestation_params) } it { expect(procedure.attestation_template).to have_attributes(attestation_params) }
it { expect(procedure.attestation_template.activated).to be true } it { expect(procedure.attestation_template.activated).to be true }
it { expect(procedure.attestation_template.logo_active_storage.download).to eq(logo2.read) } it { expect(procedure.attestation_template.logo.download).to eq(logo2.read) }
it { expect(procedure.attestation_template.signature_active_storage.download).to eq(signature2.read) } it { expect(procedure.attestation_template.signature.download).to eq(signature2.read) }
it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) } it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) }
it { expect(flash.notice).to eq("L'attestation a bien été sauvegardée") } it { expect(flash.notice).to eq("L'attestation a bien été sauvegardée") }
@ -130,8 +130,8 @@ describe Admin::AttestationTemplatesController, type: :controller do
end end
it { expect(procedure.attestation_template).to have_attributes(attestation_params) } it { expect(procedure.attestation_template).to have_attributes(attestation_params) }
it { expect(procedure.attestation_template.logo_active_storage.download).to eq(logo2.read) } it { expect(procedure.attestation_template.logo.download).to eq(logo2.read) }
it { expect(procedure.attestation_template.signature_active_storage.download).to eq(signature2.read) } it { expect(procedure.attestation_template.signature.download).to eq(signature2.read) }
it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) } it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) }
it { expect(flash.notice).to eq("L'attestation a bien été modifiée") } it { expect(flash.notice).to eq("L'attestation a bien été modifiée") }

View file

@ -7,11 +7,6 @@ FactoryBot.define do
end end
trait :with_files do trait :with_files do
logo_active_storage { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') }
signature_active_storage { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') }
end
trait :with_legacy_files do
logo { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') } logo { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') }
signature { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') } signature { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') }
end end

View file

@ -53,10 +53,6 @@ FactoryBot.define do
end end
trait :with_logo do trait :with_logo do
logo_active_storage { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') }
end
trait :with_legacy_logo do
logo { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') } logo { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') }
end end

View file

@ -1,60 +0,0 @@
describe '2019_08_20_migrate_procedure_logo.rake' do
let(:rake_task) { Rake::Task['2019_08_20_migrate_procedure_logo:run'] }
let(:procedures) do
[
create(:procedure),
create(:procedure, :with_legacy_logo),
create(:procedure, :with_legacy_logo)
]
end
let(:run_task) do
rake_task.invoke
procedures.each(&:reload)
end
before do
procedures.each do |procedure|
if procedure.logo.present?
stub_request(:get, procedure.logo_url)
.to_return(status: 200, body: File.read(procedure.logo.path))
end
end
end
after do
ENV['LIMIT'] = nil
rake_task.reenable
end
it 'should migrate logo' do
expect(procedures.map(&:logo_active_storage).map(&:attached?)).to eq([false, false, false])
run_task
expect(Procedure.where(logo: nil).count).to eq(1)
expect(procedures.map(&:logo_active_storage).map(&:attached?)).to eq([false, true, true])
end
it 'should migrate logo within limit' do
expect(procedures.map(&:logo_active_storage).map(&:attached?)).to eq([false, false, false])
ENV['LIMIT'] = '1'
run_task
expect(Procedure.where(logo: nil).count).to eq(1)
expect(procedures.map(&:logo_active_storage).map(&:attached?)).to eq([false, true, false])
end
context 'when a procedure is hidden' do
let(:hidden_procedure) { create(:procedure, :hidden, :with_legacy_logo) }
let(:procedures) { [hidden_procedure] }
it 'should migrate logo' do
run_task
expect(hidden_procedure.logo_active_storage.attached?).to be true
end
end
end

View file

@ -1,128 +0,0 @@
describe '2019_08_22_migrate_attestation_files.rake' do
let(:rake_task) { Rake::Task["2019_08_22_migrate_attestation_files:#{sub_task}"] }
let(:run_task) do
rake_task.invoke
models.each(&:reload)
end
after do
ENV['LIMIT'] = nil
rake_task.reenable
end
context 'attestation' do
let(:models) do
[
create(:attestation, created_at: 3.days.ago),
create(:attestation, :with_legacy_pdf, created_at: 2.days.ago),
create(:attestation, :with_legacy_pdf, created_at: 1.day.ago)
]
end
before do
first_attestation = models[0]
expect(first_attestation.pdf.present?).to be_falsey
expect(first_attestation.read_attribute(:pdf)).to be_nil
models.each do |attestation|
if attestation.pdf.present?
stub_request(:get, attestation.pdf_url)
.to_return(status: 200, body: File.read(attestation.pdf.path))
end
end
end
context 'pdf' do
let(:sub_task) { 'migrate_attestation_pdf' }
it 'should migrate pdf' do
expect(models.map(&:pdf_active_storage).map(&:attached?)).to eq([false, false, false])
run_task
expect(Attestation.where(pdf: nil).count).to eq(1)
expect(models.map(&:pdf_active_storage).map(&:attached?)).to eq([false, true, true])
end
it 'should migrate pdf within limit' do
expect(models.map(&:pdf_active_storage).map(&:attached?)).to eq([false, false, false])
ENV['LIMIT'] = '1'
run_task
expect(Attestation.where(pdf: nil).count).to eq(1)
expect(models.map(&:pdf_active_storage).map(&:attached?)).to eq([false, true, false])
end
end
end
context 'attestation_templates' do
let(:models) do
[
create(:attestation_template),
create(:attestation_template, :with_legacy_files),
create(:attestation_template, :with_legacy_files)
]
end
before do
models.each do |attestation_template|
if attestation_template.logo.present?
stub_request(:get, attestation_template.logo_url)
.to_return(status: 200, body: File.read(attestation_template.logo.path))
end
if attestation_template.signature.present?
stub_request(:get, attestation_template.signature_url)
.to_return(status: 200, body: File.read(attestation_template.signature.path))
end
end
end
context 'logo' do
let(:sub_task) { 'migrate_attestation_template_logo' }
it 'should migrate logo' do
expect(models.map(&:logo_active_storage).map(&:attached?)).to eq([false, false, false])
run_task
expect(AttestationTemplate.where(logo: nil).count).to eq(1)
expect(models.map(&:logo_active_storage).map(&:attached?)).to eq([false, true, true])
end
it 'should migrate logo within limit' do
expect(models.map(&:logo_active_storage).map(&:attached?)).to eq([false, false, false])
ENV['LIMIT'] = '1'
run_task
expect(AttestationTemplate.where(logo: nil).count).to eq(1)
expect(models.map(&:logo_active_storage).map(&:attached?)).to eq([false, true, false])
end
end
context 'signature' do
let(:sub_task) { 'migrate_attestation_template_signature' }
it 'should migrate signature' do
expect(models.map(&:signature_active_storage).map(&:attached?)).to eq([false, false, false])
run_task
expect(AttestationTemplate.where(signature: nil).count).to eq(1)
expect(models.map(&:signature_active_storage).map(&:attached?)).to eq([false, true, true])
end
it 'should migrate signature within limit' do
expect(models.map(&:signature_active_storage).map(&:attached?)).to eq([false, false, false])
ENV['LIMIT'] = '1'
run_task
expect(AttestationTemplate.where(signature: nil).count).to eq(1)
expect(models.map(&:signature_active_storage).map(&:attached?)).to eq([false, true, false])
end
end
end
end

View file

@ -57,13 +57,11 @@ describe AttestationTemplate, type: :model do
describe 'dup' do describe 'dup' do
before do before do
@logo = File.open('spec/fixtures/files/white.png') @logo = Rack::Test::UploadedFile.new('spec/fixtures/files/white.png', 'image/png')
@signature = File.open('spec/fixtures/files/black.png') @signature = Rack::Test::UploadedFile.new('spec/fixtures/files/black.png', 'image/png')
end end
after do after do
@logo.close
@signature.close
subject.destroy subject.destroy
end end
@ -75,17 +73,17 @@ describe AttestationTemplate, type: :model do
it { is_expected.to have_attributes(attributes) } it { is_expected.to have_attributes(attributes) }
it { is_expected.to have_attributes(id: nil) } it { is_expected.to have_attributes(id: nil) }
it { expect(subject.logo.file).to be_nil } it { expect(subject.logo.attached?).to be_falsey }
end end
context 'with an attestation with images' do context 'with an attestation with images' do
let(:attributes) { { logo: @logo, signature: @signature } } let(:attributes) { { logo: @logo, signature: @signature } }
it { expect(subject.logo.file.file).not_to eq(attestation_template.logo.file.file) } it { expect(subject.logo.blob).not_to eq(attestation_template.logo.blob) }
it { expect(subject.logo.file.read).to eq(attestation_template.logo.file.read) } it { expect(subject.logo.download).to eq(attestation_template.logo.download) }
it { expect(subject.signature.file.file).not_to eq(attestation_template.signature.file.file) } it { expect(subject.signature.blob).not_to eq(attestation_template.signature.blob) }
it { expect(subject.signature.file.read).to eq(attestation_template.signature.file.read) } it { expect(subject.signature.download).to eq(attestation_template.signature.download) }
end end
end end
@ -113,14 +111,10 @@ describe AttestationTemplate, type: :model do
end end
before do before do
@logo = File.open('spec/fixtures/files/white.png')
@signature = File.open('spec/fixtures/files/black.png')
Timecop.freeze(Time.zone.now) Timecop.freeze(Time.zone.now)
end end
after do after do
@logo.close
@signature.close
Timecop.return Timecop.return
end end

View file

@ -139,8 +139,6 @@ RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods config.include FactoryBot::Syntax::Methods
config.before(:each) do config.before(:each) do
allow_any_instance_of(ProcedureLogoUploader).to receive(:generate_secure_token).and_return("3dbb3535-5388-4a37-bc2d-778327b9f998")
Flipflop::FeatureSet.current.test!.reset! Flipflop::FeatureSet.current.test!.reset!
end end

View file

@ -1,11 +0,0 @@
require 'spec_helper'
describe BaseUploader do
let(:uploader) { described_class.new }
describe '#cache_dir' do
subject { uploader.cache_dir }
it { is_expected.to eq '/tmp/tps-test-cache' }
end
end

View file

@ -1,17 +0,0 @@
require 'spec_helper'
describe RemoteDownloader do
let(:filename) { 'file_name.pdf' }
subject { described_class.new filename }
describe '#url' do
it { expect(subject.url).to eq 'https://static.demarches-simplifiees.fr/tps_dev/file_name.pdf' }
context 'no filename' do
let(:filename) { nil }
it { expect(subject.url).to be_nil }
end
end
end