From 4db87e497248dfb0fcb5ffd8b7be428f8cdb170d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 11 Jun 2013 22:44:06 +0200 Subject: [PATCH] added untested discourse_imgur plugin tried to keep as much logic/data from the original source code --- vendor/gems/discourse_imgur/Gemfile | 13 +++++ vendor/gems/discourse_imgur/LICENSE.txt | 22 ++++++++ vendor/gems/discourse_imgur/README.md | 3 + vendor/gems/discourse_imgur/Rakefile | 7 +++ .../discourse_imgur/discourse_imgur.gemspec | 23 ++++++++ .../discourse_imgur/lib/discourse_imgur.rb | 2 + .../lib/discourse_imgur/engine.rb | 19 +++++++ .../lib/discourse_imgur/imgur.rb | 31 +++++++++++ .../lib/discourse_imgur/locale/server.cs.yml | 5 ++ .../lib/discourse_imgur/locale/server.de.yml | 5 ++ .../lib/discourse_imgur/locale/server.en.yml | 5 ++ .../lib/discourse_imgur/locale/server.es.yml | 5 ++ .../lib/discourse_imgur/locale/server.fr.yml | 5 ++ .../lib/discourse_imgur/locale/server.it.yml | 5 ++ .../lib/discourse_imgur/locale/server.nl.yml | 5 ++ .../lib/discourse_imgur/locale/server.pt.yml | 5 ++ .../lib/discourse_imgur/locale/server.sv.yml | 5 ++ .../discourse_imgur/locale/server.zh_CN.yml | 5 ++ .../discourse_imgur/locale/server.zh_TW.yml | 5 ++ .../lib/discourse_imgur/plugin.rb | 23 ++++++++ .../lib/discourse_imgur/version.rb | 3 + .../gems/discourse_imgur/spec/imgur_spec.rb | 55 +++++++++++++++++++ .../gems/discourse_imgur/spec/spec_helper.rb | 11 ++++ 23 files changed, 267 insertions(+) create mode 100644 vendor/gems/discourse_imgur/Gemfile create mode 100644 vendor/gems/discourse_imgur/LICENSE.txt create mode 100644 vendor/gems/discourse_imgur/README.md create mode 100644 vendor/gems/discourse_imgur/Rakefile create mode 100644 vendor/gems/discourse_imgur/discourse_imgur.gemspec create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur.rb create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/engine.rb create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/imgur.rb create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.cs.yml create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.de.yml create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.en.yml create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.es.yml create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.fr.yml create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.it.yml create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.nl.yml create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.pt.yml create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.sv.yml create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.zh_CN.yml create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.zh_TW.yml create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/plugin.rb create mode 100644 vendor/gems/discourse_imgur/lib/discourse_imgur/version.rb create mode 100644 vendor/gems/discourse_imgur/spec/imgur_spec.rb create mode 100644 vendor/gems/discourse_imgur/spec/spec_helper.rb diff --git a/vendor/gems/discourse_imgur/Gemfile b/vendor/gems/discourse_imgur/Gemfile new file mode 100644 index 000000000..fcb7fdc29 --- /dev/null +++ b/vendor/gems/discourse_imgur/Gemfile @@ -0,0 +1,13 @@ +source 'https://rubygems.org' + +group :test do + gem 'rails' + gem 'rspec' + gem 'mocha' +end + +# TODO: We need our own gem server +gem 'discourse_plugin', path: '../discourse_plugin' + +# Specify your gem's dependencies in rails_multisite.gemspec +gemspec diff --git a/vendor/gems/discourse_imgur/LICENSE.txt b/vendor/gems/discourse_imgur/LICENSE.txt new file mode 100644 index 000000000..864fc8d8d --- /dev/null +++ b/vendor/gems/discourse_imgur/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2013 Régis Hanol + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/gems/discourse_imgur/README.md b/vendor/gems/discourse_imgur/README.md new file mode 100644 index 000000000..a266e8fc3 --- /dev/null +++ b/vendor/gems/discourse_imgur/README.md @@ -0,0 +1,3 @@ +# DiscourseImgur + +Add support for Imgur storage \ No newline at end of file diff --git a/vendor/gems/discourse_imgur/Rakefile b/vendor/gems/discourse_imgur/Rakefile new file mode 100644 index 000000000..56bb7292a --- /dev/null +++ b/vendor/gems/discourse_imgur/Rakefile @@ -0,0 +1,7 @@ +#!/usr/bin/env rake +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:test) do |spec| + spec.pattern = 'spec/*_spec.rb' +end diff --git a/vendor/gems/discourse_imgur/discourse_imgur.gemspec b/vendor/gems/discourse_imgur/discourse_imgur.gemspec new file mode 100644 index 000000000..c3a5eac8e --- /dev/null +++ b/vendor/gems/discourse_imgur/discourse_imgur.gemspec @@ -0,0 +1,23 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'discourse_imgur/version' + +Gem::Specification.new do |spec| + spec.name = "discourse_imgur" + spec.version = DiscourseImgur::VERSION + spec.authors = ["Régis Hanol"] + spec.email = ["regis@hanol.fr"] + spec.description = %q{Add support for Imgur} + spec.summary = %q{Add support for Imgur} + spec.homepage = "" + spec.license = "MIT" + + spec.files = `git ls-files`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_development_dependency "bundler", "~> 1.3" + spec.add_development_dependency "rake" +end diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur.rb b/vendor/gems/discourse_imgur/lib/discourse_imgur.rb new file mode 100644 index 000000000..5ee2b6690 --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur.rb @@ -0,0 +1,2 @@ +require "discourse_imgur/version" +require 'discourse_imgur/engine' if defined?(Rails) && (!Rails.env.test?) diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/engine.rb b/vendor/gems/discourse_imgur/lib/discourse_imgur/engine.rb new file mode 100644 index 000000000..4b1ca1262 --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/engine.rb @@ -0,0 +1,19 @@ +require 'discourse_imgur/plugin' + +module DiscourseImgur + + class Engine < Rails::Engine + + engine_name 'discourse_imgur' + + initializer "discourse_imgur.configure_rails_initialization" do |app| + + app.config.after_initialize do + DiscoursePluginRegistry.setup(DiscourseImgur::Plugin) + end + + end + + end + +end diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/imgur.rb b/vendor/gems/discourse_imgur/lib/discourse_imgur/imgur.rb new file mode 100644 index 000000000..1e4f084a0 --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/imgur.rb @@ -0,0 +1,31 @@ +require 'rest_client' + +# /!\ WARNING /!\ +# This plugin has been extracted from the Discourse source code and has not been tested. +# It really needs some love <3 +# /!\ WARNING /!\ + + +module Imgur + + 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 + + blob = file.read + + response = RestClient.post( + SiteSetting.imgur_endpoint, + { image: Base64.encode64(blob) }, + { 'Authorization' => "ClientID #{SiteSetting.imgur_client_id}" } + ) + + json = JSON.parse(response.body)['data'] rescue nil + + return nil if json.blank? + return json['link'] + end + +end diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.cs.yml b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.cs.yml new file mode 100644 index 000000000..cee48d518 --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.cs.yml @@ -0,0 +1,5 @@ +cs: + site_settings: + enable_imgur: "Používat imgur pro ukládání obrázků, nehostovat obrázky lokálně" + imgur_client_id: "Přístupový klíč 'client ID' pro imgur.com - vyžadováno pro upload obrázků" + imgur_client_secret: "Přístupový klíč 'client secret' pro imgur.com - vyžadováno pro upload obrázků" diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.de.yml b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.de.yml new file mode 100644 index 000000000..e82a77d8e --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.de.yml @@ -0,0 +1,5 @@ +de: + site_settings: + enable_imgur: "Aktiviere die Imgur API zum Hochladen, so dass Dateien nicht lokal gehostet werden." + imgur_client_id: "Deine imgur.com Client-ID. Wird benötigt, um Bilder hochzuladen." + imgur_client_secret: "Dein imgur.com Geheimniss. Wird zur Zeit für das Hochladen der Bilder nicht benötigt, könnte aber in Zukunft benötigt werden." diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.en.yml b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.en.yml new file mode 100644 index 000000000..168d4277f --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.en.yml @@ -0,0 +1,5 @@ +en: + site_settings: + enable_imgur: "Enable imgur api for uploading, don't host files locally" + 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." diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.es.yml b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.es.yml new file mode 100644 index 000000000..1e79c3037 --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.es.yml @@ -0,0 +1,5 @@ +es: + site_settings: + enable_imgur: "Activar imgur api para subidas, no almacenes ficheros localmente" + imgur_api_key: "Tu imgur.com api key, requerida para la función de subida de imágenes" + imgur_endpoint: "End point para la subida de imagenes de imgur.com" diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.fr.yml b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.fr.yml new file mode 100644 index 000000000..01a31fa2e --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.fr.yml @@ -0,0 +1,5 @@ +fr: + site_settings: + enable_imgur: "utiliser l'envoi des images avec l'API imgur, ne pas stocker les fichiers localement" + imgur_client_id: "Votre ID client imgur.com. Requis pour que l'envoi d'images fonctionne." + imgur_client_secret: "Votre secret imgur.com. Pas nécessaire pour que l'envoi d'image fonctionne, mais pourrais le devenir." diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.it.yml b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.it.yml new file mode 100644 index 000000000..9d71fb755 --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.it.yml @@ -0,0 +1,5 @@ +it: + site_settings: + enable_imgur: "Utilizza le api imgur per l'upload, non salvare file in locale" + 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." diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.nl.yml b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.nl.yml new file mode 100644 index 000000000..fb407c0b2 --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.nl.yml @@ -0,0 +1,5 @@ +nl: + site_settings: + enable_imgur: "Gebruik de imgur API voor uploads en sla afbeeldingen niet lokaal op" + imgur_client_id: "Je imgur.com client ID, nodig om afbeeldingen te kunnen uploaden naar imgur" + imgur_client_secret: "Je imgur.com client secret. Is nog niet nodig voor het uploaden van afbeeldingen, maar dat zou in de toekomst kunnen veranderen." diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.pt.yml b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.pt.yml new file mode 100644 index 000000000..e160d5f43 --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.pt.yml @@ -0,0 +1,5 @@ +pt: + site_settings: + enable_imgur: "permitir imgur api para uploading, não guardar ficheiros localmente" + imgur_api_key: "chave para imgur.com api - necessária para image upload" + imgur_endpoint: "end point para uploading imgur.com images" diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.sv.yml b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.sv.yml new file mode 100644 index 000000000..14d7e617c --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.sv.yml @@ -0,0 +1,5 @@ +sv: + site_settings: + enable_imgur: "Aktivera imgur.coms API för uppladdning, sparar inte filer lokalt" + imgur_api_key: "Din API-nyckel för imgur.com, krävs för att bilduppladdningen ska funka" + imgur_endpoint: "Ändpunkt för uppladdning av bilder till imgur.com" diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.zh_CN.yml b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.zh_CN.yml new file mode 100644 index 000000000..02728cc36 --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.zh_CN.yml @@ -0,0 +1,5 @@ +zs_CN: + site_settings: + enable_imgur: "启用 Imgur API 来上传文件,不在本地(站点服务器)保存文件" + imgur_client_id: "你的imgur.com的客户端ID,以便图片上传能正常工作。" + imgur_client_secret: "你的imgur.com的客户端secret。 目前图片上传功能并不需要这项信息,但是将来的某天可能需要。" diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.zh_TW.yml b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.zh_TW.yml new file mode 100644 index 000000000..d2b9f3461 --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/locale/server.zh_TW.yml @@ -0,0 +1,5 @@ +zh_TW: + site_settings: + enable_imgur: "啓用 Imgur API 來上傳文件,不在本地(站點服務器)保存文件" + imgur_client_id: "你的imgur.com的客戶端ID,以便圖片上傳能正常工作。" + imgur_client_secret: "你的imgur.com的客戶端secret。 目前圖片上傳功能並不需要這項信息,但是將來的某天可能需要。" diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/plugin.rb b/vendor/gems/discourse_imgur/lib/discourse_imgur/plugin.rb new file mode 100644 index 000000000..700c4e7a0 --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/plugin.rb @@ -0,0 +1,23 @@ +require 'discourse_plugin' + +# /!\ WARNING /!\ +# This plugin has been extracted from the Discourse source code and has not been tested. +# It really needs some love <3 +# /!\ WARNING /!\ + +module DiscourseImgur + + class Plugin < DiscoursePlugin + + def setup + # add_setting(:enable_imgur, false) + # add_setting(:imgur_client_id, '') + # add_setting(:imgur_endpoint, "https://api.imgur.com/3/image.json") + + # TODO: Mix the following logic in Upload#store_file + # return Imgur.store_file(file, image_info, upload_id) if SiteSetting.enable_imgur? + end + + end + +end diff --git a/vendor/gems/discourse_imgur/lib/discourse_imgur/version.rb b/vendor/gems/discourse_imgur/lib/discourse_imgur/version.rb new file mode 100644 index 000000000..5b5ab058a --- /dev/null +++ b/vendor/gems/discourse_imgur/lib/discourse_imgur/version.rb @@ -0,0 +1,3 @@ +module DiscourseImgur + VERSION = "0.0.1" +end diff --git a/vendor/gems/discourse_imgur/spec/imgur_spec.rb b/vendor/gems/discourse_imgur/spec/imgur_spec.rb new file mode 100644 index 000000000..b350ad5dc --- /dev/null +++ b/vendor/gems/discourse_imgur/spec/imgur_spec.rb @@ -0,0 +1,55 @@ +require 'spec_helper' +require 'discourse_imgur/imgur' + +# /!\ WARNING /!\ +# This plugin has been extracted from the Discourse source code and has not been tested. +# It really needs some love <3 +# /!\ WARNING /!\ + +describe Imgur do + + describe "store_file" do + + let(:file) { Rails.root.join('app', 'assets', 'images', 'logo.png') } + let(:image_info) { FastImage.new(file) } + let(:params) { [SiteSetting.imgur_endpoint, { image: Base64.encode64(file.read) }, { 'Authorization' => "ClientID #{SiteSetting.imgur_client_id}" }] } + + before(:each) do + SiteSetting.stubs(:imgur_endpoint).returns("imgur_endpoint") + SiteSetting.stubs(:imgur_client_id).returns("imgur_client_id") + end + + it 'returns the url 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) + RestClient.expects(:post).with(*params).returns(response) + + Imgur.store_file(file, image_info, 1).should == 'http://imgur.com/fake.png' + 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.store_file(file, image_info, 1).should be_nil + end + + end + +end diff --git a/vendor/gems/discourse_imgur/spec/spec_helper.rb b/vendor/gems/discourse_imgur/spec/spec_helper.rb new file mode 100644 index 000000000..e8279d775 --- /dev/null +++ b/vendor/gems/discourse_imgur/spec/spec_helper.rb @@ -0,0 +1,11 @@ +require 'rubygems' +require 'rails' + +ENV["RAILS_ENV"] ||= 'test' + +RSpec.configure do |config| + + config.mock_framework = :mocha + config.color_enabled = true + +end