mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 22:21:55 +08:00
6be26f5316
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>
28 lines
621 B
Ruby
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
|