Method: IntelligentUtils#upload_chunk_intelligently

Defined in:
lib/filestack/utils/utils.rb

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

Upload a single chunk

Parameters:

  • job (Dict)

    Dictionary with all job options

  • state (IntelligentState)

    An IntelligentState object

  • apikey (String)

    Filestack API key

  • filename (String)

    Name of incoming file

  • filepath (String)

    Local path to the file

  • options (Hash)

    User-defined options for multipart uploads

Returns:

  • (Typhoeus::Response)


368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/filestack/utils/utils.rb', line 368

def upload_chunk_intelligently(job, state, apikey, filepath, io, options, storage)
  file = filepath ? File.open(filepath) : io
  file.seek(job[:seek_point] + job[:offset])

  chunk = file.read(state.offset)
  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 },
    offset: job[:offset],
    fii: true
  }

  data = data.merge!(options) if options

  fs_response = Typhoeus.post(FilestackConfig.multipart_upload_url(job[:location_url]),
                              body: data.to_json,
                              headers: FilestackConfig::HEADERS)

  # POST to multipart/upload
  begin
    unless fs_response.code == 200
      if [400, 403, 404].include? fs_response.code
        raise 'FAILURE'
      else
        raise 'BACKEND_SERVER'
      end
    end

  rescue StandardError
    raise 'BACKEND_NETWORK'
  end
  fs_response = JSON.parse(fs_response.body)

  # PUT to S3
  begin
    amazon_response = Typhoeus.put(
      fs_response['url'], headers: fs_response['headers'], body: chunk
    )
    unless amazon_response.code == 200
      if [400, 403, 404].include? amazon_response.code
        raise 'FAILURE'
      else
        raise 'S3_SERVER'
      end
    end

  rescue StandardError
    raise 'S3_NETWORK'
  end
  amazon_response
end