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
|
# File 'lib/avatars.io.rb', line 22
def self.upload(filename, identifier = '')
file = File.read filename
response = self.post '/v1/token', {
:headers => {
'x-client_id' => @@client_id,
'Authorization' => "OAuth #{ @@access_token }"
}, :body => {
:data => {
:filename => filename,
:md5 => Digest::MD5::hexdigest(file),
:size => File.size(filename),
:path => identifier
}
}.to_json
}
return response["data"]["url"] if !response["data"]["upload_info"]
info = response["data"]["upload_info"]
AvatarsIOFile.put info["upload_url"].gsub('http://s3.amazonaws.com', ''), {
:headers => {
'Authorization' => info["signature"],
'Date' => info["date"],
'Content-Type' => info["content_type"],
'x-amz-acl' => 'public-read'
},
:body => file
}
response = self.post "/v1/token/#{ response["data"]["id"] }/complete", {
:headers => {
'x-client_id' => @@client_id,
'Authorization' => "OAuth #{ @@access_token }"
},
:body => ''
}
response["data"]
end
|