refactor(archive): remove dependencie on archive from ArchiveUploader
This commit is contained in:
parent
cf8c084a59
commit
561b83781e
3 changed files with 17 additions and 13 deletions
|
@ -4,7 +4,7 @@ class ArchiveUploader
|
||||||
# when file size is bigger, active storage expects the chunks + a manifest.
|
# 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
|
MAX_FILE_SIZE_FOR_BACKEND_BEFORE_CHUNKING = ENV.fetch('ACTIVE_STORAGE_FILE_SIZE_THRESHOLD_BEFORE_CUSTOM_UPLOAD') { 4.gigabytes }.to_i
|
||||||
|
|
||||||
def upload
|
def upload(archive)
|
||||||
uploaded_blob = create_and_upload_blob
|
uploaded_blob = create_and_upload_blob
|
||||||
begin
|
begin
|
||||||
archive.file.purge if archive.file.attached?
|
archive.file.purge if archive.file.attached?
|
||||||
|
@ -21,9 +21,13 @@ class ArchiveUploader
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def blob
|
||||||
|
create_and_upload_blob
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
attr_reader :procedure, :archive, :filepath
|
attr_reader :procedure, :filename, :filepath
|
||||||
|
|
||||||
def create_and_upload_blob
|
def create_and_upload_blob
|
||||||
if active_storage_service_local? || File.size(filepath) < MAX_FILE_SIZE_FOR_BACKEND_BEFORE_CHUNKING
|
if active_storage_service_local? || File.size(filepath) < MAX_FILE_SIZE_FOR_BACKEND_BEFORE_CHUNKING
|
||||||
|
@ -62,7 +66,7 @@ class ArchiveUploader
|
||||||
def blob_default_params(filepath)
|
def blob_default_params(filepath)
|
||||||
{
|
{
|
||||||
key: namespaced_object_key,
|
key: namespaced_object_key,
|
||||||
filename: archive.filename(procedure),
|
filename: filename,
|
||||||
content_type: 'application/zip',
|
content_type: 'application/zip',
|
||||||
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
||||||
}
|
}
|
||||||
|
@ -89,9 +93,9 @@ class ArchiveUploader
|
||||||
system(ENV.fetch('ACTIVE_STORAGE_BIG_FILE_UPLOADER_WITH_ENCRYPTION_PATH').to_s, filepath, blob.key, exception: true)
|
system(ENV.fetch('ACTIVE_STORAGE_BIG_FILE_UPLOADER_WITH_ENCRYPTION_PATH').to_s, filepath, blob.key, exception: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(procedure:, archive:, filepath:)
|
def initialize(procedure:, filename:, filepath:)
|
||||||
@procedure = procedure
|
@procedure = procedure
|
||||||
@archive = archive
|
@filename = filename
|
||||||
@filepath = filepath
|
@filepath = filepath
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,9 +25,9 @@ class ProcedureArchiveService
|
||||||
|
|
||||||
attachments = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers)
|
attachments = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers)
|
||||||
|
|
||||||
ArchiveUploader.new(procedure: @procedure, archive: archive, filepath: zip_filepath)
|
|
||||||
.upload
|
|
||||||
DownloadableFileService.download_and_zip(@procedure, attachments, zip_root_folder(archive)) do |zip_filepath|
|
DownloadableFileService.download_and_zip(@procedure, attachments, zip_root_folder(archive)) do |zip_filepath|
|
||||||
|
ArchiveUploader.new(procedure: @procedure, filename: archive.filename(@procedure), filepath: zip_filepath)
|
||||||
|
.upload(archive)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,18 +4,18 @@ describe ProcedureArchiveService do
|
||||||
let(:file) { Tempfile.new }
|
let(:file) { Tempfile.new }
|
||||||
let(:fixture_blob) { ActiveStorage::Blob.create_before_direct_upload!(filename: File.basename(file.path), byte_size: file.size, checksum: 'osf') }
|
let(:fixture_blob) { ActiveStorage::Blob.create_before_direct_upload!(filename: File.basename(file.path), byte_size: file.size, checksum: 'osf') }
|
||||||
|
|
||||||
let(:uploader) { ArchiveUploader.new(procedure: procedure, archive: archive, filepath: file.path) }
|
let(:uploader) { ArchiveUploader.new(procedure: procedure, filename: archive.filename(procedure), filepath: file.path) }
|
||||||
|
|
||||||
describe '.upload' do
|
describe '.upload' do
|
||||||
context 'when active storage service is local' do
|
context 'when active storage service is local' do
|
||||||
it 'uploads with upload_with_active_storage' do
|
it 'uploads with upload_with_active_storage' do
|
||||||
expect(uploader).to receive(:active_storage_service_local?).and_return(true)
|
expect(uploader).to receive(:active_storage_service_local?).and_return(true)
|
||||||
expect(uploader).to receive(:upload_with_active_storage).and_return(fixture_blob)
|
expect(uploader).to receive(:upload_with_active_storage).and_return(fixture_blob)
|
||||||
uploader.upload
|
uploader.upload(archive)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'link the created blob as an attachment to the current archive instance' do
|
it 'link the created blob as an attachment to the current archive instance' do
|
||||||
expect { uploader.upload }
|
expect { uploader.upload(archive) }
|
||||||
.to change { ActiveStorage::Attachment.where(name: 'file', record_type: 'Archive', record_id: archive.id).count }.by(1)
|
.to change { ActiveStorage::Attachment.where(name: 'file', record_type: 'Archive', record_id: archive.id).count }.by(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -31,7 +31,7 @@ describe ProcedureArchiveService do
|
||||||
|
|
||||||
it 'uploads with upload_with_active_storage' do
|
it 'uploads with upload_with_active_storage' do
|
||||||
expect(uploader).to receive(:upload_with_active_storage).and_return(fixture_blob)
|
expect(uploader).to receive(:upload_with_active_storage).and_return(fixture_blob)
|
||||||
uploader.upload
|
uploader.upload(archive)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,12 +40,12 @@ describe ProcedureArchiveService do
|
||||||
|
|
||||||
it 'uploads with upload_with_chunking_wrapper' do
|
it 'uploads with upload_with_chunking_wrapper' do
|
||||||
expect(uploader).to receive(:upload_with_chunking_wrapper).and_return(fixture_blob)
|
expect(uploader).to receive(:upload_with_chunking_wrapper).and_return(fixture_blob)
|
||||||
uploader.upload
|
uploader.upload(archive)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'link the created blob as an attachment to the current archive instance' do
|
it 'link the created blob as an attachment to the current archive instance' do
|
||||||
expect(uploader).to receive(:upload_with_chunking_wrapper).and_return(fixture_blob)
|
expect(uploader).to receive(:upload_with_chunking_wrapper).and_return(fixture_blob)
|
||||||
expect { uploader.upload }
|
expect { uploader.upload(archive) }
|
||||||
.to change { ActiveStorage::Attachment.where(name: 'file', record_type: 'Archive', record_id: archive.id).count }.by(1)
|
.to change { ActiveStorage::Attachment.where(name: 'file', record_type: 'Archive', record_id: archive.id).count }.by(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue