Class: Rise::Transport::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/core/transport.rb

Overview

Handles uploading files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder_path, include_folder = true) ⇒ Uploader

Returns a new instance of Uploader.



18
19
20
21
22
23
24
# File 'lib/core/transport.rb', line 18

def initialize(folder_path, include_folder=true)
  @folder_path    = folder_path
  @files          = Dir.glob("#{File.absolute_path(folder_path)}/**/*")
  @total_files    = @files.length
  @include_folder = include_folder
  @uuid           = "#{File.basename(File.absolute_path(folder_path))}-#{Rex::Text::rand_text_alphanumeric(8)}"  # Structure: foldername-8RNDLTRS
end

Instance Attribute Details

#current_file ⇒ Object (readonly)

Returns the value of attribute current_file.



15
16
17
# File 'lib/core/transport.rb', line 15

def current_file
  @current_file
end

#files ⇒ Object

Returns the value of attribute files.



16
17
18
# File 'lib/core/transport.rb', line 16

def files
  @files
end

#folder_path ⇒ Object (readonly)

Returns the value of attribute folder_path.



15
16
17
# File 'lib/core/transport.rb', line 15

def folder_path
  @folder_path
end

#include_folder ⇒ Object (readonly)

Returns the value of attribute include_folder.



15
16
17
# File 'lib/core/transport.rb', line 15

def include_folder
  @include_folder
end

#total_files ⇒ Object (readonly)

Returns the value of attribute total_files.



15
16
17
# File 'lib/core/transport.rb', line 15

def total_files
  @total_files
end

#uuid ⇒ Object (readonly)

Returns the value of attribute uuid.



15
16
17
# File 'lib/core/transport.rb', line 15

def uuid
  @uuid
end

Instance Method Details

#upload! ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/core/transport.rb', line 26

def upload!(*)
  upload_uri_base = "http://rise.sh:8080/api/v1/#{@uuid}"
  access_uri = "https://rise.sh/#{@uuid}"
  uri = ''

  # This sorts the files by (file path) length.
  # It is supposed to make the server make the first layer of files
  # before the rest of the layers.
  ordered_files = files.sort_by(&:length)
  ordered_files.each do |f|
    isdir = File.directory?(f)
    final_path = File.absolute_path(f).gsub(File.expand_path(folder_path), '')
    uri = URI.parse("#{upload_uri_base}/#{final_path}?dir=#{isdir}")
    begin
      HTTP.put(uri.to_s, :body => File.read(f))
    rescue Errno::EISDIR
      HTTP.put(uri.to_s, :body => '')
      next
    end
  end
  return access_uri
end