2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-28 13:16:27 +01:00
|
|
|
class ArchiveUploader
|
|
|
|
# see: https://docs.ovh.com/fr/storage/pcs/capabilities-and-limitations/#max_file_size-5368709122-5gb
|
|
|
|
# officialy it's 5Gb. but let's avoid to reach the exact spot of the limit
|
|
|
|
# when file size is bigger, active storage expects the chunks + a manifest.
|
|
|
|
MAX_FILE_SIZE_FOR_BACKEND_BEFORE_CHUNKING = ENV.fetch('ACTIVE_STORAGE_FILE_SIZE_THRESHOLD_BEFORE_CUSTOM_UPLOAD') { 4.gigabytes }.to_i
|
|
|
|
|
2022-04-08 17:09:22 +02:00
|
|
|
def upload(archive)
|
2022-02-28 13:16:27 +01:00
|
|
|
uploaded_blob = create_and_upload_blob
|
2023-08-18 15:51:11 +02:00
|
|
|
begin
|
|
|
|
archive.file.purge if archive.file.attached?
|
|
|
|
rescue ActiveStorage::FileNotFoundError
|
|
|
|
archive.file.destroy
|
|
|
|
archive.file.detach
|
|
|
|
end
|
|
|
|
archive.reload
|
|
|
|
uploaded_blob.reload
|
2023-10-03 11:21:12 +02:00
|
|
|
archive.file.attach(uploaded_blob.signed_id) # attaching a blob directly might run identify/virus scanner and wipe it
|
2022-02-28 13:16:27 +01:00
|
|
|
end
|
|
|
|
|
2022-04-08 17:09:22 +02:00
|
|
|
def blob
|
|
|
|
create_and_upload_blob
|
|
|
|
end
|
|
|
|
|
2022-02-28 13:16:27 +01:00
|
|
|
private
|
|
|
|
|
2022-04-08 17:09:22 +02:00
|
|
|
attr_reader :procedure, :filename, :filepath
|
2022-02-28 13:16:27 +01:00
|
|
|
|
|
|
|
def create_and_upload_blob
|
|
|
|
if active_storage_service_local? || File.size(filepath) < MAX_FILE_SIZE_FOR_BACKEND_BEFORE_CHUNKING
|
|
|
|
upload_with_active_storage
|
|
|
|
else
|
|
|
|
upload_with_chunking_wrapper
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def active_storage_service_local?
|
|
|
|
Rails.application.config.active_storage.service == :local
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_with_active_storage
|
2023-10-10 10:20:47 +02:00
|
|
|
params = blob_default_params(filepath).merge(io: File.open(filepath))
|
2022-02-28 13:16:27 +01:00
|
|
|
blob = ActiveStorage::Blob.create_and_upload!(**params)
|
|
|
|
return blob
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_with_chunking_wrapper
|
2024-02-05 12:24:03 +01:00
|
|
|
params = blob_default_params(filepath).merge(byte_size: File.size(filepath),
|
2022-02-28 13:16:27 +01:00
|
|
|
checksum: Digest::SHA256.file(filepath).hexdigest)
|
|
|
|
blob = ActiveStorage::Blob.create_before_direct_upload!(**params)
|
2022-04-05 15:05:22 +02:00
|
|
|
if retryable_syscall_to_custom_uploader(blob)
|
2022-02-28 13:16:27 +01:00
|
|
|
return blob
|
|
|
|
else
|
|
|
|
blob.purge
|
2022-04-05 15:05:22 +02:00
|
|
|
fail "custom archive attachment failed twice, retry later"
|
2022-02-28 13:16:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# keeps consistency between ActiveStorage api calls (otherwise archives are not storaged in '/archives') :
|
|
|
|
# - create_and_upload, blob is attached by active storage
|
|
|
|
# - upload_with_chunking_wrapper, blob is attached by custom script
|
|
|
|
def blob_default_params(filepath)
|
|
|
|
{
|
|
|
|
key: namespaced_object_key,
|
2022-04-08 17:09:22 +02:00
|
|
|
filename: filename,
|
2022-02-28 13:16:27 +01:00
|
|
|
content_type: 'application/zip',
|
2023-10-10 10:20:47 +02:00
|
|
|
metadata: { analyzed: true, identified: true, virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
2022-02-28 13:16:27 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# explicitely memoize so it keeps its consistency across many calls (Ex: retry)
|
|
|
|
def namespaced_object_key
|
|
|
|
@namespaced_object_key ||= "archives/#{Date.today.strftime("%Y-%m-%d")}/#{SecureRandom.uuid}"
|
|
|
|
end
|
|
|
|
|
2022-04-05 15:05:22 +02:00
|
|
|
def retryable_syscall_to_custom_uploader(blob)
|
|
|
|
limit_to_retry = 1
|
|
|
|
begin
|
|
|
|
syscall_to_custom_uploader(blob)
|
2023-07-19 10:27:02 +02:00
|
|
|
rescue => e
|
2022-04-05 15:05:22 +02:00
|
|
|
if limit_to_retry > 0
|
|
|
|
limit_to_retry = limit_to_retry - 1
|
|
|
|
retry
|
2023-07-19 10:27:02 +02:00
|
|
|
else
|
|
|
|
Sentry.set_tags(procedure:)
|
|
|
|
Sentry.capture_exception(e, extra: { filename: })
|
2022-04-05 15:05:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-28 13:16:27 +01:00
|
|
|
def syscall_to_custom_uploader(blob)
|
|
|
|
system(ENV.fetch('ACTIVE_STORAGE_BIG_FILE_UPLOADER_WITH_ENCRYPTION_PATH').to_s, filepath, blob.key, exception: true)
|
|
|
|
end
|
|
|
|
|
2022-04-08 17:09:22 +02:00
|
|
|
def initialize(procedure:, filename:, filepath:)
|
2022-02-28 13:16:27 +01:00
|
|
|
@procedure = procedure
|
2022-04-08 17:09:22 +02:00
|
|
|
@filename = filename
|
2022-02-28 13:16:27 +01:00
|
|
|
@filepath = filepath
|
|
|
|
end
|
|
|
|
end
|