2013-02-05 14:16:51 -05:00
|
|
|
require 'rest_client'
|
|
|
|
require 'image_size'
|
|
|
|
|
|
|
|
module Imgur
|
|
|
|
|
|
|
|
def self.upload_file(file)
|
|
|
|
|
|
|
|
blob = file.read
|
2013-05-01 14:49:13 -04:00
|
|
|
response = RestClient.post(SiteSetting.imgur_endpoint, { image: Base64.encode64(blob) }, { 'Authorization' => "Client-ID #{SiteSetting.imgur_client_id}" })
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-01 14:49:13 -04:00
|
|
|
json = JSON.parse(response.body)['data'] rescue nil
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
return nil if json.blank?
|
|
|
|
|
|
|
|
# Resize the image
|
2013-05-01 14:49:13 -04:00
|
|
|
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
|
|
|
|
}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|