Module: Releaseable::GitHub

Defined in:
lib/releaseable/github.rb

Class Method Summary collapse

Class Method Details

.upload(info = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/releaseable/github.rb', line 6

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 = HTTP.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