-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
101 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,63 @@ | ||
module Multipart | ||
# From: http://deftcode.com/code/flickr_upload/multipartpost.rb | ||
# Helper class to prepare an HTTP POST request with a file upload | ||
# Mostly taken from | ||
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/113774 | ||
# WAS: | ||
# Anything that's broken and wrong probably the fault of Bill Stilwell | ||
# (bill@marginalia.org) | ||
# NOW: | ||
# Everything wrong is due to keith@oreilly.com | ||
require 'rubygems' | ||
require 'mime/types' | ||
require 'net/http' | ||
require 'cgi' | ||
|
||
class Param | ||
attr_accessor :k, :v | ||
def initialize(k, v) | ||
@k = k | ||
@v = v | ||
module OauthChina | ||
|
||
module Multipart | ||
|
||
class Param | ||
attr_accessor :k, :v | ||
def initialize(k, v) | ||
@k = k | ||
@v = v | ||
end | ||
|
||
def to_multipart | ||
#return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n" | ||
# Don't escape mine... | ||
return "Content-Disposition: form-data; name=\"#{k}\"\r\n\r\n#{v}\r\n" | ||
end | ||
end | ||
|
||
def to_multipart | ||
#return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n" | ||
# Don't escape mine... | ||
return "Content-Disposition: form-data; name=\"#{k}\"\r\n\r\n#{v}\r\n" | ||
class FileParam | ||
attr_accessor :k, :filename, :content | ||
def initialize(k, filename, content) | ||
@k = k | ||
@filename = filename | ||
@content = content | ||
end | ||
|
||
def to_multipart | ||
#return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n " | ||
# Don't escape mine | ||
return "Content-Disposition: form-data; name=\"#{k}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n" | ||
end | ||
end | ||
end | ||
class MultipartPost | ||
|
||
BOUNDARY = 'tarsiers-rule0000' | ||
ContentType = "multipart/form-data; boundary=" + BOUNDARY | ||
|
||
def set_form_data(req, params) | ||
body, content_type = prepare_query(params) | ||
req["Content-Type"] = content_type | ||
req.body = body | ||
req["Content-Length"] = body.bytesize | ||
req | ||
end | ||
|
||
def prepare_query(params) | ||
fp = [] | ||
params.each {|k,v| | ||
if v.respond_to?(:read) | ||
fp.push(FileParam.new(k, v.path, v.read)) | ||
v.close | ||
else | ||
fp.push(Param.new(k,v)) | ||
end | ||
} | ||
body = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--rn" | ||
return body, ContentType | ||
end | ||
|
||
class FileParam | ||
attr_accessor :k, :filename, :content | ||
def initialize(k, filename, content) | ||
@k = k | ||
@filename = filename | ||
@content = content | ||
end | ||
|
||
def to_multipart | ||
#return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n " | ||
# Don't escape mine | ||
return "Content-Disposition: form-data; name=\"#{k}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n" | ||
end | ||
end | ||
class MultipartPost | ||
|
||
BOUNDARY = 'tarsiers-rule0000' | ||
ContentType = "multipart/form-data; boundary=" + BOUNDARY | ||
|
||
def set_form_data(req, params) | ||
body, content_type = prepare_query(params) | ||
req["Content-Type"] = content_type | ||
req.body = body | ||
req["Content-Length"] = body.bytesize | ||
req | ||
end | ||
|
||
def prepare_query(params) | ||
fp = [] | ||
params.each {|k,v| | ||
if v.respond_to?(:read) | ||
fp.push(FileParam.new(k, v.path, v.read)) | ||
v.close | ||
else | ||
fp.push(Param.new(k,v)) | ||
end | ||
} | ||
body = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--rn" | ||
return body, ContentType | ||
end | ||
|
||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module OauthChina | ||
module Upload | ||
|
||
#NOTICE: | ||
#各个微博字段名可能不统一 | ||
def upload(url, options) | ||
url = URI.parse(url) | ||
http = Net::HTTP.new(url.host, url.port) | ||
|
||
req = Net::HTTP::Post.new(url.request_uri) | ||
req = sign_without_pic_field(req, self.access_token, options) | ||
req = set_multipart_field(req, options) | ||
|
||
http.request(req) | ||
end | ||
|
||
#图片不参与签名 | ||
def sign_without_pic_field(req, access_token, options) | ||
req.set_form_data(params_without_pic_field(options)) | ||
self.consumer.sign!(req, access_token) | ||
req | ||
end | ||
|
||
#mutipart编码:http://www.ietf.org/rfc/rfc1867.txt | ||
def set_multipart_field(req, params) | ||
multipart_post = Multipart::MultipartPost.new | ||
multipart_post.set_form_data(req, params) | ||
end | ||
|
||
def params_without_pic_field(options) | ||
options.except(:pic) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module OauthChina | ||
VERSION = "0.1.1" | ||
VERSION = "0.2.0" | ||
end |