Class: TelestreamCloud::Tts::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/telestream_cloud_tts/uploader.rb

Defined Under Namespace

Classes: ConcurrentFileUploadSession, FileReader, FileUploadSession, MissingPartError, UploadError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options) ⇒ Uploader

Returns a new instance of Uploader.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/telestream_cloud_tts/uploader.rb', line 154

def initialize(client, options)
  @client = client

  @thread_pool = Concurrent::ThreadPoolExecutor.new(
    min_threads: 5,
    max_threads: 5,
    max_queue: 100,
    fallback_policy: :caller_runs
  )

  file = options.delete(:file) do |key|
    raise KeyError, "key not found: #{key}"
  end

  @extra_files = parse_extra_files(options.delete(:extra_files))

  @upload_session = upload_session(file)

  defaults = {
    multi_chunk: true
  }.merge(@upload_session.params)

  @params = defaults.merge(options)

  @params[:extra_files] = @extra_files.map(&:params) if @extra_files

  @status = :ready
end

Instance Attribute Details

#concurrencyObject (readonly)

Returns the value of attribute concurrency.



147
148
149
# File 'lib/telestream_cloud_tts/uploader.rb', line 147

def concurrency
  @concurrency
end

#errorObject (readonly)

Returns the value of attribute error.



147
148
149
# File 'lib/telestream_cloud_tts/uploader.rb', line 147

def error
  @error
end

#readerObject (readonly)

Returns the value of attribute reader.



147
148
149
# File 'lib/telestream_cloud_tts/uploader.rb', line 147

def reader
  @reader
end

#statusObject (readonly)

Returns the value of attribute status.



147
148
149
# File 'lib/telestream_cloud_tts/uploader.rb', line 147

def status
  @status
end

#thread_poolObject (readonly)

Returns the value of attribute thread_pool.



147
148
149
# File 'lib/telestream_cloud_tts/uploader.rb', line 147

def thread_pool
  @thread_pool
end

Class Method Details

.upload(options) ⇒ Object



149
150
151
152
# File 'lib/telestream_cloud_tts/uploader.rb', line 149

def self.upload(options)
  retries = options.delete(:retries)
  new(options).tap { |x| x.upload(*[retries].compact) }.video
end

Instance Method Details

#parse_extra_files(extra_files) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/telestream_cloud_tts/uploader.rb', line 183

def parse_extra_files(extra_files)
  return [] unless extra_files

  extra_files.flat_map do |tag, paths|
    paths = [paths] unless paths.kind_of?(Array)

    paths.each_with_index.map do |path, i|
      upload_session(path, paths.size > 1 ? "#{tag}.index-#{i}" : tag)
    end
  end
end

#success?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/telestream_cloud_tts/uploader.rb', line 212

def success?
  @resource_id && @upload_session.success? && Array(@extra_files).all?(&:success?)
end

#upload(retries = 5) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/telestream_cloud_tts/uploader.rb', line 195

def upload(retries = 5)
  session

  count = 0
  begin
    count += 1
    upload!
    raise MissingPartError unless success?
  rescue StandardError => e
    puts e
    @error = e
    retry if count < retries
  end

  @resource_id
end

#upload_session(path, tag = nil) ⇒ Object



216
217
218
# File 'lib/telestream_cloud_tts/uploader.rb', line 216

def upload_session(path, tag = nil)
  ConcurrentFileUploadSession.new(self, path, tag)
end