discourse/lib/imgur.rb
David Celis 6be26f5316 Update to Imgur API v3
Version 2 of Imgur's API is deprecated. Their documentation for v2 is no
longer online, and applications can only be registered under version 3.
Version 3 of their API has a slightly different endpoint but, more
importantly, uses a Client ID/Secret pair instead of an API Key.

This PR updates Discourse to use the new version of Imgur's API.

Signed-off-by: David Celis <me@davidcel.is>
2013-05-01 21:39:10 -07:00

28 lines
621 B
Ruby

require 'rest_client'
require 'image_size'
module Imgur
def self.upload_file(file)
blob = file.read
response = RestClient.post(SiteSetting.imgur_endpoint, { image: Base64.encode64(blob) }, { 'Authorization' => "Client-ID #{SiteSetting.imgur_client_id}" })
json = JSON.parse(response.body)['data'] rescue nil
return nil if json.blank?
# Resize the image
image_info = FastImage.new(file, raise_on_failure: true)
width, height = ImageSizer.resize(*image_info.size)
{
url: json['link'],
filesize: File.size(file),
width: width,
height: height
}
end
end