Class: SimpleFormsApi::FormRemediation::Uploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
Includes:
UploaderVirusScan
Defined in:
app/uploaders/simple_forms_api/form_remediation/uploader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UploaderVirusScan

#validate_virus_free

Constructor Details

#initialize(directory:, config:) ⇒ Uploader

Returns a new instance of Uploader.



10
11
12
13
14
15
16
17
18
19
# File 'app/uploaders/simple_forms_api/form_remediation/uploader.rb', line 10

def initialize(directory:, config:)
  raise 'The S3 directory is missing.' if directory.blank?
  raise 'The configuration is missing.' unless config

  @config = config
  @directory = directory

  super()
  set_storage_options!
end

Instance Attribute Details

#configObject (readonly, private)

Returns the value of attribute config.



64
65
66
# File 'app/uploaders/simple_forms_api/form_remediation/uploader.rb', line 64

def config
  @config
end

Instance Method Details

#extension_allowlistObject

Allowed file types, including those specific to benefits intake



26
27
28
# File 'app/uploaders/simple_forms_api/form_remediation/uploader.rb', line 26

def extension_allowlist
  %w[bmp csv gif jpeg jpg json pdf png tif tiff txt zip]
end

#get_s3_file(from_path, to_path) ⇒ Object



54
55
56
57
58
59
60
# File 'app/uploaders/simple_forms_api/form_remediation/uploader.rb', line 54

def get_s3_file(from_path, to_path)
  s3_obj(from_path).get(response_target: to_path)
rescue Aws::S3::Errors::NoSuchKey
  nil
rescue => e
  config.handle_error('An error occurred while downloading the file.', e)
end


45
46
47
48
49
50
51
52
# File 'app/uploaders/simple_forms_api/form_remediation/uploader.rb', line 45

def get_s3_link(file_path, filename = nil)
  filename ||= File.basename(file_path)
  s3_obj(file_path).presigned_url(
    :get,
    expires_in: 30.minutes.to_i,
    response_content_disposition: "attachment; filename=\"#{filename}\""
  )
end

#s3_obj(file_path) ⇒ Object (private)



66
67
68
69
70
# File 'app/uploaders/simple_forms_api/form_remediation/uploader.rb', line 66

def s3_obj(file_path)
  client = Aws::S3::Client.new(region: config.s3_settings.region)
  resource = Aws::S3::Resource.new(client:)
  resource.bucket(config.s3_settings.bucket).object(file_path)
end

#set_storage_options!Object (private)



72
73
74
75
76
77
78
79
80
# File 'app/uploaders/simple_forms_api/form_remediation/uploader.rb', line 72

def set_storage_options!
  settings = config.s3_settings

  self.aws_credentials = { region: settings.region }
  self.aws_acl = 'private'
  self.aws_bucket = settings.bucket
  self.aws_attributes = { server_side_encryption: 'AES256' }
  self.class.storage = :aws
end

#size_rangeObject



21
22
23
# File 'app/uploaders/simple_forms_api/form_remediation/uploader.rb', line 21

def size_range
  (1.byte)...(150.megabytes)
end

#store!(file) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'app/uploaders/simple_forms_api/form_remediation/uploader.rb', line 34

def store!(file)
  raise 'Invalid file object provided for upload. Skipping.' if file.nil? || !file.respond_to?(:filename)

  super(file)
rescue Aws::S3::Errors::ServiceError => e
  Rails.logger.error("Upload failed for #{file.filename}. Enqueuing for retry.", e)
  UploadRetryJob.perform_async(file, @directory, config)
rescue RuntimeError => e
  config.handle_error('An error occurred while uploading the file.', e)
end

#store_dirObject



30
31
32
# File 'app/uploaders/simple_forms_api/form_remediation/uploader.rb', line 30

def store_dir
  @directory
end