lib/filestack/utils/multipart_upload_utils.rb in filestack-2.2.1 vs lib/filestack/utils/multipart_upload_utils.rb in filestack-2.3.0

- old
+ new

@@ -1,18 +1,17 @@ require 'base64' require 'digest' require 'mimemagic' require 'json' require 'parallel' -require 'unirest' +require 'typhoeus' require 'progress_bar' require 'filestack/config' require 'filestack/utils/utils' include UploadUtils include IntelligentUtils -Unirest.timeout(30) # Includes all the utility functions for Filestack multipart uploads module MultipartUploadUtils def get_file_info(file) filename = File.basename(file) filesize = File.size(file) @@ -32,11 +31,11 @@ # @param [FilestackSecurity] security Security object with # policy/signature # @param [Hash] options User-defined options for # multipart uploads # - # @return [Unirest::Response] + # @return [Typhoeus::Response] def multipart_start(apikey, filename, filesize, mimetype, security, options) params = { apikey: apikey, filename: filename, mimetype: mimetype, @@ -51,12 +50,12 @@ unless security.nil? params[:policy] = security.policy params[:signature] = security.signature end - response = Unirest.post( - FilestackConfig::MULTIPART_START_URL, parameters: params, + response = Typhoeus.post( + FilestackConfig::MULTIPART_START_URL, body: params, headers: FilestackConfig::HEADERS ) if response.code == 200 response.body else @@ -68,11 +67,11 @@ # # @param [String] apikey Filestack API key # @param [String] filename Name of incoming file # @param [String] filepath Local path to file # @param [Int] filesize Size of incoming file - # @param [Unirest::Response] start_response Response body from + # @param [Typhoeus::Response] start_response Response body from # multipart_start # @param [Hash] options User-defined options for # multipart uploads # # @return [Array] @@ -107,11 +106,11 @@ seek_point += FilestackConfig::DEFAULT_CHUNK_SIZE end jobs end - + # Uploads one chunk of the file # # @param [Hash] job Hash of options needed # to upload a chunk # @param [String] apikey Filestack API key @@ -119,16 +118,16 @@ # from endpoint # @param [String] filepath Local path to file # @param [Hash] options User-defined options for # multipart uploads # - # @return [Unirest::Response] + # @return [Typhoeus::Response] def upload_chunk(job, apikey, filepath, options) file = File.open(filepath) file.seek(job[:seek]) chunk = file.read(FilestackConfig::DEFAULT_CHUNK_SIZE) - + md5 = Digest::MD5.new md5 << chunk data = { apikey: apikey, part: job[:part], @@ -139,16 +138,16 @@ upload_id: job[:upload_id], store_location: job[:store_location], file: Tempfile.new(job[:filename]) } data = data.merge!(options) if options - fs_response = Unirest.post( - FilestackConfig::MULTIPART_UPLOAD_URL, parameters: data, + fs_response = Typhoeus.post( + FilestackConfig::MULTIPART_UPLOAD_URL, body: data, headers: FilestackConfig::HEADERS ).body - Unirest.put( - fs_response['url'], headers: fs_response['headers'], parameters: chunk + Typhoeus.put( + fs_response['url'], headers: fs_response['headers'], body: chunk ) end # Runs all jobs in parallel # # @param [Array] jobs Array of jobs to be run @@ -162,11 +161,11 @@ bar = ProgressBar.new(jobs.length) results = Parallel.map(jobs, in_threads: 4) do |job| response = upload_chunk( job, apikey, filepath, options ) - if response.code == 200 + if response.code == 200 bar.increment! part = job[:part] etag = response.headers[:etag] "#{part}:#{etag}" end @@ -177,21 +176,21 @@ # # @param [String] apikey Filestack API key # @param [String] filename Name of incoming file # @param [Int] filesize Size of incoming file # @param [String] mimetype Mimetype of incoming file - # @param [Unirest::Response] start_response Response body from + # @param [Typhoeus::Response] start_response Response body from # multipart_start # @param [FilestackSecurity] security Security object with # policy/signature # @param [Array] parts_and_etags Array of strings defining # etags and their associated # part numbers # @param [Hash] options User-defined options for # multipart uploads # - # @return [Unirest::Response] + # @return [Typhoeus::Response] def multipart_complete(apikey, filename, filesize, mimetype, start_response, parts_and_etags, options, intelligent = false) if !intelligent data = { apikey: apikey, uri: start_response['uri'], @@ -218,12 +217,12 @@ 'multipart' => 'true' } end data = data.merge!(options) if options - Unirest.post( - FilestackConfig::MULTIPART_COMPLETE_URL, parameters: data, + Typhoeus.post( + FilestackConfig::MULTIPART_COMPLETE_URL, body: data, headers: FilestackConfig::HEADERS ) end # Run entire multipart process through with file and options @@ -233,11 +232,11 @@ # @param [FilestackSecurity] security Security object with # policy/signature # @param [Hash] options User-defined options for # multipart uploads # - # @return [Unirest::Response] + # @return [Typhoeus::Response] def multipart_upload(apikey, filepath, security, options, timeout, intelligent: false) filename, filesize, mimetype = get_file_info(filepath) start_response = multipart_start( apikey, filename, filesize, mimetype, security, options ) @@ -265,10 +264,10 @@ Timeout::timeout(timeout){ while response_complete.code == 202 response_complete = multipart_complete( apikey, filename, filesize, mimetype, start_response, nil, options, intelligent - ) + ) end } rescue raise "Upload timed out upon completion. Please try again later" end