discourse/lib/imgur.rb

26 lines
681 B
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
require 'rest_client'
module Imgur
2013-06-05 06:34:53 +08:00
def self.store_file(file, image_info, upload_id)
raise Discourse::SiteSettingMissing.new("imgur_endpoint") if SiteSetting.imgur_endpoint.blank?
raise Discourse::SiteSettingMissing.new("imgur_client_id") if SiteSetting.imgur_client_id.blank?
@imgur_loaded = require 'imgur' unless @imgur_loaded
2013-02-06 03:16:51 +08:00
blob = file.read
2013-06-05 06:34:53 +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
json = JSON.parse(response.body)['data'] rescue nil
2013-02-06 03:16:51 +08:00
return nil if json.blank?
2013-06-05 06:34:53 +08:00
return json['link']
2013-02-06 03:16:51 +08:00
end
end