Class: Ecoportal::API::V2::S3::Files::BatchUpload

Inherits:
Object
  • Object
show all
Includes:
Common::Concerns::Benchmarkable, Common::Concerns::Threadable
Defined in:
lib/ecoportal/api/v2/s3/files/batch_upload.rb

Overview

Note:

it uploads to S3, and converts it into a file container as well.

Class service to upload multiple files in one go. Full cycle: from local files to the org file manager.

Defined Under Namespace

Classes: FileResult

Constant Summary collapse

SIZE_UNITS =

KB

1_024
MIN_UNIT =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, files_api:) ⇒ BatchUpload

Returns a new instance of BatchUpload.

Parameters:

  • files (Array<String>)

    the files to be uploaded

  • files_api (API::S3::Files)

    the api object.



38
39
40
41
# File 'lib/ecoportal/api/v2/s3/files/batch_upload.rb', line 38

def initialize(files, files_api:)
  @files     = [files].flatten.compact
  @files_api = files_api
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



16
17
18
# File 'lib/ecoportal/api/v2/s3/files/batch_upload.rb', line 16

def files
  @files
end

#files_apiObject (readonly)

Returns the value of attribute files_api.



16
17
18
# File 'lib/ecoportal/api/v2/s3/files/batch_upload.rb', line 16

def files_api
  @files_api
end

Instance Method Details

#upload!(benchmarking: false, threads: 1, **kargs) ⇒ Object

Do the actual upload of the file



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ecoportal/api/v2/s3/files/batch_upload.rb', line 44

def upload!(benchmarking: false, threads: 1, **kargs) # rubocop:disable Metrics/AbcSize
  bench        = benchmarking && benchmark_enabled?
  bench_mth    = "#{self.class}##{__method__}"
  thr_children = []

  benchmarking("#{bench_mth}.#{files.count}_files", print: bench) do
    with_preserved_thread_globals(report: false) do
      files.each do |file|
        new_thread(thr_children, max: threads) do
          file_results << (result = FileResult.new(file))

          s3_ref = nil

          kbytes = bench ? file_size(file) : 1

          benchmarking("#{bench_mth}.s3_upload (KB)", units: kbytes, print: bench) do
            s3_ref = result.s3_file_reference = s3_api.upload_file(file)
          end

          benchmarking("##{bench_mth}.ep_poll (KB)", units: kbytes, print: bench) do
            files_api.poll!(s3_ref, **kargs) do |poll|
              result.poll = poll
            end
          end
          yield(result) if block_given?
        rescue *rescued_errors => err # each_file
          result.error = err
          yield(result) if block_given?
        end
      end
    end
  end

  thr_children.each(&:join)
  file_results
ensure
  puts benchmark_summary(:all) if bench
end