qiniu-rs 3.2.2 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/qiniu/rs.rb CHANGED
@@ -17,6 +17,7 @@ module Qiniu
17
17
  autoload :AccessToken, 'qiniu/tokens/access_token'
18
18
  autoload :QboxToken, 'qiniu/tokens/qbox_token'
19
19
  autoload :UploadToken, 'qiniu/tokens/upload_token'
20
+ autoload :DownloadToken, 'qiniu/tokens/download_token'
20
21
  autoload :Abstract, 'qiniu/rs/abstract'
21
22
 
22
23
  class << self
@@ -100,12 +101,14 @@ module Qiniu
100
101
  end
101
102
 
102
103
  def upload_file opts = {}
103
- [:uptoken, :file, :bucket, :key].each do |opt|
104
- raise MissingArgsError, [opt] unless opts.has_key?(opt)
105
- end
104
+ uncontained_opts = [:uptoken, :file, :bucket, :key] - opts.keys
105
+ raise MissingArgsError, uncontained_opts unless uncontained_opts.empty?
106
+
106
107
  source_file = opts[:file]
107
108
  raise NoSuchFileError, source_file unless File.exist?(source_file)
109
+
108
110
  opts[:enable_resumable_upload] = true unless opts.has_key?(:enable_resumable_upload)
111
+
109
112
  if opts[:enable_resumable_upload] && File::size(source_file) > Config.settings[:block_size]
110
113
  code, data = UP.upload_with_token(opts[:uptoken],
111
114
  opts[:file],
@@ -230,6 +233,15 @@ module Qiniu
230
233
  token_obj.generate_token
231
234
  end
232
235
 
236
+ def generate_download_token(opts = {})
237
+ token_obj = DownloadToken.new(opts)
238
+ token_obj.access_key = Config.settings[:access_key]
239
+ token_obj.secret_key = Config.settings[:secret_key]
240
+ #token_obj.expires_in = opts[:expires_in]
241
+ #token_obj.pattern = opts[:pattern]
242
+ token_obj.generate_token
243
+ end
244
+
233
245
  end
234
246
 
235
247
  end
data/lib/qiniu/rs/up.rb CHANGED
@@ -58,7 +58,7 @@ module Qiniu
58
58
  class TmpData
59
59
  def initialize(dir, filename)
60
60
  @tmpdir = Config.settings[:tmpdir] + File::SEPARATOR + dir
61
- FileUtils.mkdir_p(@tmpdir) unless Dir.exists?(@tmpdir)
61
+ FileUtils.mkdir_p(@tmpdir) unless File.directory?(@tmpdir)
62
62
  @tmpfile = @tmpdir + File::SEPARATOR + filename
63
63
  end
64
64
 
@@ -83,7 +83,7 @@ module Qiniu
83
83
  end
84
84
 
85
85
  def sweep!
86
- FileUtils.rm_r(@tmpdir) if Dir.exists?(@tmpdir)
86
+ FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir)
87
87
  end
88
88
  end
89
89
 
@@ -100,7 +100,6 @@ module Qiniu
100
100
  customer = nil,
101
101
  callback_params = nil,
102
102
  rotate = nil)
103
- raise NoSuchFileError, local_file unless File.exist?(local_file)
104
103
  begin
105
104
  ifile = File.open(local_file, 'rb')
106
105
  fh = FileData.new(ifile)
@@ -110,11 +109,7 @@ module Qiniu
110
109
  mime = MIME::Types.type_for local_file
111
110
  mime_type = mime.empty? ? 'application/octet-stream' : mime[0].content_type
112
111
  end
113
- if fsize > Config.settings[:block_size]
114
- code, data = _resumable_upload(uptoken, fh, fsize, bucket, key, mime_type, custom_meta, customer, callback_params, rotate)
115
- else
116
- code, data = IO.upload_with_token(uptoken, local_file, bucket, key, mime_type, custom_meta, callback_params, true, rotate)
117
- end
112
+ code, data = _resumable_upload(uptoken, fh, fsize, bucket, key, mime_type, custom_meta, customer, callback_params, rotate)
118
113
  [code, data]
119
114
  ensure
120
115
  ifile.close unless ifile.nil?
@@ -175,7 +170,7 @@ module Qiniu
175
170
  _call_binary_with_token(uptoken, url, body)
176
171
  end
177
172
 
