Module: MultipartUploadUtils

Included in:
FilestackClient
Defined in:
lib/filestack/utils/multipart_upload_utils.rb

Overview

Includes all the utility functions for Filestack multipart uploads

Instance Method Summary collapse

Instance Method Details

#create_upload_jobs(apikey, filename, filesize, start_response, storage, options) ⇒ Array

Create array of jobs for parallel uploading

Parameters:

  • apikey (String)

    Filestack API key

  • filename (String)

    Name of incoming file

  • filesize (Int)

    Size of incoming file

  • start_response (Typhoeus::Response)

    Response body from multipart_start

  • storage (String)

    Default storage to be used for uploads

  • options (Hash)

    User-defined options for multipart uploads

Returns:

  • (Array)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/filestack/utils/multipart_upload_utils.rb', line 89

def create_upload_jobs(apikey, filename, filesize, start_response, storage, options)
  jobs = []
  part = 1
  seek_point = 0
  while seek_point < filesize
    part_info = {
      seek_point: seek_point,
      filename: filename,
      apikey: apikey,
      part: part,
      filesize: filesize,
      uri: start_response['uri'],
      region: start_response['region'],
      upload_id: start_response['upload_id'],
      location_url: start_response['location_url'],
      start_response: start_response,
      store: { location: storage },
    }

    part_info[:store].merge!(options) if options

    size = if seek_point + FilestackConfig::DEFAULT_CHUNK_SIZE > filesize
      filesize - (seek_point)
    else
      FilestackConfig::DEFAULT_CHUNK_SIZE
           end
    part_info[:size] = size
    jobs.push(part_info)
    part += 1
    seek_point += FilestackConfig::DEFAULT_CHUNK_SIZE
  end
  jobs
end

#get_file_attributes(file, options = {}) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/filestack/utils/multipart_upload_utils.rb', line 17

def get_file_attributes(file, options = {})
  filename = options[:filename] || File.basename(file)
  mimetype = options[:mimetype] || get_mimetype_from_file(file) || FilestackConfig::DEFAULT_UPLOAD_MIMETYPE
  filesize = File.size(file)

  [filename, filesize, mimetype.to_s]
end

#get_io_attributes(io, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/filestack/utils/multipart_upload_utils.rb', line 25

def get_io_attributes(io, options = {})
  filename = options[:filename] || 'unnamed_file'
  mimetype = options[:mimetype] || FilestackConfig::DEFAULT_UPLOAD_MIMETYPE

  io.seek(0, IO::SEEK_END)
  filesize = io.tell

  [filename, filesize, mimetype.to_s]
end

#multipart_complete(apikey, filename, filesize, mimetype, start_response, parts_and_etags, options, storage, intelligent = false) ⇒ Typhoeus::Response

Send complete call to multipart endpoint

Parameters:

  • apikey (String)

    Filestack API key

  • filename (String)

    Name of incoming file

  • filesize (Int)

    Size of incoming file

  • mimetype (String)

    Mimetype of incoming file

  • start_response (Typhoeus::Response)

    Response body from multipart_start

  • security (FilestackSecurity)

    Security object with policy/signature

  • parts_and_etags (Array)

    Array of strings defining etags and their associated part numbers

  • options (Hash)

    User-defined options for multipart uploads

  • storage (String)

    Default storage to be used for uploads

  • intelligent (Boolean) (defaults to: false)

    Upload file using Filestack Intelligent Ingestion

Returns:

  • (Typhoeus::Response)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/filestack/utils/multipart_upload_utils.rb', line 208

def multipart_complete(apikey, filename, filesize, mimetype, start_response, parts_and_etags, options, storage, intelligent = false)
  data = {
    apikey: apikey,
    uri: start_response['uri'],
    region: start_response['region'],
    upload_id: start_response['upload_id'],
    filename: filename,
    size: filesize,
    mimetype: mimetype,
    store: { location: storage },
  }
  data[:store].merge!(options) if options
  data.merge!(intelligent ? { fii: intelligent } : { parts: parts_and_etags })

  Typhoeus.post(FilestackConfig.multipart_complete_url(start_response['location_url']),
                body: data.to_json,
                headers: FilestackConfig::HEADERS)
end

#multipart_start(apikey, filename, filesize, mimetype, security, storage, options = {}, intelligent) ⇒ Typhoeus::Response

Send start response to multipart endpoint

Parameters:

  • apikey (String)

    Filestack API key

  • filename (String)

    Name of incoming file

  • filesize (Int)

    Size of incoming file

  • mimetype (String)

    Mimetype of incoming file

  • security (FilestackSecurity)

    Security object with policy/signature

  • storage (String)

    Default storage to be used for uploads

  • options (Hash) (defaults to: {})

    User-defined options for multipart uploads

  • intelligent (Bool)

    Upload file using Filestack Intelligent Ingestion

Returns:

  • (Typhoeus::Response)


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
# File 'lib/filestack/utils/multipart_upload_utils.rb', line 49

