Class: Uploadcare::Client::MultipartUploaderClient

Inherits:
UploadClient
  • Object
show all
Includes:
MultipartUpload
Defined in:
lib/uploadcare/client/multipart_upload_client.rb

Overview

Client for multipart uploads

Instance Method Summary collapse

Methods inherited from UploadClient

#api_root, #headers

Methods included from Uploadcare::Concerns::ThrottleHandler

#handle_throttling

Methods included from Uploadcare::Concerns::ErrorHandler

#failure, #wrap

Instance Method Details

#upload(object, options = {}, &block) ⇒ Object

Upload a big file by splitting it into parts and sending those parts into assigned buckets object should be File



16
17
18
19
20
21
22
23
24
# File 'lib/uploadcare/client/multipart_upload_client.rb', line 16

def upload(object, options = {}, &block)
  response = upload_start(object, options)
  return response unless response.success[:parts] && response.success[:uuid]

  links = response.success[:parts]
  uuid = response.success[:uuid]
  ChunksClient.upload_chunks(object, links, &block)
  upload_complete(uuid)
end

#upload_complete(uuid) ⇒ Object

When every chunk is uploaded, ask Uploadcare server to finish the upload



37
38
39
40
41
42
43
44
45
# File 'lib/uploadcare/client/multipart_upload_client.rb', line 37

def upload_complete(uuid)
  body = HTTP::FormData::Multipart.new(
    {
      UPLOADCARE_PUB_KEY: Uploadcare.config.public_key,
      uuid: uuid
    }
  )
  post(path: 'multipart/complete/', body: body, headers: { 'Content-Type': body.content_type })
end

#upload_start(object, options = {}) ⇒ Object

Asks Uploadcare server to create a number of storage bin for uploads



27
28
29
30
31
32
33
34
# File 'lib/uploadcare/client/multipart_upload_client.rb', line 27

def upload_start(object, options = {})
  body = HTTP::FormData::Multipart.new(
    Param::Upload::UploadParamsGenerator.call(options).merge(form_data_for(object))
  )
  post(path: 'multipart/start/',
       headers: { 'Content-Type': body.content_type },
       body: body)
end