2021-04-29 17:29:47 +02:00
|
|
|
require 'tempfile'
|
|
|
|
|
|
|
|
class ProcedureArchiveService
|
2021-11-29 15:43:51 +01:00
|
|
|
ARCHIVE_CREATION_DIR = ENV.fetch('ARCHIVE_CREATION_DIR') { '/tmp' }
|
|
|
|
|
2021-04-29 17:29:47 +02:00
|
|
|
def initialize(procedure)
|
|
|
|
@procedure = procedure
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_pending_archive(instructeur, type, month = nil)
|
|
|
|
groupe_instructeurs = instructeur
|
|
|
|
.groupe_instructeurs
|
|
|
|
.where(procedure: @procedure)
|
|
|
|
|
|
|
|
Archive.find_or_create_archive(type, month, groupe_instructeurs)
|
|
|
|
end
|
|
|
|
|
2022-03-30 16:19:14 +02:00
|
|
|
def make_and_upload_archive(archive, instructeur)
|
2022-01-13 12:35:07 +01:00
|
|
|
dossiers = Dossier.visible_by_administration
|
|
|
|
.where(groupe_instructeur: archive.groupe_instructeurs)
|
|
|
|
|
|
|
|
dossiers = if archive.time_span_type == 'everything'
|
|
|
|
dossiers.state_termine
|
2021-04-29 17:29:47 +02:00
|
|
|
else
|
2022-04-05 13:39:37 +02:00
|
|
|
dossiers.processed_in_month(archive.month)
|
2021-04-29 17:29:47 +02:00
|
|
|
end
|
|
|
|
|
2022-04-04 17:45:10 +02:00
|
|
|
attachments = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers)
|
|
|
|
|
2022-03-18 15:00:58 +01:00
|
|
|
download_and_zip(archive, attachments) do |zip_filepath|
|
2022-02-28 13:16:27 +01:00
|
|
|
ArchiveUploader.new(procedure: @procedure, archive: archive, filepath: zip_filepath)
|
|
|
|
.upload
|
2021-04-29 17:29:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.procedure_files_size(procedure)
|
|
|
|
dossiers_files_size(procedure.dossiers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.dossiers_files_size(dossiers)
|
|
|
|
dossiers.map do |dossier|
|
|
|
|
liste_pieces_justificatives_for_archive(dossier).sum(&:byte_size)
|
|
|
|
end.sum
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-03-18 15:00:58 +01:00
|
|
|
def download_and_zip(archive, attachments, &block)
|
2021-11-29 15:43:51 +01:00
|
|
|
Dir.mktmpdir(nil, ARCHIVE_CREATION_DIR) do |tmp_dir|
|
2022-03-18 15:00:58 +01:00
|
|
|
archive_dir = File.join(tmp_dir, zip_root_folder(archive))
|
|
|
|
zip_path = File.join(ARCHIVE_CREATION_DIR, "#{zip_root_folder(archive)}.zip")
|
2021-11-29 15:43:51 +01:00
|
|
|
|
|
|
|
begin
|
|
|
|
FileUtils.remove_entry_secure(archive_dir) if Dir.exist?(archive_dir)
|
|
|
|
Dir.mkdir(archive_dir)
|
|
|
|
|
2021-12-22 16:03:36 +01:00
|
|
|
download_manager = DownloadManager::ProcedureAttachmentsExport.new(@procedure, attachments, archive_dir)
|
|
|
|
download_manager.download_all
|
2021-11-29 15:43:51 +01:00
|
|
|
|
2021-12-13 15:21:35 +01:00
|
|
|
Dir.chdir(tmp_dir) do
|
2021-12-13 14:35:13 +01:00
|
|
|
File.delete(zip_path) if File.exist?(zip_path)
|
2022-03-18 15:00:58 +01:00
|
|
|
system 'zip', '-0', '-r', zip_path, zip_root_folder(archive)
|
2021-12-13 15:21:35 +01:00
|
|
|
end
|
2021-11-29 15:43:51 +01:00
|
|
|
yield(zip_path)
|
|
|
|
ensure
|
|
|
|
FileUtils.remove_entry_secure(archive_dir) if Dir.exist?(archive_dir)
|
|
|
|
File.delete(zip_path) if File.exist?(zip_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-18 15:00:58 +01:00
|
|
|
def zip_root_folder(archive)
|
|
|
|
"procedure-#{@procedure.id}-#{archive.id}"
|
2021-08-04 15:08:14 +02:00
|
|
|
end
|
|
|
|
|
2021-04-29 17:29:47 +02:00
|
|
|
def self.attachments_from_champs_piece_justificative(champs)
|
|
|
|
champs
|
|
|
|
.filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) }
|
|
|
|
.map(&:piece_justificative_file)
|
2021-12-13 14:35:13 +01:00
|
|
|
.filter(&:attached?)
|
2021-04-29 17:29:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.liste_pieces_justificatives_for_archive(dossier)
|
|
|
|
champs_blocs_repetables = dossier.champs
|
|
|
|
.filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:repetition) }
|
|
|
|
.flat_map(&:champs)
|
|
|
|
|
|
|
|
attachments_from_champs_piece_justificative(champs_blocs_repetables + dossier.champs)
|
|
|
|
end
|
|
|
|
end
|