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, key, excluded_files = [], include_folder = true) ⇒ Uploader

Returns a new instance of Uploader.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/core/transport.rb', line 20

def initialize(folder_path, key, excluded_files = [], include_folder = true)
  excluded_files.map! do |a|
    File.join(File.absolute_path(folder_path), a)
  end unless excluded_files.nil?
  @folder_path      = folder_path
  @files            = Dir.glob("#{File.absolute_path(folder_path)}/**/*")
  @files           -= excluded_files unless excluded_files.nil?
  @total_files      = @files.length
  @total_files_size = calculate_files_size
  @include_folder   = include_folder
  @uuid             = "#{File.basename(File.absolute_path(folder_path)).gsub('_', '-')}-#{Rex::Text.rand_text_alphanumeric(8)}" # Structure: foldername-8RNDLTRS
  @key              = key
end

Instance Attribute Details

#current_fileObject (readonly)

Returns the value of attribute current_file.



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

def current_file
  @current_file
end

#filesObject

Returns the value of attribute files.



18
19
20
# File 'lib/core/transport.rb', line 18

def files
  @files
end

#folder_pathObject (readonly)

Returns the value of attribute folder_path.



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

def folder_path
  @folder_path
end

#include_folderObject (readonly)

Returns the value of attribute include_folder.



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

def include_folder
  @include_folder
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#total_filesObject (readonly)

Returns the value of attribute total_files.



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

def total_files
  @total_files
end

#total_files_sizeObject (readonly)

Returns the value of attribute total_files_size.



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

def total_files_size
  @total_files_size
end

#uuidObject (readonly)

Returns the value of attribute uuid.



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

def uuid
  @uuid
end

Instance Method Details

#upload!Object

This makes a HTTP PUT request on port 8080 to the /api/v1/ endpoint for each file in the selected folder.

The body of the request is the contents of the file.

The Authorization request header is used for making the .keyfile on the serverside for the future file deletion method.

Returns:

  • String the final URL of the uploaded contents



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/core/transport.rb', line 43

def upload!
  upload_uri_base = "http://rise.sh/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.gsub(' ', '')}?dir=#{isdir}")
    begin
      Rise::Text.vputs("Uploading #{File.basename(f)}")
      res = HTTP.auth("#{key}").put(uri.to_s, body: ActiveSupport::Gzip.compress(File.read(f)))
      abort(Paint["Upload failed. Got error code #{res.code} with message: #{JSON.parse(res)['message']}", :red]) unless (!res.code.nil? && res.code == 200)
    rescue Errno::EISDIR
      res = HTTP.auth("#{key}").put(uri.to_s, body: '')
      abort(Paint["Upload failed. Got error code #{res.code} with message: #{JSON.parse(res)['message']}", :red]) unless (!res.code.nil? && res.code == 200)
      next
    end
  end
  access_uri
end