lib/filestack/utils/utils.rb in filestack-2.7.0 vs lib/filestack/utils/utils.rb in filestack-2.8.0

- old
+ new

@@ -86,11 +86,11 @@ # @param [FilestackSecurity] security Security object with # policy/signature # @param [Hash] options User-defined options for # multipart uploads # @return [Hash] - def send_upload(apikey, external_url: nil, security: nil, options: nil) + def send_upload(apikey, external_url = nil, security = nil, options = nil) base = "#{FilestackConfig::CDN_URL}/#{apikey}/#{build_store_task(options)}" if security policy = security.policy signature = security.signature @@ -197,19 +197,19 @@ # # @param [Array] jobs A list of file parts # @param [IntelligentState] state An IntelligentState object # # @return [Array] - def run_intelligent_upload_flow(jobs, state, storage) + def run_intelligent_upload_flow(jobs, filepath, io, state, storage) bar = ProgressBar.new(jobs.length) generator = create_intelligent_generator(jobs) working_offset = FilestackConfig::DEFAULT_OFFSET_SIZE while generator.alive? batch = get_generator_batch(generator) # run parts Parallel.map(batch, in_threads: 4) do |part| - state = run_intelligent_uploads(part, state, storage) + state = run_intelligent_uploads(part, filepath, io, state, storage) # condition: a chunk has failed but we have not reached the maximum retries while bad_state(state) # condition: timeout to S3, requiring offset size to be changed if state.error_type == 'S3_NETWORK' sleep(5) @@ -217,11 +217,11 @@ # condition: timeout to backend, requiring only backoff elsif ['S3_SERVER', 'BACKEND_SERVER'].include? state.error_type sleep(state.backoff) end state.add_retry - state = run_intelligent_uploads(part, state, storage) + state = run_intelligent_uploads(part, filepath, io, state, storage) end raise "Upload has failed. Please try again later." unless state.ok bar.increment! end end @@ -273,19 +273,18 @@ # @param [Int] filesize Size of incoming file # @param [Typhoeus::Response] start_response Response body from # multipart_start # # @return [Dict] - def chunk_job(job, state, apikey, filename, filepath, filesize, start_response, storage) + def chunk_job(job, state, apikey, filename, filesize, start_response, storage) offset = 0 seek_point = job[:seek_point] chunk_list = [] while (offset < FilestackConfig::DEFAULT_CHUNK_SIZE) && (seek_point + offset) < filesize chunk_list.push( seek_point: seek_point, - filepath: filepath, filename: filename, apikey: apikey, part: job[:part], size: job[:size], uri: start_response['uri'], @@ -305,19 +304,18 @@ # @param [Dict] part A dictionary representing the information # for a single part # @param [IntelligentState] state An IntelligentState object # # @return [IntelligentState] - def run_intelligent_uploads(part, state, storage) + def run_intelligent_uploads(part, filepath, io, state, storage) failed = false chunks = chunk_job( - part, state, part[:apikey], part[:filename], part[:filepath], - part[:filesize], part[:start_response], storage + part, state, part[:apikey], part[:filename], part[:filesize], part[:start_response], storage ) Parallel.map(chunks, in_threads: 3) do |chunk| begin - upload_chunk_intelligently(chunk, state, part[:apikey], part[:filepath], part[:options], storage) + upload_chunk_intelligently(chunk, state, part[:apikey], filepath, io, part[:options], storage) rescue => e state.error_type = e.message failed = true Parallel::Kill end @@ -362,11 +360,11 @@ # @param [String] filepath Local path to the file # @param [Hash] options User-defined options for # multipart uploads # # @return [Typhoeus::Response] - def upload_chunk_intelligently(job, state, apikey, filepath, options, storage) - file = File.open(filepath) + 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