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.



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

def initialize(folder_path, include_folder = true)
  @folder_path      = folder_path
  @files            = Dir.glob("#{File.absolute_path(folder_path)}/**/*")
  @total_files      = @files.length
  @total_files_size = calculate_files_size
  @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_fileObject (readonly)

Returns the value of attribute current_file.



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

def current_file
  @current_file
end

#filesObject

Returns the value of attribute files.



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

def files
  @files
end

#folder_pathObject (readonly)

Returns the value of attribute folder_path.



13
14
15
# File 'lib/core/transport.rb', line 13

def folder_path
  @folder_path
end

#include_folderObject (readonly)

Returns the value of attribute include_folder.



13
14
15
# File 'lib/core/transport.rb', line 13

def include_folder
  @include_folder
end

#total_filesObject (readonly)

Returns the value of attribute total_files.



13
14
15
# File 'lib/core/transport.rb', line 13

def total_files
  @total_files
end

#total_files_sizeObject (readonly)

Returns the value of attribute total_files_size.



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

def total_files_size
  @total_files_size
end

#uuidObject (readonly)

Returns the value of attribute uuid.



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

def uuid
  @uuid
end

Instance Method Details

#upload!Object

Uploads the files from folder_path to the upload server

Returns:

  • String the final URL of the uploaded contents



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/core/transport.rb', line 30

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
  access_uri
end