ucloud_storage 0.0.10 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,6 +15,6 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
-
19
18
  spec/fixtures/vcr_cassettes/*
20
- spec/support/auth_info.yml
19
+ spec/support/auth_info.yml
20
+ *.DS_Store
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ ## v0.0.12
2
+
3
+ * remove fork
4
+
1
5
  ## v0.0.2
2
6
 
3
7
  * Add runtime_dependency
data/README.md CHANGED
@@ -18,11 +18,15 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- ucloud = UcloudStorage.new(user: 'email', pass: 'API_KEY')
22
-
23
- ucloud.authoize
24
- ucloud.upload(filepath, boxname, destination)
25
- ucloud.delte(boxname, destination)
21
+ ucloud = UcloudStorage.new(user: 'email', pass: 'API_KEY')
22
+
23
+ ucloud.authoize
24
+ # Upload
25
+ ucloud.upload(filepath, boxname, destination)
26
+ # Delete
27
+ ucloud.delete(boxname, destination)
28
+ # Get
29
+ ucloud.get(boxname, destination)
26
30
 
27
31
  ## Configuration
28
32
 
@@ -11,6 +11,7 @@ module UcloudStorage
11
11
  def initialize(options={})
12
12
  @user = options.fetch(:user) { Configuration.user }
13
13
  @pass = options.fetch(:pass) { Configuration.pass }
14
+ @authorized = false
14
15
  end
15
16
 
16
17
  def authorize
@@ -24,27 +25,36 @@ module UcloudStorage
24
25
  when 200
25
26
  self.storage_url = response.headers["X-Storage-Url"]
26
27
  self.auth_token = response.headers["X-Auth-Token"]
27
- true
28
+ @authorized = true
28
29
  when 401 then false
29
30
  else raise TotalyWrongException
30
31
  end
31
32
  end
32
33
 
34
+ def is_authorized?
35
+ @authorized
36
+ end
37
+
33
38
  def upload(file_path, box_name, destination, &block)
34
39
  raise NotAuthorized if storage_url.nil?
35
40
 
36
41
  file = File.new(file_path)
37
- content_type = `file --mime-type #{file_path}`.split(": ").last
42
+ content_type = get_image_extension(file_path)
43
+ upload_blob(file.read, box_name, destination, content_type, &block)
44
+ end
45
+
46
+ def upload_blob(blob, box_name, destination, content_type, &block)
47
+ raise NotAuthorized if storage_url.nil?
38
48
 
39
49
  response = HTTParty.put(storage_url+ "/#{box_name}/#{destination}",
40
50
  headers: {
41
51
  "X-Auth-Token" => auth_token,
42
52
  "Content-Type" => content_type,
43
- "Content-Length" => file.size.to_s },
44
- body: file.read)
53
+ "Content-Length" => blob.size.to_s },
54
+ body: blob)
45
55
 
46
56
  if response.code == 401 and authorize
47
- return upload(file_path,box_name,destination,&block)
57
+ return upload_blob(blob, box_name, destination, content_type, &block)
48
58
  end
49
59
 
50
60
  yield response if block_given?
@@ -62,5 +72,52 @@ module UcloudStorage
62
72
 
63
73
  response.code == 204 ? true : false
64
74
  end
75
+
76
+ def get(box_name, destination)
77
+ raise NotAuthorized if storage_url.nil?
78
+
79
+ response = HTTParty.get(storage_url+ "/#{box_name}/#{destination}",
80
+ headers: { "X-Auth-Token" => auth_token })
81
+
82
+ yield response if block_given?
83
+
84
+ [200, 304].include?(response.code) ? true : false
85
+ end
86
+
87
+ def exist?(box_name, destination)
88
+ raise NotAuthorized if storage_url.nil?
89
+
90
+ response = HTTParty.head(storage_url+ "/#{box_name}/#{destination}",
91
+ headers: { "X-Auth-Token" => auth_token })
92
+
93
+ [200, 204].include?(response.code) ? true : false # Adding 200 for Ucloud's way
94
+ end
95
+
96
+ private
97
+
98
+ private
99
+
100
+ # stolen from http://stackoverflow.com/a/16636012/1802026
101
+ def get_image_extension(local_file_path)
102
+ png = Regexp.new("\x89PNG".force_encoding("binary"))
103
+ jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
104
+ jpg2 = Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"))
105
+ case IO.read(local_file_path, 10)
106
+ when /^GIF8/
107
+ 'gif'
108
+ when /^#{png}/
109
+ 'png'
110
+ when /^#{jpg}/
111
+ 'jpg'
112
+ when /^#{jpg2}/
113
+ 'jpg'
114
+ else
115
+ nil
116
+ # mime_type = `file #{local_file_path} --mime-type`.gsub("\n", '') # Works on linux and mac
117
+ # raise UnprocessableEntity, "unknown file type" if !mime_type
118
+ # mime_type.split(':')[1].split('/')[1].gsub('x-', '').gsub(/jpeg/, 'jpg').gsub(/text/, 'txt').gsub(/x-/, '')
119
+ end
120
+ end
121
+
65
122
  end
66
123
  end
@@ -1,3 +1,3 @@
1
1
  module UcloudStorage
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.12"
3
3
  end
@@ -43,9 +43,11 @@ describe UcloudStorage do
43
43
  describe '#authorize' do
44
44
  it "can authorize with valid user/pass" do
45
45
  VCR.use_cassette("storage/v1/auth") do
46
+ valid_ucloud.is_authorized?.should_not == true
46
47
  valid_ucloud.authorize.should == true
47
48
  valid_ucloud.storage_url.should_not be_nil
48
49
  valid_ucloud.auth_token.should_not be_nil
50
+ valid_ucloud.is_authorized?.should == true
49
51
  end
50
52
  end
51
53
 
@@ -157,4 +159,23 @@ describe UcloudStorage do
157
159
  end
158
160
  end
159
161
  end
162
+
163
+ describe "#exist" do
164
+ it 'should get updated object' do
165
+ valid_ucloud.authorize
166
+ file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
167
+ box = 'dev_box'
168
+ destination = 'cropped_images/'+Pathname(file_path).basename.to_s
169
+
170
+ VCR.use_cassette("v1/put_storage_object_02") do
171
+ valid_ucloud.upload(file_path, box, destination)
172
+ end
173
+
174
+ VCR.use_cassette("v1/get_storage_object") do
175
+ valid_ucloud.get(box, destination) do |response|
176
+ [200, 304].should include(response.code)
177
+ end.should == true
178
+ end
179
+ end
180
+ end
160
181
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ucloud_storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-06-12 00:00:00.000000000 Z
13
+ date: 2013-10-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &70267624686700 !ruby/object:Gem::Requirement
17
+ requirement: &70357224523380 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70267624686700
25
+ version_requirements: *70357224523380
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: vcr
28
- requirement: &70267624686280 !ruby/object:Gem::Requirement
28
+ requirement: &70357224522960 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70267624686280
36
+ version_requirements: *70357224522960
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: webmock
39
- requirement: &70267624685860 !ruby/object:Gem::Requirement
39
+ requirement: &70357224522540 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70267624685860
47
+ version_requirements: *70357224522540
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: httparty
50
- requirement: &70267624685440 !ruby/object:Gem::Requirement
50
+ requirement: &70357224538500 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: '0'
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *70267624685440
58
+ version_requirements: *70357224538500
59
59
  description: ucloud storage API
60
60
  email:
61
61
  - wangsy@wangsy.com