module Upload

Module for handling outbound requests

Public Instance Methods

http_post_header(data = {}) click to toggle source

Default headers provided for uploading files, any argument provided is merged with the default values

   # File lib/skynet/upload.rb
11 def http_post_header(data = {})
12   default_data = {
13     headers: {
14       'Accept' => 'application/octet-stream',
15       'Content-Type' => 'application/octet-stream',
16       'Transfer-Encoding' => 'gzip, chunked'
17     }
18   }
19 
20   default_data.merge(data) unless data.empty?
21 end
upload_file(file_path, override_options = {}) click to toggle source

Uploads file to the skynet, file_path is required but options are optional since default values are provided

   # File lib/skynet/upload.rb
24 def upload_file(file_path, override_options = {})
25   options = Helper::Upload.default_options
26   options = Helper::Upload.default_options.merge(override_options) unless override_options.empty?
27   return "File #{file_path} does not exist!" unless File.exist?(file_path)
28 
29   host = options[:portal_url]
30   path = options[:portal_upload_path]
31   filename = options[:custom_filename] || file_path
32   filename = Helper::Upload.strip_dotslash_path(filename)
33 
34   url = "#{host}#{path}?filename=#{filename}"
35   binary_content = IO.readlines(file_path, mode: 'rb').join
36 
37   header_data = http_post_header({
38                                    headers: {
39                                      'Content-Disposition' => "attachment; filename='#{filename}'"
40                                    },
41                                    body: binary_content,
42                                    options[:portal_file_fieldname] => filename
43                                  })
44 
45   upload_request = HTTParty.post(url, header_data)
46   parsed_request = upload_request.to_hash
47   "Upload successful, skylink: #{parsed_request['skylink']}"
48 end