diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index 6787a0989..3e924dd89 100755 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -67,8 +67,9 @@ class SiteSetting < ActiveRecord::Base setting(:queue_jobs, !Rails.env.test?) setting(:crawl_images, !Rails.env.test?) setting(:enable_imgur, false) - setting(:imgur_api_key, '') - setting(:imgur_endpoint, "http://api.imgur.com/2/upload.json") + setting(:imgur_client_id, '') + setting(:imgur_client_secret, '') + setting(:imgur_endpoint, "http://api.imgur.com/3/image.json") setting(:max_image_width, 690) client_setting(:category_featured_topics, 6) setting(:topics_per_page, 30) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index dfd002952..01736627c 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -413,7 +413,8 @@ en: crawl_images: "Enable retrieving images from third party sources to insert width and height dimensions" ninja_edit_window: "Number of seconds after posting where edits do not create a new version" enable_imgur: "Enable imgur api for uploading, don't host files locally" - imgur_api_key: "Your imgur.com api key, required for image upload to function" + imgur_client_id: "Your imgur.com client ID, required for image upload to function" + imgur_client_secret: "Your imgur.com client secret. Not currently required for image upload to function, but may be at some point." imgur_endpoint: "End point for uploading imgur.com images" max_image_width: "Maximum allowed width of images in a post" category_featured_topics: "Number of topics displayed per category in the /categories page" @@ -920,4 +921,4 @@ en: image: fetch_failure: "Sorry, there has been an error while fetching the image." unknown_image_type: "Sorry, but the file you tried to upload doesn't appear to be an image." - size_not_found: "Sorry, but we couldn't determine the size of the image. Maybe your image is corrupted?" \ No newline at end of file + size_not_found: "Sorry, but we couldn't determine the size of the image. Maybe your image is corrupted?" diff --git a/lib/imgur.rb b/lib/imgur.rb index 48df3b01f..4cc029553 100644 --- a/lib/imgur.rb +++ b/lib/imgur.rb @@ -6,19 +6,22 @@ module Imgur def self.upload_file(file) blob = file.read - response = RestClient.post(SiteSetting.imgur_endpoint, key: SiteSetting.imgur_api_key, image: Base64.encode64(blob)) + response = RestClient.post(SiteSetting.imgur_endpoint, { image: Base64.encode64(blob) }, { 'Authorization' => "Client-ID #{SiteSetting.imgur_client_id}" }) - json = JSON.parse(response.body)['upload'] rescue nil + json = JSON.parse(response.body)['data'] rescue nil return nil if json.blank? # Resize the image - json['image']['width'], json['image']['height'] = ImageSizer.resize(json['image']['width'], json['image']['height']) + image_info = FastImage.new(file, raise_on_failure: true) + width, height = ImageSizer.resize(*image_info.size) - {url: json['links']['original'], - filesize: json['image']['size'], - width: json['image']['width'], - height: json['image']['height']} + { + url: json['link'], + filesize: File.size(file), + width: width, + height: height + } end end diff --git a/spec/components/imgur_spec.rb b/spec/components/imgur_spec.rb new file mode 100644 index 000000000..f92717a05 --- /dev/null +++ b/spec/components/imgur_spec.rb @@ -0,0 +1,55 @@ +require 'spec_helper' +require 'imgur' + +describe Imgur do + + describe "upload_file" do + + let(:file) { Rails.root.join('app', 'assets', 'images', 'logo.png') } + let(:params) { [SiteSetting.imgur_endpoint, { image: Base64.encode64(file.read) }, { 'Authorization' => "Client-ID #{SiteSetting.imgur_client_id}" }] } + + it 'returns JSON of the Imgur upload if successful' do + json = { + data: { + id: 'fake', + link: 'http://imgur.com/fake.png', + deletehash: 'a3kaoad30' + }, + success: true, + status: 200 + }.to_json + + response = mock + response.expects(:body).returns(json) + + image_info = { + url: 'http://imgur.com/fake.png', + filesize: File.size(file) + } + + RestClient.expects(:post).with(*params).returns(response) + result = Imgur.upload_file(file) + + # Not testing what width/height actually are because ImageSizer is already tested + result[:url].should eq(image_info[:url]) + result[:filesize].should eq(image_info[:filesize]) + result[:width].should_not be_nil + result[:height].should_not be_nil + end + + it 'returns nil if the request fails' do + json = { + success: false, + status: 400 + }.to_json + + response = mock + response.expects(:body).returns(json) + RestClient.expects(:post).with(*params).returns(response) + + Imgur.upload_file(file).should be_nil + end + + end + +end