Commit 5e9c942b by lanrion

Merge pull request #20 from lanrion/upload-media-from-remote

[WIP] #17 Upload media from remote
parents e63d88f2 c6aa2dc7
source 'https://rubygems.org'
group :test do
group :test, :development do
gem "rspec", "~> 3.0.0.beta1"
gem "redis-namespace", "~> 1.4.1"
gem "rake", "~> 0.9.6"
gem 'simplecov', '~> 0.7.1', :require => false
gem "codeclimate-test-reporter", require: nil
gem 'coveralls', require: false
end
group :development do
# For debugger
gem "pry-rails", "~> 0.3.2"
......
require "rest-client"
require "carrierwave"
require "weixin_authorize/carrierwave/weixin_uploader"
require 'yajl/json_gem'
require "weixin_authorize/config"
require "weixin_authorize/handler"
require "weixin_authorize/api"
......
# encoding: utf-8
module WeixinAuthorize
module Api
module Media
# 上传多媒体文件
# http请求方式: POST/FORM
# http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
# 支持传路径或者文件类型
def upload_media(media, media_type)
file = media.is_a?(File) ? media : File.new(media)
file = process_file(media)
upload_media_url = "#{media_base_url}/upload"
http_post(upload_media_url, {media: file}, {type: media_type}, "file")
end
......@@ -24,9 +24,67 @@ module WeixinAuthorize
private
def process_file(media)
return media if media.is_a?(File) && jpep?(media)
media_url = media
uploader = WeixinUploader.new
if http?(media_url) # remote
uploader.download!(media_url.to_s)
else # local
media_file = media.is_a?(File) ? media : File.new(media_url)
uploader.cache!(media_file)
end
file = process_media(uploader)
CarrierWave.clean_cached_files! # clear last one day cache
file
end
def media_base_url
"/media"
end
def process_media(uploader)
uploader = covert(uploader)
uploader.file.to_file
end
# JUST ONLY FOR JPG IMAGE
def covert(uploader)
# image process
unless (uploader.file.content_type =~ /image/).nil?
if !jpep?(uploader.file)
require "mini_magick"
# covert to jpeg
image = MiniMagick::Image.open(uploader.path)
image.format("jpg")
uploader.cache!(File.open(image.path))
image.destroy! # remove /tmp from MinMagick generate
end
end
uploader
end
def http?(uri)
return false if !uri.is_a?(String)
uri = URI.parse(uri)
uri.scheme =~ /^https?$/
end
def jpep?(file)
content_type = if file.respond_to?(:content_type)
file.content_type
else
content_type(file.path)
end
!(content_type =~ /jpeg/).nil?
end
def content_type(media_path)
MIME::Types.type_for(media_path).first.content_type
end
end
end
end
module WeixinAuthorize
class WeixinUploader < CarrierWave::Uploader::Base
end
end
......@@ -2,25 +2,71 @@ require "spec_helper"
describe WeixinAuthorize::Api::Media do
let(:image_path) do
let(:image_jpg_path) do
"#{File.dirname(__FILE__)}/medias/ruby-logo.jpg"
end
let(:image_file) do
File.new(image_path)
let(:image_ico_path) do
"#{File.dirname(__FILE__)}/medias/favicon.ico"
end
it "can upload a image" do
response = $client.upload_media(image_file, "image")
let(:image_jpg_file) do
File.new(image_jpg_path)
end
let(:image_ico_file) do
File.new(image_ico_path)
end
let(:remote_png_path) do
"http://l.ruby-china.org/user/large_avatar/273.png"
end
let(:remote_jpg_path) do
"http://g.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=ce55457e4334970a537e187df4a3baad/03087bf40ad162d99455ef4d13dfa9ec8b13632762d0ed14.jpg"
end
it "can upload a jpg File image" do
response = $client.upload_media(image_jpg_file, "image")
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
end
it "can upload a ico File image" do
response = $client.upload_media(image_ico_file, "image")
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
end
it "can upload a local image" do
response = $client.upload_media(image_jpg_path, "image")
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
end
it "can upload a local ico image" do
response = $client.upload_media(image_ico_path, "image")
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
end
it "can upload a remote png image" do
response = $client.upload_media(remote_png_path, "image")
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
end
it "can upload a remote jpg image" do
response = $client.upload_media(remote_jpg_path, "image")
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
end
it "#download_media_url return a String url" do
image = $client.upload_media(image_file, "image")
image = $client.upload_media(image_ico_path, "image")
media_id = image.result["media_id"]
response = $client.download_media_url(media_id)
expect(response.class).to eq(String)
image_url = $client.download_media_url(media_id)
expect(image_url.class).to eq(String)
end
end
......@@ -23,6 +23,8 @@ require 'coveralls'
require 'simplecov'
require "codeclimate-test-reporter"
require "pry-rails"
Coveralls.wear!
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
......
......@@ -20,6 +20,10 @@ Gem::Specification.new do |spec|
spec.add_dependency "rest-client", ">= 1.6.7"
spec.add_dependency "redis", "~> 3.0.7"
spec.add_dependency "carrierwave", "~> 0.10.0"
spec.add_dependency 'mini_magick', '~> 3.7.0'
# A streaming JSON parsing and encoding library for Ruby (C bindings to yajl)
# https://github.com/brianmario/yajl-ruby
spec.add_dependency "yajl-ruby", "~> 1.2.0"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment