Sha256: 0bf2d4d4ceca743be054603a28301dd5c02e86a2eccec610cca24fdddeede495

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

require 'rmega/loggable'
require 'rmega/utils'
require 'rmega/pool'
require 'rmega/progress'

module Rmega
  class Uploader
    include Loggable

    attr_reader :pool, :base_url, :filesize, :local_path, :last_result

    def initialize(params)
      @pool = Pool.new(params[:threads])
      @filesize = params[:filesize]
      @base_url = params[:base_url]
      @local_path = params[:local_path]
      @last_result = nil
    end

    def upload_chunk(start, buffer)
      size = buffer.length
      stop = start + size - 1
      url = "#{base_url}/#{start}-#{stop}"

      HTTPClient.new.post(url, buffer).body
    end

    def read_chunk(start, size)
      @local_file.seek(start)
      @local_file.read(size)
    end

    def chunks
      Utils.chunks(filesize)
    end

    def upload(&block)
      @local_file = ::File.open(local_path, 'rb')

      progress = Progress.new(total: filesize, caption: 'Upload')

      chunks.each do |start, size|

        pool.defer do
          clean_buffer = pool.synchronize { read_chunk(start, size) }
          encrypted_buffer = yield(start, clean_buffer)
          @last_result = upload_chunk(start, encrypted_buffer)
          progress.increment(clean_buffer.size)
        end
      end

      pool.wait_done
      pool.shutdown
    ensure
      @local_file.close
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rmega-0.1.5 lib/rmega/uploader.rb
rmega-0.1.4 lib/rmega/uploader.rb
rmega-0.1.3 lib/rmega/uploader.rb
rmega-0.1.2 lib/rmega/uploader.rb
rmega-0.1.1 lib/rmega/uploader.rb