Class: Rise::Transport::Uploader
- Inherits:
-
Object
- Object
- Rise::Transport::Uploader
- Defined in:
- lib/core/transport.rb
Overview
Handles uploading files
Instance Attribute Summary collapse
-
#current_file ⇒ Object
readonly
Returns the value of attribute current_file.
-
#files ⇒ Object
Returns the value of attribute files.
-
#folder_path ⇒ Object
readonly
Returns the value of attribute folder_path.
-
#include_folder ⇒ Object
readonly
Returns the value of attribute include_folder.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#total_files ⇒ Object
readonly
Returns the value of attribute total_files.
-
#total_files_size ⇒ Object
readonly
Returns the value of attribute total_files_size.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Instance Method Summary collapse
-
#initialize(folder_path, key, excluded_files = [], include_folder = true) ⇒ Uploader
constructor
A new instance of Uploader.
-
#upload! ⇒ Object
This makes a HTTP
PUT
request on port 8080 to the /api/v1/ endpoint for each file in the selected folder.
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_file ⇒ Object (readonly)
Returns the value of attribute current_file.
17 18 19 |
# File 'lib/core/transport.rb', line 17 def current_file @current_file end |
#files ⇒ Object
Returns the value of attribute files.
18 19 20 |
# File 'lib/core/transport.rb', line 18 def files @files end |
#folder_path ⇒ Object (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_folder ⇒ Object (readonly)
Returns the value of attribute include_folder.
16 17 18 |
# File 'lib/core/transport.rb', line 16 def include_folder @include_folder end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
17 18 19 |
# File 'lib/core/transport.rb', line 17 def key @key end |
#total_files ⇒ Object (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_size ⇒ Object (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 |
#uuid ⇒ Object (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.
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.(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 |