178
- def _resumable_put_block(uptoken, fh, block_index, block_size, chunk_size, progress, retry_times = 1, notifier)
173
+ def _resumable_put_block(uptoken, fh, block_index, block_size, chunk_size, progress, retry_times, notifier)
179
174
  code, data = 0, {}
180
175
  fpath = fh.path
181
176
  # this block has never been uploaded.
@@ -323,6 +318,7 @@ module Qiniu
323
318
  end
324
319
 
325
320
  end
321
+
326
322
  end
327
323
  end
328
324
  end
@@ -4,8 +4,8 @@ module Qiniu
4
4
  module RS
5
5
  module Version
6
6
  MAJOR = 3
7
- MINOR = 2
8
- PATCH = 2
7
+ MINOR = 3
8
+ PATCH = 0
9
9
  # Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
10
10
  #
11
11
  # Example
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'json'
4
+ require 'qiniu/tokens/access_token'
5
+ require 'qiniu/rs/utils'
6
+
7
+ module Qiniu
8
+ module RS
9
+ class DownloadToken < AccessToken
10
+
11
+ include Utils
12
+
13
+ attr_accessor :pattern, :expires_in
14
+
15
+ def initialize(opts = {})
16
+ @pattern = opts[:pattern] || "*"
17
+ @expires_in = opts[:expires_in] || 3600
18
+ end
19
+
20
+ def generate_signature
21
+ params = {"S" => @pattern, "E" => Time.now.to_i + @expires_in}
22
+ Utils.urlsafe_base64_encode(params.to_json)
23
+ end
24
+
25
+ def generate_token
26
+ signature = generate_signature
27
+ encoded_digest = generate_encoded_digest(signature)
28
+ %Q(#{@access_key}:#{encoded_digest}:#{signature})
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -8,6 +8,8 @@ module Qiniu
8
8
  module RS
9
9
  class QboxToken < AccessToken
10
10
 
11
+ include Utils
12
+
11
13
  attr_accessor :url, :params
12
14
 
13
15
  def initialize(opts = {})
@@ -20,16 +22,16 @@ module Qiniu
20
22
  signature = uri.path
21
23
  query_string = uri.query
22
24
  signature += '?' + query_string if !query_string.nil? && !query_string.empty?
23
- signature += "\n";
25
+ signature += "\n"
24
26
  if @params.is_a?(Hash)
25
- total_param = @params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) }
26
- signature += total_param.join("&")
27
+ params_string = Utils.generate_query_string(@params)
28
+ signature += params_string
27
29
  end
28
30
  signature
29
31
  end
30
32
 
31
33
  def generate_token
32
- encoded_digest = generate_encoded_digest(signature)
34
+ encoded_digest = generate_encoded_digest(generate_signature)
33
35
  %Q(#{@access_key}:#{encoded_digest})
34
36
  end
35
37
 
@@ -27,7 +27,7 @@ module Qiniu
27
27
  params[:callbackBodyType] = @callback_body_type if !@callback_body_type.nil? && !@callback_body_type.empty?
28
28
  params[:customer] = @customer if !@customer.nil? && !@customer.empty?
29
29
  params[:escape] = 1 if @escape == 1 || @escape == true
30
- urlsafe_base64_encode(params.to_json)
30
+ Utils.urlsafe_base64_encode(params.to_json)
31
31
  end
32
32
 
33
33
  def generate_token
data/qiniu-rs.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
18
18
 
19
19
  # specify any dependencies here; for example:
20
20
  gem.add_development_dependency "rake", ">= 0.9"
21
- gem.add_development_dependency "rspec", "~> 2.11"
21
+ gem.add_development_dependency "rspec", ">= 2.11"
22
22
  gem.add_development_dependency "fakeweb", "~> 1.3"
23
23
  gem.add_runtime_dependency "json", "~> 1.7"
24
24
  gem.add_runtime_dependency "rest-client", "~> 1.6"
@@ -41,6 +41,7 @@ module Qiniu
41
41
  result.should_not be_false
42
42
  end
43
43
 
44
+ =begin
44
45
  context ".set_watermark" do
45
46
  it "should works" do
46
47
  options = {
@@ -59,6 +60,7 @@ module Qiniu
59
60
  puts data.inspect
60
61
  end
61
62
  end
63
+ =end
62
64
 
63
65
  end
64
66
  end
@@ -1,4 +1,4 @@
1
- # Utils.-*- encoding: utf-8 -*-
1
+ # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'spec_helper'
4
4
  require 'qiniu/rs/auth'
@@ -11,7 +11,7 @@ module Qiniu
11
11
 
12
12
  before :all do
13
13
  @bucket = "io_test_bucket"
14
- @key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s
14
+ @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s)
15
15
 
16
16
  result = Qiniu::RS.mkbucket(@bucket)
17
17
  puts result.inspect
@@ -12,7 +12,7 @@ module Qiniu
12
12
 
13
13
  before :all do
14
14
  @bucket = "rs_test_bucket"
15
- @key = Digest::SHA1.hexdigest (Time.now.to_i+rand(100)).to_s
15
+ @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s)
16
16
  @domain = 'rs-test-bucket.dn.qbox.me'