def multipart_start(apikey, filename, filesize, mimetype, security, storage, options = {}, intelligent)
  params = {
    apikey: apikey,
    filename: filename,
    mimetype: mimetype,
    size: filesize,
    store: { location: storage },
    fii: intelligent
  }

  params[:store].merge!(options) if options

  unless security.nil?
    params[:policy] = security.policy
    params[:signature] = security.signature
  end

  response = Typhoeus.post(FilestackConfig.multipart_start_url,
                           body: params.to_json,
                           headers: FilestackConfig::HEADERS)

  if response.code == 200
    JSON.parse(response.body)
  else
    raise RuntimeError.new(response.body)
  end
end

#multipart_upload(apikey, filepath, io, security, options, timeout, storage, intelligent = false) ⇒ Hash

Run entire multipart process through with file and options

Parameters:

  • apikey (String)

    Filestack API key

  • filename (String)

    Name of incoming file

  • io (StringIO)

    The IO object

  • security (FilestackSecurity)

    Security object with policy/signature

  • options (Hash)

    User-defined options for multipart uploads

  • storage (String)

    Default storage to be used for uploads

  • intelligent (Boolean) (defaults to: false)

    Upload file using Filestack Intelligent Ingestion

Returns:



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/filestack/utils/multipart_upload_utils.rb', line 240

def multipart_upload(apikey, filepath, io, security, options, timeout, storage, intelligent = false)
  filename, filesize, mimetype = if filepath
                                  get_file_attributes(filepath, options)
                                 else
                                  get_io_attributes(io, options)
                                 end

  start_response = multipart_start(
    apikey, filename, filesize, mimetype, security, storage, options, intelligent
  )

  jobs = create_upload_jobs(
    apikey, filename, filesize, start_response, storage, options
  )

  if intelligent
    state = IntelligentState.new
    run_intelligent_upload_flow(jobs, filepath, io, state, storage)
    response_complete = multipart_complete(
      apikey, filename, filesize, mimetype,
      start_response, nil, options, storage, intelligent
    )
  else
    parts_and_etags = run_uploads(jobs, apikey, filepath, io, options, storage)
    response_complete = multipart_complete(
      apikey, filename, filesize, mimetype,
      start_response, parts_and_etags, options, storage
    )
  end
  begin
    Timeout::timeout(timeout) {
      while response_complete.code == 202
        response_complete = multipart_complete(
          apikey, filename, filesize, mimetype,
          start_response, nil, options, storage, intelligent
        )
      end
    }
  rescue StandardError
    raise "Upload timed out upon completion. Please try again later"
  end
  JSON.parse(response_complete.body)
end

#run_uploads(jobs, apikey, filepath, io, options, storage) ⇒ Array

Runs all jobs in parallel

Parameters:

  • jobs (Array)

    Array of jobs to be run

  • apikey (String)

    Filestack API key

  • filepath (String)

    Local path to file

  • options (Hash)

    User-defined options for multipart uploads

  • storage (String)

    Default storage to be used for uploads

Returns:

  • (Array)

    Array of parts/etags strings



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/filestack/utils/multipart_upload_utils.rb', line 174

def run_uploads(jobs, apikey, filepath, io, options, storage)
  bar = ProgressBar.new(jobs.length)
  results = Parallel.map(jobs, in_threads: 4) do |job|
    response = upload_chunk(
      job, apikey, filepath, io, options, storage
    )
    if response.code == 200
      bar.increment!
      part = job[:part]
      etag = response.headers[:etag]
      { part_number: part, etag: etag }
    end
  end
  results
end

#upload_chunk(job, apikey, filepath, io, options, storage) ⇒ Typhoeus::Response

Uploads one chunk of the file

Parameters:

  • job (Hash)

    Hash of options needed to upload a chunk

  • apikey (String)

    Filestack API key

  • filepath (String)

    Location url given back from endpoint

  • io (StringIO)

    The IO object

  • options (Hash)

    User-defined options for multipart uploads

  • storage (String)

    Default storage to be used for uploads

Returns:

  • (Typhoeus::Response)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/filestack/utils/multipart_upload_utils.rb', line 137

def upload_chunk(job, apikey, filepath, io, options, storage)
  file = filepath ? File.open(filepath) : io
  file.seek(job[:seek_point])
  chunk = file.read(FilestackConfig::DEFAULT_CHUNK_SIZE)

  md5 = Digest::MD5.new
  md5 << chunk
  data = {
    apikey: apikey,
    part: job[:part],
    size: chunk.length,
    md5: md5.base64digest,
    uri: job[:uri],
    region: job[:region],
    upload_id: job[:upload_id],
    store: { location: storage },
  }
  data = data.merge!(options) if options

  fs_response = Typhoeus.post(FilestackConfig.multipart_upload_url(job[:location_url]),
                              body: data.to_json,
                              headers: FilestackConfig::HEADERS).body
  fs_response = JSON.parse(fs_response)
  Typhoeus.put(
    fs_response['url'], headers: fs_response['headers'], body: chunk
  )
end