2019-07-01 15:55:37 +02:00
|
|
|
class ActiveStorage::DownloadableFile
|
2021-11-29 15:43:51 +01:00
|
|
|
# https://edgeapi.rubyonrails.org/classes/ActiveStorage/Blob.html#method-i-download
|
|
|
|
def self.download(attachment:, destination_path:, in_chunk: true)
|
|
|
|
byte_written = 0
|
|
|
|
|
|
|
|
File.open(destination_path, mode: 'wb') do |fd| # we expact a path as string, so we can recreate the file (ex: failure/retry on former existing fd)
|
|
|
|
if in_chunk
|
|
|
|
attachment.download do |chunk|
|
|
|
|
byte_written += fd.write(chunk)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
byte_written = fd.write(attachment.download)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
byte_written
|
|
|
|
end
|
|
|
|
|
2021-09-28 11:23:54 +02:00
|
|
|
def self.create_list_from_dossier(dossier, for_expert = false)
|
2021-04-29 16:16:12 +02:00
|
|
|
dossier_export = PiecesJustificativesService.generate_dossier_export(dossier)
|
2021-09-28 11:23:54 +02:00
|
|
|
pjs = [dossier_export] + PiecesJustificativesService.liste_documents(dossier, for_expert)
|
2021-04-29 17:29:47 +02:00
|
|
|
pjs.map do |piece_justificative|
|
2019-07-01 15:55:37 +02:00
|
|
|
[
|
2020-07-20 11:14:34 +02:00
|
|
|
piece_justificative,
|
2021-04-29 17:29:47 +02:00
|
|
|
"dossier-#{dossier.id}/#{self.timestamped_filename(piece_justificative)}"
|
2019-07-01 15:55:37 +02:00
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
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/'
|
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
|
|
|
def using_local_backend?
|
|
|
|
[:local, :local_test, :test].include?(Rails.application.config.active_storage.service)
|
|
|
|
end
|
|
|
|
end
|