mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
Merge pull request #800 from goodbrews/imgur-api-v3
Update to Imgur API v3
This commit is contained in:
commit
e59ab32210
4 changed files with 71 additions and 11 deletions
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
|
|
17
lib/imgur.rb
17
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
|
||||
|
|
55
spec/components/imgur_spec.rb
Normal file
55
spec/components/imgur_spec.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue