#require 'httpclient' require 'net/http' require 'net/https' require 'xmlsimple' require 'json' module Releaseable module GitHub OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE def self.post address, data uri = URI(address) server = Net::HTTP.new(uri.host, uri.port) server.use_ssl = (uri.scheme == 'https') server.verify_mode = OpenSSL::SSL::VERIFY_NONE if server.use_ssl? server.start do |http| req = Net::HTTP::Post.new(uri.path) case data when Hash req.form_data = data when Array req.body = data req.content_type = 'multipart/form-data' end http.request(req) end end def self.upload info = {} info[:login] ||= `git config github.user`.chomp info[:token] ||= `git config github.token`.chomp raise "login or token is empty" if info[:login].empty? or info[:token].empty? raise "required repository name" unless info[:repos] raise "required file to upload" unless info[:file] info[:repos] = info[:login] + '/' + info[:repos] unless info[:repos].include? '/' if info[:file] file = info[:file] raise "bad file #{info[:file]}" unless File.exist?(file) && File.readable?(file) info[:name] ||= File.basename(file) # + rand(1000).to_s end raise "required name for filename with data parameter" unless info[:name] info[:content_type] ||= 'application/octet-stream' response = post "https://github.com/#{info[:repos]}/downloads", 'file_size' => File.stat(info[:file]).size, 'content_type' => info[:content_type], 'file_name' => info[:name], 'description' => info[:description] || '', 'login' => info[:login], 'token' => info[:token] #p response.body raise "Failed to post file info" unless response.code.to_i == 200 #status == 200 upload = JSON.parse(response.body) #upload.each { |k, v| print "#{k}:"; p v } #sec = Time.utc(*upload['expirationdate'].split(/[-T:]/)).to_i curl = "curl -F \"key=#{upload['path']}\" -F \"acl=#{upload['acl']}\" " + "-F \"success_action_status=201\" -F \"Filename=#{info[:name]}\" " + "-F \"AWSAccessKeyId=#{upload['accesskeyid']}\" -F \"Policy=#{upload['policy']}\" "+ "-F \"Signature=#{upload['signature']}\" -F \"Content-Type=application/octet-stream\" "+ "-F \"file=@#{info[:file]}\" https://github.s3.amazonaws.com/" response = `#{curl}` analyze_curl_response response #f = File.open(info[:file], 'rb') ###stat = HTTPClient.post("http://github.s3.amazonaws.com/", [ #response = post "http://github.s3.amazonaws.com/", # 'key' => upload['path'], #.gsub(/\//, '%2F'), # 'acl' => upload['acl'], #"public-read", # # 'success_action_status' => 201, # 'Filename' => info[:name], # 'AWSAccessKeyId' => upload['accesskeyid'], # 'Policy' => upload['policy'], # #'policy' => upload['policy'], # #'signature' => upload['signature'], # 'Signature' => upload['signature'], # 'Expires' => 1483421360, #sec, #upload['expirationdate'], # 'Content-Type' => upload['mime_type'] || 'application/octet-stream', # 'file' => f ##response = post "http://github.s3.amazonaws.com/", ## 'Filename' => info[:name], ## 'policy' => upload['policy'], ## 'success_action_status' => 201, ## 'key' => upload['path'].gsub(/\//, '%2F'), ## 'AWSAccessKeyId' => upload['accesskeyid'], ## 'Content-Type' => upload['mime_type'] || 'application/octet-stream', ## 'signature' => upload['signature'], ## 'acl' => upload['acl'], #"public-read", # ## 'file' => f # ##stat = HTTPClient.post("http://github.s3.amazonaws.com/", [ ## ['Filename', info[:name]], ## ['policy', upload_info['policy']], ## ['success_action_status', 201], ## ['key', upload_info['path']], ## ['AWSAccessKeyId', upload_info['accesskeyid']], ## ['Content-Type', upload_info['content_type'] || 'application/octet-stream'], ## ['signature', upload_info['signature']], ## ['acl', upload_info['acl']], ## ['file', f] ##]) #f.close #analyze_aws_response response end private def self.analyze_curl_response response message = XmlSimple.xml_in(response) if message['Location'] message['Location'].first else raise "Failed to upload to AWS" + (" due to #{message}" rescue '') end end def self.analyze_aws_response response message = XmlSimple.xml_in(response.body) if response.code.to_i == 201 message['PostResponse']['Location'] else #error = message['Error'] raise "Failed to upload" + (" due to #{message['Code']} (#{message['Message']})" rescue '') end end end end