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
91
92
93
94
95
96
97
98
99
|
# File 'lib/net/github-upload.rb', line 19
def upload info
unless info[:repos]
raise "required repository name"
end
info[:repos] = @login + '/' + info[:repos] unless info[:repos].include? '/'
if info[:file]
file = info[:file]
unless File.exist?(file) && File.readable?(file)
raise "file does not exsits or readable"
end
info[:name] ||= File.basename(file)
end
unless info[:file] || info[:data]
raise "required file or data parameter to upload"
end
unless info[:name]
raise "required name parameter for filename with data parameter"
end
if info[:replace]
list_files(info[:repos]).each { |obj|
next unless obj[:name] == info[:name]
HTTPClient.post("http://github.com/#{info[:repos]}/downloads/#{obj[:id].gsub( "download_", '')}", {
"_method" => "delete",
"login" => @login,
"token" => @token
})
}
elsif list_files(info[:repos]).any?{|obj| obj[:name] == info[:name]}
raise "file '#{info[:name]}' is already uploaded. please try different name"
end
info[:content_type] ||= 'application/octet-stream'
stat = HTTPClient.post("http://github.com/#{info[:repos]}/downloads", {
"file_size" => info[:file] ? File.stat(info[:file]).size : info[:data].size,
"content_type" => info[:content_type],
"file_name" => info[:name],
"description" => info[:description] || '',
"login" => @login,
"token" => @token
})
unless stat.code == 200
raise "Failed to post file info"
end
upload_info = FasterXmlSimple.xml_in(stat.content)['hash']
if info[:file]
f = File.open(info[:file], 'rb')
stat = HTTPClient.post("http://github.s3.amazonaws.com/", [
['Filename', info[:name]],
['policy', upload_info['policy']],
['success_action_status', 201],
['key', upload_info['prefix'] + info[:name]],
['AWSAccessKeyId', upload_info['accesskeyid']],
['signature', upload_info['signature']],
['acl', upload_info['acl']],
['file', f]
])
f.close
else
stat = HTTPClient.post("http://github.s3.amazonaws.com/", [
['Filename', info[:name]],
['policy', upload_info['policy']],
['success_action_status', 201],
['key', upload_info['prefix'] + info[:name]],
['AWSAccessKeyId', upload_info['accesskeyid']],
['signature', upload_info['signature']],
['acl', upload_info['acl']],
['file', StringIO.new(info[:data])]
])
end
if stat.code == 201
return FasterXmlSimple.xml_in(stat.content)['PostResponse']['Location']
else
raise 'Failed to upload' + (stat)
end
end
|