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