17
17
 
18
18
  code, data = Qiniu::RS::RS.mkbucket(@bucket)
@@ -1,4 +1,4 @@
1
- # Utils.-*- encoding: utf-8 -*-
1
+ # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'digest/sha1'
4
4
  require 'spec_helper'
@@ -11,7 +11,7 @@ module Qiniu
11
11
 
12
12
  before :all do
13
13
  @localfile = "bigfile.txt"
14
- File.open(@localfile, "w"){|f| 5242888.times{f.write(Random.rand(9).to_s)}}
14
+ File.open(@localfile, "w"){|f| 5242888.times{f.write(rand(9).to_s)}}
15
15
  @bucket = "up_test_bucket"
16
16
  @key = Digest::SHA1.hexdigest(@localfile+Time.now.to_s)
17
17
 
@@ -3,6 +3,7 @@
3
3
  require 'digest/sha1'
4
4
  require 'spec_helper'
5
5
  require 'qiniu/rs'
6
+ require 'qiniu/rs/exceptions'
6
7
 
7
8
  module Qiniu
8
9
  describe RS do
@@ -30,10 +31,13 @@ module Qiniu
30
31
  end
31
32
 
32
33
  after :all do
33
- result = Qiniu::RS.drop(@bucket)
34
- puts result.inspect
34
+ result = Qiniu::RS.unpublish(@domain)
35
35
  result.should_not be_false
36
36
 
37
+ result1 = Qiniu::RS.drop(@bucket)
38
+ puts result1.inspect
39
+ result1.should_not be_false
40
+
37
41
  result2 = Qiniu::RS.drop(@test_image_bucket)
38
42
  puts result2.inspect
39
43
  result2.should_not be_false
@@ -79,6 +83,7 @@ module Qiniu
79
83
  end
80
84
  end
81
85
 
86
+ =begin
82
87
  context ".set_watermark" do
83
88
  it "should works" do
