class FallCli::Middleware::UploaderBrowser

Check if the client has set-up configuration yet.

Constants

SPLASH

Public Instance Methods

call(env) click to toggle source
# File lib/fallcli/middleware/upload_browser.rb, line 77
def call(env)
  config = env["config"]

  say SPLASH

  sleep(2)

  files = get_files

  upload_browser = FallCli::UploaderBrowserHelper.new(files)

  Dispel::Screen.open do |screen|
    screen.draw show_ui(upload_browser)

    Dispel::Keyboard.output do |key|
      case key
      when :up then upload_browser.position_up
      when :down then upload_browser.position_down
      when :enter then
        file = upload_browser.get_current_file
        file_name = File.basename(file)
        filepath = Pathname.new(file).realpath.to_s
        total_size = File.size(filepath)
        contents  = File.read(filepath)

        if total_size < 5*1024*1024
          env['dropbox-client'].upload file_name, contents
        else
          say "Larger than 5MB: Progress Upload"

          upload_progress_bar = ::ProgressBar.create(:title => "Upload progress",
            :format => '%a <%B> %p%% %t',
            :starting_at => 0,
            :total => total_size)

          response = env['dropbox-client'].chunked_upload file_name, File.open(filepath), :chunk_size => 0.1*1024*1024 do |offset, upload|
            upload_progress_bar.progress = offset
          end
        end

        say "File uploaded successfully!"
      when "q" then break
      end
      screen.draw show_ui(upload_browser)
    end
  end

  @app.call(env)
end
get_files() click to toggle source
# File lib/fallcli/middleware/upload_browser.rb, line 15
def get_files
  require 'find'
  files = []
  Find.find('.') do |e|
    if !File.directory?(e)
      files << e unless e.match /.git/
    end
  end
  files
end
show_ui(filelist_obj) click to toggle source
# File lib/fallcli/middleware/upload_browser.rb, line 11
def show_ui filelist_obj
  ["\n", filelist_obj.show_files, "\nCurrent position: #{filelist_obj.position + 1} "].join("\n")
end