Skip to content

Commit

Permalink
fixing the uploader so that it actually works
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Aug 4, 2009
1 parent 890ef01 commit ba44177
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -8,5 +8,5 @@ Hoe.spec('vimeo') do
self.readme_file = 'README.rdoc'
self.history_file = 'CHANGELOG.rdoc'
self.extra_rdoc_files = FileList['*.rdoc']
self.extra_deps = [['jnunemaker-httparty', '>= 0.2.6']]
self.extra_deps = [['jnunemaker-httparty', '>= 0.2.6'], 'curb']
end
2 changes: 1 addition & 1 deletion lib/vimeo.rb
Expand Up @@ -7,5 +7,5 @@
require 'vimeo/advanced'

module Vimeo
VERSION = '1.3.0'
VERSION = '1.3.1'
end
64 changes: 8 additions & 56 deletions lib/vimeo/advanced/upload.rb
@@ -1,4 +1,6 @@
require 'webrick/httputils'
require 'curl'


module Vimeo
module Advanced
Expand Down Expand Up @@ -27,31 +29,18 @@ def check_upload_status(ticket_id, auth_token)
###
# Upload +file+ to vimeo with +ticket_id+ and +auth_token+
def upload file, ticket_id, auth_token
boundary = rand_string 20

enctype = "multipart/form-data; boundary=#{boundary}"

file_part = file_to_multipart(file)

params = {
:auth_token => auth_token,
:ticket_id => ticket_id
}
params[:api_sig] = generate_api_sig params

data = (params.map { |k,v| param_to_multipart(k,v) } +
[file_part]
).map { |part|
"--#{boundary}\r\n#{part}"
}.join + "--#{boundary}--\r\n"

uri = URI.parse('http://vimeo.com/services/upload')

http_ctx = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request['Content-Type'] = enctype
request['Content-Length'] = data.size.to_s
http_ctx.request(request, data)
c = Curl::Easy.new('http://vimeo.com/services/upload')
c.multipart_form_post = true
c.http_post(
*(params.map { |k,v| Curl::PostField.content(k.to_s, v) } +
[Curl::PostField.file('video', file)])
)
end

def signature_for_file_upload(ticket_id, auth_token)
Expand All @@ -61,43 +50,6 @@ def signature_for_file_upload(ticket_id, auth_token)
}
generate_api_sig sig_options
end

private
def mime_value_quote(str)
str.to_s.gsub(/(["\r\\])/){|s| '\\' + s}
end

def param_to_multipart name, value
"Content-Disposition: form-data; name=\"#{mime_value_quote(name)}\"" +
"\r\n#{value}\r\n"
end

def file_to_multipart file
file_name = File.basename(file)

headers = nil

File.open(file, 'rb') { |fh|

mime_type = WEBrick::HTTPUtils.mime_type(
file,
WEBrick::HTTPUtils::DefaultMimeTypes
)

headers = [
"Content-Disposition: form-data; name=\"" +
"#{mime_value_quote(file)}\"; " +
"filename=\"#{mime_value_quote(file_name)}\"",
"Content-Transfer-Encoding: binary",
]

headers << "Content-Type: #{mime_type}" if mime_type
headers << nil
headers << fh.read
}
headers.join("\r\n") + "\r\n"
end

end

end # Advanced
Expand Down
7 changes: 5 additions & 2 deletions vimeo.gemspec
Expand Up @@ -2,11 +2,11 @@

Gem::Specification.new do |s|
s.name = %q{vimeo}
s.version = "1.3.0"
s.version = "1.3.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Matt Hooks"]
s.date = %q{2009-07-11}
s.date = %q{2009-08-03}
s.description = %q{}
s.email = ["matthooks@gmail.com"]
s.extra_rdoc_files = ["Manifest.txt", "CHANGELOG.rdoc", "README.rdoc"]
Expand All @@ -25,13 +25,16 @@ Gem::Specification.new do |s|

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<jnunemaker-httparty>, [">= 0.2.6"])
s.add_runtime_dependency(%q<curb>, [">= 0"])
s.add_development_dependency(%q<hoe>, [">= 2.3.2"])
else
s.add_dependency(%q<jnunemaker-httparty>, [">= 0.2.6"])
s.add_dependency(%q<curb>, [">= 0"])
s.add_dependency(%q<hoe>, [">= 2.3.2"])
end
else
s.add_dependency(%q<jnunemaker-httparty>, [">= 0.2.6"])
s.add_dependency(%q<curb>, [">= 0"])
s.add_dependency(%q<hoe>, [">= 2.3.2"])
end
end

0 comments on commit ba44177

Please sign in to comment.