2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-09-07 15:13:58 +02:00
|
|
|
require 'fog/openstack'
|
|
|
|
|
2021-12-13 14:35:13 +01:00
|
|
|
class ActiveStorage::DownloadableFile
|
2024-03-08 17:14:21 +01:00
|
|
|
def self.create_list_from_dossiers(dossiers:, user_profile:, export_template: nil)
|
|
|
|
pj_service = PiecesJustificativesService.new(user_profile:, export_template:)
|
2024-02-07 17:33:34 +01:00
|
|
|
|
2024-05-17 17:36:40 +02:00
|
|
|
files = []
|
|
|
|
DossierPreloader.new(dossiers).in_batches_with_block do |loaded_dossiers|
|
|
|
|
files += pj_service.generate_dossiers_export(loaded_dossiers) + pj_service.liste_documents(loaded_dossiers)
|
|
|
|
end
|
2024-05-17 16:47:30 +02:00
|
|
|
|
2024-05-17 17:36:40 +02:00
|
|
|
files
|
2022-04-04 17:45:10 +02:00
|
|
|
end
|
|
|
|
|
2022-09-05 11:20:30 +02:00
|
|
|
def self.cleanup_list_from_dossier(files)
|
|
|
|
if Rails.application.config.active_storage.service != :openstack
|
|
|
|
return files
|
|
|
|
end
|
|
|
|
|
|
|
|
files.filter do |file, _filename|
|
2022-11-18 11:30:21 +01:00
|
|
|
if file.is_a?(ActiveStorage::FakeAttachment)
|
2022-09-05 11:20:30 +02:00
|
|
|
true
|
|
|
|
else
|
|
|
|
service = file.blob.service
|
|
|
|
begin
|
|
|
|
client.head_object(service.container, file.blob.key)
|
|
|
|
true
|
|
|
|
rescue Fog::OpenStack::Storage::NotFound
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-01 15:55:37 +02:00
|
|
|
private
|
|
|
|
|
2023-09-07 15:13:58 +02:00
|
|
|
def self.client
|
|
|
|
credentials = Rails.application.config.active_storage
|
|
|
|
.service_configurations['openstack']['credentials']
|
|
|
|
|
|
|
|
Fog::OpenStack::Storage.new(credentials)
|
|
|
|
end
|
|
|
|
|
2022-04-04 23:40:20 +02:00
|
|
|
def self.bill_and_path(bill)
|
|
|
|
[
|
|
|
|
bill,
|
|
|
|
"bills/#{self.timestamped_filename(bill)}"
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2022-04-04 23:00:54 +02:00
|
|
|
def self.pj_and_path(dossier_id, pj)
|
2022-04-04 22:16:33 +02:00
|
|
|
[
|
|
|
|
pj,
|
2022-04-04 23:00:54 +02:00
|
|
|
"dossier-#{dossier_id}/#{self.timestamped_filename(pj)}"
|
2022-04-04 22:16:33 +02:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2020-12-08 16:34:38 +01:00
|
|
|
def self.timestamped_filename(attachment)
|
2020-07-20 17:20:12 +02:00
|
|
|
# we pad the original file name with a timestamp
|
|
|
|
# and a short id in order to help identify multiple versions and avoid name collisions
|
2020-12-08 16:34:38 +01:00
|
|
|
folder = self.folder(attachment)
|
|
|
|
extension = File.extname(attachment.filename.to_s)
|
|
|
|
basename = File.basename(attachment.filename.to_s, extension)
|
|
|
|
timestamp = attachment.created_at.strftime("%d-%m-%Y-%H-%M")
|
|
|
|
id = attachment.id % 10000
|
2020-07-20 11:22:56 +02:00
|
|
|
|
2021-04-29 17:29:47 +02:00
|
|
|
[folder, "#{basename}-#{timestamp}-#{id}#{extension}"].join
|
2020-12-08 16:34:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.folder(attachment)
|
2021-04-29 17:29:47 +02:00
|
|
|
if attachment.name == 'pdf_export_for_instructeur'
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
|
2020-12-08 16:34:38 +01:00
|
|
|
case attachment.record_type
|
|
|
|
when 'Dossier'
|
2021-04-29 17:29:47 +02:00
|
|
|
'dossier/'
|
2020-12-08 16:34:38 +01:00
|
|
|
when 'DossierOperationLog', 'BillSignature'
|
2021-04-29 17:29:47 +02:00
|
|
|
'horodatage/'
|
2020-12-08 16:34:38 +01:00
|
|
|
when 'Commentaire'
|
2021-04-29 17:29:47 +02:00
|
|
|
'messagerie/'
|
2023-10-03 17:07:25 +02:00
|
|
|
when 'Avis'
|
|
|
|
'avis/'
|
2020-12-08 16:34:38 +01:00
|
|
|
else
|
2021-04-29 17:29:47 +02:00
|
|
|
'pieces_justificatives/'
|
2020-12-08 16:34:38 +01:00
|
|
|
end
|
2020-07-20 11:22:56 +02:00
|
|
|
end
|
2019-07-01 15:55:37 +02:00
|
|
|
end
|