Skip to content

Commit

Permalink
add logic to properly upload files to S3
Browse files Browse the repository at this point in the history
  • Loading branch information
pennja committed Sep 23, 2024
1 parent 9a4fb45 commit e17a555
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
45 changes: 45 additions & 0 deletions app/uploaders/veteran_facing_forms_remediation_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

class VeteranFacingFormsRemediationUploader < CarrierWave::Uploader::Base
include SetAWSConfig

def size_range
(1.byte)...(100_000_000.bytes)
end

# All the same files allowed by benefits intake, with the
# addition of json for metadata and csv for manifest
def extension_allowlist
%w[bmp csv gif jpeg jpg json pdf png tif tiff txt]
end

def initialize(benefits_intake_uuid, directory)
raise 'The benefits_intake_uuid is missing.' if @benefits_intake_uuid.blank?

super
@benefits_intake_uuid = benefits_intake_uuid
@directory = directory

set_storage_options!
end

def store_dir
raise 'The s3 directory is missing.' if @directory.blank?

@directory
end

def set_storage_options!
# TODO: update this to vff specific S3 bucket once it has been created
s3_settings = Settings.reports.aws
# defaults to CarrierWave::Storage::File if not AWS unless a real aws_access_key_id is set
if s3_settings.aws_access_key_id.present?
set_aws_config(
s3_settings.aws_access_key_id,
s3_settings.aws_secret_access_key,
s3_settings.region,
s3_settings.bucket
)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def initialize(parent_dir: 'vff-simple-forms', **options) # rubocop:disable Lint
def upload
log_info("Uploading archive: #{benefits_intake_uuid} to S3 bucket")

upload_temp_folder_to_s3
upload_directory_to_s3(temp_directory_path)
cleanup
generate_presigned_url
rescue => e
Expand Down Expand Up @@ -72,11 +72,16 @@ def build_submission_archive(**)
SubmissionArchiveBuilder.new(**).run
end

def upload_temp_folder_to_s3
Dir.glob("#{temp_directory_path}/**/*").each do |path|
def upload_directory_to_s3(directory_path)
raise "Directory #{directory_path} does not exist" unless Dir.exist?(directory_path)

Dir.glob(File.join(directory_path, '**', '*')).each do |path|
next if File.directory?(path)

File.open(path, 'rb') { |file| save_file_to_s3(file.read) }
File.open(path) do |file_obj|
sanitized_file = CarrierWave::SanitizedFile.new(file_obj)
s3_uploader.store!(sanitized_file)
end
end
end

Expand All @@ -102,10 +107,6 @@ def generate_presigned_url
submission_object.presigned_url(:get, expires_in: 30.minutes.to_i)
end

def save_file_to_s3(content)
submission_object.tap { |obj| obj.put(body: content) }
end

def submission_object
s3_resource.bucket(target_bucket).object(s3_submission_file_path)
end
Expand All @@ -131,6 +132,10 @@ def s3_directory_path
@s3_directory_path ||= "#{parent_dir}/#{unique_file_name}"
end

def s3_uploader
@s3_uploader ||= VeteranFacingFormsRemediationUploader.new(benefits_intake_uuid, s3_directory_path)
end

def local_submission_file_path
@local_submission_file_path ||= build_local_file_path(s3_submission_file_path)
end
Expand Down

0 comments on commit e17a555

Please sign in to comment.