Method: Ikbis::Advanced::Upload#uploadFile

Defined in:
lib/ikbis/advanced/upload.rb

#uploadFile(file_path, options) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ikbis/advanced/upload.rb', line 76

def uploadFile(file_path, options)
  puts "|------------------------ Uploading ------------------------|"
  begin                  
    # open file for uploading
    begin
      file = File.new(file_path)
    rescue
      raise InvalidUploadFile
    end
     
    url = URI.parse('http://localhost:3000/api/medias')

    puts "| Initiating POST request to `#{url.request_uri}'"
    req = Net::HTTP::Post.new(url.request_uri)

    puts "| Constructing File object"
    req = Net::HTTP::Post::Multipart.new url.path, 
      { "file" => UploadIO.new(file, mime_type(file), file.path),
        "title" => options[:title], 
        "caption" => options[:caption], 
        "type" => type(file), 
        "tags" => options[:tags], 
        "permission" => options[:permission]
      }

    puts "| Authenticating..."
    begin
      @consumer.sign!(req, @access_token)
    rescue
      raise CouldNotAuthenticate
    end

    puts "| Initiating connection with `#{url.host}:#{url.port}'"
    res = ""
    
    Net::HTTP.new(url.host, url.port).start do |http| 
      print "| Processing request..."
      res = http.request(req)
      print " done!\n"
    end
  
    puts "| Upload request sent successfully:"
    puts "|   Request URI -> #{url.request_uri}"
    puts "|   Response -> #{res}"

  rescue InvalidMediaType
    print "|   File format not supported. Supported types: "
    print "\n|   Images => "
    print @@image_types.join(", ")
    print "\n|   Videos => "
    print @@video_types.join(", ")
    print "\n"     
  rescue CouldNotAuthenticate
      puts "|  Error while authenticating, have you run oauth_authenticate() ?"
  rescue InvalidUploadFile
      puts "| An error occured processing the file you input, please make sure the file is valid."
  rescue
    puts "| Upload failed with `#{$!}'\n"
    raise CouldNotUpload
  end
  
  puts "|------------------------ --------- ------------------------|"
          
end