84
89
  options = {
@@ -97,6 +102,7 @@ module Qiniu
97
102
  puts result.inspect
98
103
  end
99
104
  end
105
+ =end
100
106
 
101
107
  context ".put_auth" do
102
108
  it "should works" do
@@ -148,13 +154,36 @@ module Qiniu
148
154
  result.should_not be_false
149
155
  puts result.inspect
150
156
  end
157
+
158
+ it "should raise MissingArgsError" do
159
+ uptoken_opts = {:scope => @bucket, :escape => 0}
160
+ upload_opts = {
161
+ :uptoken => Qiniu::RS.generate_upload_token(uptoken_opts),
162
+ :file => __FILE__,
163
+ :key => @key,
164
+ :enable_crc32_check => true
165
+ }
166
+ lambda { Qiniu::RS.upload_file(upload_opts) }.should raise_error(RS::MissingArgsError)
167
+ end
168
+
169
+ it "should raise NoSuchFileError" do
170
+ uptoken_opts = {:scope => @bucket, :escape => 0}
171
+ upload_opts = {
172
+ :uptoken => Qiniu::RS.generate_upload_token(uptoken_opts),
173
+ :file => 'no_this_file',
174
+ :bucket => @bucket,
175
+ :key => @key,
176
+ :enable_crc32_check => true
177
+ }
178
+ lambda { Qiniu::RS.upload_file(upload_opts) }.should raise_error(RS::NoSuchFileError)
179
+ end
151
180
  end
152
181
 
153
182
  context ".resumable_upload_file" do
154
183
  it "should works" do
155
184
  # generate bigfile for testing
156
185
  localfile = "test_bigfile"
157
- File.open(localfile, "w"){|f| 5242888.times{f.write(Random.rand(9).to_s)}}
186
+ File.open(localfile, "w"){|f| 5242888.times{f.write(rand(9).to_s)}}
158
187
  key = Digest::SHA1.hexdigest(localfile+Time.now.to_s)
159
188
  # generate the upload token
160
189
  uptoken_opts = {:scope => @bucket, :expires_in => 3600, :customer => "awhy.xu@gmail.com", :escape => 0}
@@ -255,12 +284,14 @@ module Qiniu
255
284
  end
256
285
  end
257
286
 
287
+ =begin
258
288
  context ".unpublish" do
259
289
  it "should works" do
260
290
  result = Qiniu::RS.unpublish(@domain)
261
291
  result.should_not be_false
262
292
  end
263
293
  end
294
+ =end
264
295
 
265
296
  context ".delete" do
266
297
  it "should works" do
@@ -334,6 +365,16 @@ module Qiniu
334
365
  data = Qiniu::RS.generate_upload_token({:scope => 'test_bucket', :expires_in => 3600, :escape => 0})
335
366
  data.should_not be_empty
336
367
  puts data.inspect
368
+ data.split(":").length.should == 3
369
+ end
370
+ end
371
+
372
+ context ".generate_download_token" do
373
+ it "should works" do
374
+ data = Qiniu::RS.generate_download_token({:expires_in => 1, :pattern => 'http://qiniu-rs-test.dn.qbox.me/*'})
375
+ data.should_not be_empty
376
+ puts data.inspect
377
+ data.split(":").length.should == 3
337
378
  end
338
379
  end
339
380
 
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/tokens/qbox_token'
5
+
6
+ module Qiniu
7
+ module RS
8
+ describe QboxToken do
9
+ before :all do
10
+ @qbox_token = QboxToken.new(:url => 'www.qiniutek.com?key1=value1',
11
+ :params => { :key2 => 'value2' })
12
+ @qbox_token.access_key = 'access_key'
13
+ @qbox_token.secret_key = 'secret_key'
14
+ end
15
+
16
+ context "#generate_token" do
17
+ it "should generate token" do
18
+ @qbox_token.generate_token.should_not be_empty
19
+ end
20
+ end
21
+
22
+ context "#generate_signature" do
23
+ it "should generate signature" do
24
+ @qbox_token.generate_signature.should == "www.qiniutek.com?key1=value1\nkey2=value2"
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qiniu-rs
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 3.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-05 00:00:00.000000000 Z
12
+ date: 2013-01-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -32,7 +32,7 @@ dependencies:
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ~>
35
+ - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
37
  version: '2.11'
38
38
  type: :development
@@ -40,7 +40,7 @@ dependencies:
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ~>
43
+ - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '2.11'
46
46
  - !ruby/object:Gem::Dependency
@@ -156,6 +156,7 @@ files:
156
156
  - lib/qiniu/rs/utils.rb
157
157
  - lib/qiniu/rs/version.rb
158
158
  - lib/qiniu/tokens/access_token.rb
159
+ - lib/qiniu/tokens/download_token.rb
159
160
  - lib/qiniu/tokens/qbox_token.rb
160
161
  - lib/qiniu/tokens/upload_token.rb
161
162
  - qiniu-rs.gemspec
@@ -171,6 +172,7 @@ files:
171
172
  - spec/qiniu/rs/utils_spec.rb
172
173
  - spec/qiniu/rs/version_spec.rb
173
174
  - spec/qiniu/rs_spec.rb
175
+ - spec/qiniu/tokens/qbox_token_spec.rb
174
176
  - spec/spec_helper.rb
175
177
  homepage: https://github.com/qiniu/ruby-sdk
176
178
  licenses: []
@@ -186,7 +188,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
186
188
  version: '0'
187
189
  segments:
188
190
  - 0
189
- hash: -4300609686795785448
191
+ hash: 3127268457999881368
190
192
  required_rubygems_version: !ruby/object:Gem::Requirement
191
193
  none: false
192
194
  requirements:
@@ -195,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
197
  version: '0'
196
198
  segments:
197
199
  - 0
198
- hash: -4300609686795785448
200
+ hash: 3127268457999881368
199
201
  requirements: []
200
202
  rubyforge_project:
201
203
  rubygems_version: 1.8.24
@@ -215,4 +217,5 @@ test_files:
215
217
  - spec/qiniu/rs/utils_spec.rb
216
218
  - spec/qiniu/rs/version_spec.rb
217
219
  - spec/qiniu/rs_spec.rb
220
+ - spec/qiniu/tokens/qbox_token_spec.rb
218
221
  - spec/spec_helper.rb