Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 4 commits
  • 6 files changed
  • 0 commit comments
  • 2 contributors
Showing with 121 additions and 1 deletion.
  1. +5 −1 Gemfile.lock
  2. +2 −0 github.gemspec
  3. +50 −0 lib/commands/commands.rb
  4. +57 −0 lib/commands/helpers.rb
  5. +4 −0 lib/github.rb
  6. +3 −0 spec/resources/help_output.txt
View
6 Gemfile.lock
@@ -5,25 +5,29 @@ PATH
highline (~> 1.5.1)
json (~> 1.4.6)
launchy (~> 0.3.7)
+ mime-types (= 1.16)
text-format (= 1.0.0)
+ xml-simple (= 1.0.14)
GEM
remote: http://rubygems.org/
specs:
activerecord (2.3.10)
activesupport (= 2.3.10)
activesupport (2.3.10)
- configuration (1.1.0)
+ configuration (1.2.0)
highline (1.5.2)
json (1.4.6)
launchy (0.3.7)
configuration (>= 0.0.5)
rake (>= 0.8.1)
+ mime-types (1.16)
rake (0.8.7)
rspec (1.3.1)
text-format (1.0.0)
text-hyphen (~> 1.0.0)
text-hyphen (1.0.0)
+ xml-simple (1.0.14)
PLATFORMS
ruby
View
2 github.gemspec
@@ -23,6 +23,8 @@ Gem::Specification.new do |s|
s.add_dependency "highline", "~> 1.5.1"
s.add_dependency "json", "~> 1.4.6"
s.add_dependency "launchy", "~> 0.3.7"
+ s.add_dependency "xml-simple", "1.0.14"
+ s.add_dependency "mime-types", "1.16"
s.add_development_dependency "rake"
s.add_development_dependency "rspec", "~>1.3.1"
View
50 lib/commands/commands.rb
@@ -264,3 +264,53 @@
puts "No results found"
end
end
+
+desc "Uploads a file to GitHub's non-repo storage"
+usage "github upload [filename]"
+usage "github upload [filename] [user]/[repo]"
+command :upload do |filename, user, repo|
+ die "Specify a file to upload" if filename.nil?
+ if repo.nil?
+ if user
+ user, repo = user.split('/')
+ else
+ user = helper.owner
+ repo = helper.project
+ end
+ end
+ die "Cannot determine GitHub repo" if user.nil? || repo.nil?
+
+ die "Target file does not exist" unless File.size?(filename)
+ file = File.new(filename)
+ mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]
+
+ res = helper.http_get "https://github.com/#{user}/#{repo}/downloads?login=#{github_user}&token=#{github_token}"
+ is_public = res.body =~ /You are being <a href="http:\/\/github.com/
+ res = helper.http_get "#{schema}://github.com/#{user}/#{repo}/downloads?login=#{github_user}&token=#{github_token}" if is_public
+ die "File has already been uploaded" if res.body =~ /<td><a href=".+?\/downloads\/#{user}\/#{repo}\/#{filename}.*">#{filename}<\/a><\/td>/
+
+ res = helper.http_post("https://github.com/#{user}/#{repo}/downloads", {
+ :file_size => File.size(filename),
+ :content_type => mime_type.simplified,
+ :file_name => filename,
+ :description => '',
+ :login => github_user,
+ :token => github_token,
+ })
+ die "Repo not found" if res.class == Net::HTTPNotFound
+ data = XmlSimple.xml_in(res.body)
+ die "Unable to authorize upload" if data["signature"].nil?
+
+ res = helper.http_post_multipart("http://github.s3.amazonaws.com/", {
+ :key => "#{data["prefix"].first}#{filename}",
+ :Filename => filename,
+ :policy => data["policy"].first,
+ :AWSAccessKeyId => data["accesskeyid"].first,
+ :signature => data["signature"].first,
+ :acl => data["acl"].first,
+ :file => file,
+ :success_action_status => 201
+ })
+ die "File upload failed" unless res.class == Net::HTTPCreated
+ puts "File uploaded successfully"
+end
View
57 lib/commands/helpers.rb
@@ -492,3 +492,60 @@
end
puts "-----"
end
+
+helper :http_get do |url|
+ parsed_url = URI.parse(url)
+ http = Net::HTTP.new(parsed_url.host, parsed_url.port)
+ http.use_ssl = parsed_url.scheme == 'https'
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ http.get(parsed_url.request_uri)
+end
+
+helper :url_encode do |str|
+ str.to_s.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0])}
+end
+
+helper :http_post do |url,params|
+ parsed_url = URI.parse(url)
+ http = Net::HTTP.new(parsed_url.host, parsed_url.port)
+ http.use_ssl = parsed_url.scheme == 'https'
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+
+ req = Net::HTTP::Post.new(parsed_url.path)
+ req.body = params.map {|k,v| "#{url_encode(k)}=#{url_encode(v)}" }.join('&')
+ req.content_type = 'application/x-www-form-urlencoded'
+ http.request req
+end
+
+helper :prepare_param do |name, value|
+ "Content-Disposition: form-data; name=\"#{url_encode(name)}\"\r\n\r\n#{value}\r\n"
+end
+
+helper :http_post_multipart do |url, params|
+ parsed_url = URI.parse(url)
+ http = Net::HTTP.new(parsed_url.host, parsed_url.port)
+ http.use_ssl = parsed_url.scheme == 'https'
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+
+ boundary = "#{rand(1000000)}boundaryofdoomydoom#{rand(1000000)}"
+
+ fp = []
+ files = []
+
+ params.each do |k,v|
+ if v.respond_to?(:path) and v.respond_to?(:read) then
+ filename = v.path
+ content = v.read
+ mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]
+ fp.push(prepare_param("Content-Type", mime_type.simplified))
+ files.push("Content-Disposition: form-data; name=\"#{url_encode(k.to_s)}\"; filename=\"#{ filename }\"\r\nContent-Type: #{ mime_type.simplified }\r\n\r\n#{ content }\r\n")
+ else
+ fp.push(prepare_param(k,v))
+ end
+ end
+
+ http.post(parsed_url.path, "--#{boundary}\r\n" + (fp + files).join("--#{boundary}\r\n") + "--#{boundary}--", {
+ "Content-Type" => "multipart/form-data; boundary=#{boundary}",
+ "User-Agent" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6"
+ })
+end
View
4 lib/github.rb
@@ -4,11 +4,15 @@
require 'github/helper'
require 'github/ui'
require 'fileutils'
+require "net/http"
+require 'net/https'
require 'rubygems'
require 'open-uri'
require 'json'
require 'yaml'
require 'text/format'
+require 'xmlsimple'
+require 'mime/types'
##
# Starting simple.
View
3 spec/resources/help_output.txt
@@ -70,3 +70,6 @@ Available commands:
--private: Use git@github.com: instead of
git://github.com/.
--ssh : Equivalent to --private
+ upload => Uploads a file to GitHub's non-repo storage
+ % github upload [filename]
+ % github upload [filename] [user]/[repo]

No commit comments for this range

Something went wrong with that request. Please try again.