qiniu 6.2.0 → 6.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: beb8c598a8ae3aabebea6aca70a992aa8cbfb20a
4
- data.tar.gz: a23e588d3da392c136bb4112f17163658de5dc8f
3
+ metadata.gz: d8305d0d51c6dd65343f634f175edb242bc81733
4
+ data.tar.gz: ae6055351cd00d341487bd545aef683ead3ae99f
5
5
  SHA512:
6
- metadata.gz: 328f544c77cbe62e969704ce39f84a5f2a1f71e5d284cfadb3d5b6750ecef3002c48f5421cea24e7a6711e4a563d6eba908e13fc7abaf3f9d12c944304a9027b
7
- data.tar.gz: 872dc200b4fa640c965f3b10416c210142f302cc0cbd0c81e0010aab3727c520d7cb2ad0190065a32cb2b1456cee5edd31cf5230d30767f62dfd88cc0bf5f3d4
6
+ metadata.gz: 729967aa45f514f1d9b205c9b8eeabc0d886634dfcfd7b38d3a89326c6e01bdb95966cd19de1e5e0fc7d0a94e5fe76715ff514ca5c2038b1d68c379c5b914f28
7
+ data.tar.gz: 490c565dcfed6aca96e917d8228e0634c263ca52b7ed34dfc31d5ffc661dc3d6f82b39db72b1b9e196543b87ddaaabe56bc77cbc45b75f1cb8166617d77e3f66
@@ -1,13 +1,9 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.8.7
4
- - 1.9.2
5
3
  - 1.9.3
6
4
  - 2.0.0
7
5
  - 2.1.1
8
- - jruby-18mode
9
6
  - jruby-19mode
10
- - ree
11
7
  before_script:
12
8
  - export QINIU_ACCESS_KEY=LGRqJsjX7LAvtdtX6hhyxs861lS57HwnOQmJsOuX
13
9
  - export QINIU_SECRET_KEY=g_OxhuN5iJ1O7BoA6YdXyF6zSszk34UZFKDfdIIW
@@ -1,5 +1,9 @@
1
1
  ## CHANGE LOG
2
2
 
3
+ ### v6.2.1
4
+
5
+ - 去除已废弃的publish/unpublish接口。 [https://github.com/qiniu/ruby-sdk/pull/90](https://github.com/qiniu/ruby-sdk/pull/90) (#8143)
6
+
3
7
  ### v6.2.0
4
8
 
5
9
  - 重写与授权相关的函数并归入Qiniu::Auth空间,原授权凭证生成类维持不变。
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- qiniu (6.2.0)
4
+ qiniu (6.2.1)
5
5
  json (~> 1.7)
6
6
  mime-types (~> 1.19)
7
7
  rest-client (~> 1.6)
data/README.md CHANGED
@@ -9,6 +9,7 @@
9
9
  [![Build Status](https://api.travis-ci.org/qiniu/ruby-sdk.png?branch=master)](https://travis-ci.org/qiniu/ruby-sdk)
10
10
  [![Gem Version](https://badge.fury.io/rb/qiniu.png)](http://badge.fury.io/rb/qiniu)
11
11
  [![Dependency Status](https://gemnasium.com/qiniu/ruby-sdk.png)](https://gemnasium.com/qiniu/ruby-sdk)
12
+ [![Code Climate](https://codeclimate.com/github/qiniu/ruby-sdk.png)](https://codeclimate.com/github/qiniu/ruby-sdk)
12
13
 
13
14
  ## 安装
14
15
 
@@ -64,7 +64,7 @@ module Qiniu
64
64
  opts[:enable_resumable_upload] = true unless opts.has_key?(:enable_resumable_upload)
65
65
 
66
66
  if opts[:enable_resumable_upload] && File::size(source_file) > Config.settings[:block_size]
67
- code, data = Storage.upload_with_token(opts[:uptoken],
67
+ code, data, raw_headers = Storage.upload_with_token(opts[:uptoken],
68
68
  opts[:file],
69
69
  opts[:bucket],
70
70
  opts[:key],
@@ -74,7 +74,7 @@ module Qiniu
74
74
  opts[:callback_params],
75
75
  opts[:rotate])
76
76
  else
77
- code, data = Storage.upload_with_token(opts[:uptoken],
77
+ code, data, raw_headers = Storage.upload_with_token(opts[:uptoken],
78
78
  opts[:file],
79
79
  opts[:bucket],
80
80
  opts[:key],
@@ -156,16 +156,6 @@ module Qiniu
156
156
  code == StatusOK ? data : false
157
157
  end
158
158
 
159
- def publish(domain, bucket)
160
- code, data = Storage.publish(domain, bucket)
161
- code == StatusOK
162
- end
163
-
164
- def unpublish(domain)
165
- code, data = Storage.unpublish(domain)
166
- code == StatusOK
167
- end
168
-
169
159
  def drop(bucket)
170
160
  code, data = Storage.drop(bucket)
171
161
  code == StatusOK
@@ -0,0 +1,46 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # vim: sw=2 ts=2
3
+
4
+ require 'uri'
5
+
6
+ module Qiniu
7
+ module ADT
8
+
9
+ class ApiSpecification
10
+ public
11
+ def to_str; return ""; end
12
+ end # class ApiSpecification
13
+
14
+ module Policy
15
+ public
16
+ def to_json
17
+ args = {}
18
+
19
+ self.params.each_pair do |key, fld|
20
+ val = self.__send__(key)
21
+ if !val.nil? then
22
+ args[fld] = val
23
+ end
24
+ end
25
+
26
+ return args.to_json
27
+ end # to_json
28
+
29
+ def to_query_string
30
+ args = []
31
+
32
+ self.params.each_pair do |key, fld|
33
+ val = self.__send__(key)
34
+ if !val.nil? then
35
+ new_fld = CGI.escape(fld.to_s)
36
+ new_val = CGI.escape(val.to_s).gsub('+', '%20')
37
+ args.push("#{new_fld}=#{new_val}")
38
+ end
39
+ end
40
+
41
+ return args.join("&")
42
+ end # to_query_string
43
+ end # module Policy
44
+
45
+ end # module ADT
46
+ end # module Qiniu
@@ -132,22 +132,6 @@ module Qiniu
132
132
  end # class PutPolicy
133
133
 
134
134
  class << self
135
-
136
- include Utils
137
-
138
- def call_with_signature(url, data, retry_times = 0, options = {})
139
- return Utils.http_request(
140
- url,
141
- data,
142
- options.merge({:qbox_signature_token => generate_acctoken(url, data)})
143
- )
144
- end # call_with_signature
145
-
146
- def request(url, data = nil, options = {})
147
- code, data, raw_headers = Auth.call_with_signature(url, data, 0, options)
148
- [code, data, raw_headers]
149
- end # request
150
-
151
135
  EMPTY_ARGS = {}
152
136
 
153
137
  ### 生成下载授权URL
@@ -1,3 +1,4 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'qiniu/image'
4
+ require 'qiniu/pfop'
@@ -0,0 +1,137 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # vim: sw=2 ts=2
3
+
4
+ module Qiniu
5
+ module HTTP
6
+
7
+ class << self
8
+ public
9
+ def is_response_ok?(http_code)
10
+ return 200 <= http_code && http_code <= 299
11
+ end # is_response_ok?
12
+
13
+ def generate_query_string(params)
14
+ if params.is_a?(Hash)
15
+ total_param = params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) }
16
+ return total_param.join("&")
17
+ end
18
+
19
+ return params
20
+ end # generate_query_string
21
+
22
+ def get (url, opts = {})
23
+ ### 配置请求Header
24
+ req_headers = {
25
+ :connection => 'close',
26
+ :accept => '*/*',
27
+ :user_agent => Config.settings[:user_agent]
28
+ }
29
+
30
+ # 优先使用外部Header,覆盖任何特定Header
31
+ if opts[:headers].is_a?(Hash) then
32
+ req_headers.merge!(opts[:headers])
33
+ end
34
+
35
+ ### 发送请求
36
+ response = RestClient.get(url, req_headers)
37
+ return response.code.to_i, response.body, response.raw_headers
38
+ rescue => e
39
+ Log.logger.warn "#{e.message} => Qiniu::HTTP.get('#{url}')"
40
+ if e.respond_to?(:response) && e.response.respond_to?(:code) then
41
+ return e.response.code, e.response.body, e.response.raw_headers
42
+ end
43
+ return nil, nil, nil
44
+ end # get
45
+
46
+ API_RESULT_MIMETYPE = 'application/json'
47
+
48
+ def api_get (url, opts = {})
49
+ ### 配置请求Header
50
+ headers = {
51
+ :accept => API_RESULT_MIMETYPE
52
+ }
53
+
54
+ # 将特定Header混入外部Header中
55
+ if opts[:headers].is_a?(Hash) then
56
+ opts[:headers] = opts[:headers].dup.merge!(headers)
57
+ else
58
+ opts[:headers] = headers
59
+ end
60
+
61
+ ### 发送请求,然后转换返回值
62
+ resp_code, resp_body, resp_headers = get(url, opts)
63
+ if resp_code.nil? then
64
+ return 0, {}, {}
65
+ end
66
+
67
+ content_type = resp_headers["content-type"][0]
68
+ if !content_type.nil? && content_type == API_RESULT_MIMETYPE then
69
+ # 如果是JSON格式,则反序列化
70
+ resp_body = Utils.safe_json_parse(resp_body)
71
+ end
72
+
73
+ return resp_code, resp_body, resp_headers
74
+ end # api_get
75
+
76
+ def post (url, req_body = nil, opts = {})
77
+ ### 配置请求Header
78
+ req_headers = {
79
+ :connection => 'close',
80
+ :accept => '*/*',
81
+ :user_agent => Config.settings[:user_agent]
82
+ }
83
+
84
+ # 优先使用外部Header,覆盖任何特定Header
85
+ if opts[:headers].is_a?(Hash) then
86
+ req_headers.merge!(opts[:headers])
87
+ end
88
+
89
+ ### 发送请求
90
+ response = RestClient.post(url, req_body, req_headers)
91
+ return response.code.to_i, response.body, response.raw_headers
92
+ rescue => e
93
+ Log.logger.warn "#{e.message} => Qiniu::HTTP.post('#{url}')"
94
+ if e.respond_to?(:response) && e.response.respond_to?(:code) then
95
+ return e.response.code, e.response.body, e.response.raw_headers
96
+ end
97
+ return nil, nil, nil
98
+ end # post
99
+
100
+ def api_post (url, req_body = nil, opts = {})
101
+ ### 配置请求Header
102
+ headers = {
103
+ :accept => API_RESULT_MIMETYPE
104
+ }
105
+
106
+ # 将特定Header混入外部Header中
107
+ if opts[:headers].is_a?(Hash) then
108
+ opts[:headers] = opts[:headers].dup.merge!(headers)
109
+ else
110
+ opts[:headers] = headers
111
+ end
112
+
113
+ ### 发送请求,然后转换返回值
114
+ resp_code, resp_body, resp_headers = post(url, req_body, opts)
115
+ if resp_code.nil? then
116
+ return 0, {}, {}
117
+ end
118
+
119
+ content_type = resp_headers["content-type"][0]
120
+ if !content_type.nil? && content_type == API_RESULT_MIMETYPE then
121
+ # 如果是JSON格式,则反序列化
122
+ resp_body = Utils.safe_json_parse(resp_body)
123
+ end
124
+
125
+ return resp_code, resp_body, resp_headers
126
+ end # api_post
127
+
128
+ def management_post (url, body = '')
129
+ ### 授权并执行管理操作
130
+ return HTTP.api_post(url, body, {
131
+ :headers => { 'Authorization' => 'QBox ' + Auth.generate_acctoken(url, body) }
132
+ })
133
+ end # management_post
134
+ end # class << self
135
+
136
+ end # module HTTP
137
+ end # module Qiniu
@@ -7,11 +7,11 @@ module Qiniu
7
7
  include Utils
8
8
 
9
9
  def info(url)
10
- Utils.http_request url + '?imageInfo', nil, {:method => :get}
10
+ return HTTP.api_get(url + '?imageInfo')
11
11
  end # info
12
12
 
13
13
  def exif(url)
14
- Utils.http_request url + '?exif', nil, {:method => :get}
14
+ return HTTP.api_get(url + '?exif')
15
15
  end # exif
16
16
 
17
17
  def mogrify_preview_url(source_image_url, options)
@@ -1,31 +1,38 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ # vim: sw=2 ts=2
3
+
4
+ require 'qiniu/http'
2
5
 
3
6
  module Qiniu
4
7
  module Storage
5
8
  class << self
6
9
  include Utils
7
10
 
11
+ public
8
12
  def buckets
9
- Auth.request Config.settings[:rs_host] + '/buckets'
13
+ url = Config.settings[:rs_host] + '/buckets'
14
+ return HTTP.management_post(url)
10
15
  end # buckets
11
16
 
12
17
  PRIVATE_BUCKET = 0
13
18
  PUBLIC_BUCKET = 1
14
19
 
15
20
  def mkbucket(bucket_name, is_public = PUBLIC_BUCKET)
16
- Auth.request Config.settings[:rs_host] + '/mkbucket2/' + bucket_name + '/public/' + is_public.to_s
21
+ url = Config.settings[:rs_host] + '/mkbucket2/' + bucket_name + '/public/' + is_public.to_s
22
+ return HTTP.management_post(url)
17
23
  end # mkbucket
18
24
 
19
25
  def make_a_private_bucket(bucket_name)
20
- return mkbucket(bucket_name, PRIVATE_BUCKET)
26
+ return mkbucket(bucket_name, PRIVATE_BUCKET)
21
27
  end # make_a_private_bucket
22
28
 
23
29
  def make_a_public_bucket(bucket_name)
24
- return mkbucket(bucket_name, PUBLIC_BUCKET)
30
+ return mkbucket(bucket_name, PUBLIC_BUCKET)
25
31
  end # make_a_public_bucket
26
32
 
27
33
  def stat(bucket, key)
28
- Auth.request Config.settings[:rs_host] + '/stat/' + encode_entry_uri(bucket, key)
34
+ url = Config.settings[:rs_host] + '/stat/' + encode_entry_uri(bucket, key)
35
+ return HTTP.management_post(url)
29
36
  end # stat
30
37
 
31
38
  def get(bucket, key, save_as = nil, expires_in = nil, version = nil)
@@ -33,35 +40,29 @@ module Qiniu
33
40
  url += '/base/' + version unless version.nil?
34
41
  url += '/attName/' + Utils.urlsafe_base64_encode(save_as) unless save_as.nil?
35
42
  url += '/expires/' + expires_in.to_s if !expires_in.nil? && expires_in > 0
36
- Auth.request url
43
+ return HTTP.management_post(url)
37
44
  end # get
38
45
 
39
46
  def copy(source_bucket, source_key, target_bucket, target_key)
40
47
  uri = _generate_cp_or_mv_opstr('copy', source_bucket, source_key, target_bucket, target_key)
41
- Auth.request Config.settings[:rs_host] + uri
48
+ url = Config.settings[:rs_host] + uri
49
+ return HTTP.management_post(url)
42
50
  end # copy
43
51
 
44
52
  def move(source_bucket, source_key, target_bucket, target_key)
45
53
  uri = _generate_cp_or_mv_opstr('move', source_bucket, source_key, target_bucket, target_key)
46
- Auth.request Config.settings[:rs_host] + uri
54
+ url = Config.settings[:rs_host] + uri
55
+ return HTTP.management_post(url)
47
56
  end # move
48
57
 
49
58
  def delete(bucket, key)
50
- Auth.request Config.settings[:rs_host] + '/delete/' + encode_entry_uri(bucket, key)
59
+ url = Config.settings[:rs_host] + '/delete/' + encode_entry_uri(bucket, key)
60
+ return HTTP.management_post(url)
51
61
  end # delete
52
62
 
53
- def publish(domain, bucket)
54
- encoded_domain = Utils.urlsafe_base64_encode(domain)
55
- Auth.request Config.settings[:rs_host] + "/publish/#{encoded_domain}/from/#{bucket}"
56
- end # publish
57
-
58
- def unpublish(domain)
59
- encoded_domain = Utils.urlsafe_base64_encode(domain)
60
- Auth.request Config.settings[:rs_host] + "/unpublish/#{encoded_domain}"
61
- end # unpublish
62
-
63
63
  def drop(bucket)
64
- Auth.request Config.settings[:rs_host] + "/drop/#{bucket}"
64
+ url = Config.settings[:rs_host] + "/drop/#{bucket}"
65
+ return HTTP.management_post(url)
65
66
  end # drop
66
67
 
67
68
  def batch(command, bucket, keys)
@@ -70,7 +71,8 @@ module Qiniu
70
71
  encoded_uri = encode_entry_uri(bucket, key)
71
72
  execs << "op=/#{command}/#{encoded_uri}"
72
73
  end
73
- Auth.request Config.settings[:rs_host] + "/batch", execs.join("&"), {:mime => "application/x-www-form-urlencoded" }
74
+ url = Config.settings[:rs_host] + "/batch"
75
+ return HTTP.management_post(url, execs.join("&"))
74
76
  end # batch
75
77
 
76
78
  def batch_get(bucket, keys)
@@ -97,7 +99,7 @@ module Qiniu
97
99
  encoded_uri = encode_entry_uri(bucket, key)
98
100
  save_as_string = '/save-as/' + encoded_uri
99
101
  new_url = source_url + '?' + op_params_string + save_as_string
100
- Auth.request new_url
102
+ return HTTP.management_post(new_url)
101
103
  end # save_as
102
104
 
103
105
  def image_mogrify_save_as(bucket, key, source_image_url, options)
@@ -118,7 +120,8 @@ module Qiniu
118
120
  op_args.each do |e|
119
121
  execs << 'op=' + _generate_cp_or_mv_opstr(command, e[0], e[1], e[2], e[3]) if e.size == 4
120
122
  end
121
- Auth.request Config.settings[:rs_host] + "/batch", execs.join("&"), {:mime => "application/x-www-form-urlencoded" }
123
+ url = Config.settings[:rs_host] + "/batch"
124
+ return HTTP.management_post(url, execs.join("&"))
122
125
  end # _batch_cp_or_mv
123
126
  end
124
127
  end # module Storage
@@ -3,33 +3,31 @@
3
3
  module Qiniu
4
4
  module Misc
5
5
  class << self
6
- include Utils
7
-
8
6
  def set_protected(bucket, protected_mode)
9
- host = Config.settings[:pub_host]
10
- Auth.request %Q(#{host}/accessMode/#{bucket}/mode/#{protected_mode})
11
- end
7
+ url = Config.settings[:pub_host] + %Q(/accessMode/#{bucket}/mode/#{protected_mode})
8
+ return HTTP.management_post(url)
9
+ end # set_protected
12
10
 
13
11
  def set_separator(bucket, separator)
14
- host = Config.settings[:pub_host]
15
12
  encoded_separator = Utils.urlsafe_base64_encode(separator)
16
- Auth.request %Q(#{host}/separator/#{bucket}/sep/#{encoded_separator})
17
- end
13
+ url = Config.settings[:pub_host] + %Q(/separator/#{bucket}/sep/#{encoded_separator})
14
+ return HTTP.management_post(url)
15
+ end # set_separator
18
16
 
19
17
  def set_style(bucket, name, style)
20
- host = Config.settings[:pub_host]
21
18
  encoded_name = Utils.urlsafe_base64_encode(name)
22
19
  encoded_style = Utils.urlsafe_base64_encode(style)
23
- Auth.request %Q(#{host}/style/#{bucket}/name/#{encoded_name}/style/#{encoded_style})
24
- end
20
+ url = Config.settings[:pub_host] + %Q(/style/#{bucket}/name/#{encoded_name}/style/#{encoded_style})
21
+ return HTTP.management_post(url)
22
+ end # set_style
25
23
 
26
24
  def unset_style(bucket, name)
27
- host = Config.settings[:pub_host]
28
25
  encoded_name = Utils.urlsafe_base64_encode(name)
29
- Auth.request %Q(#{host}/unstyle/#{bucket}/name/#{encoded_name})
30
- end
26
+ url = Config.settings[:pub_host] + %Q(/unstyle/#{bucket}/name/#{encoded_name})
27
+ return HTTP.management_post(url)
28
+ end # unset_style
29
+ end # class << self
31
30
 
32
- end
33
31
  end # module Misc
34
32
  end # module Qiniu
35
33
 
@@ -0,0 +1,123 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # vim: sw=2 ts=2
3
+
4
+ require 'qiniu/adt'
5
+ require 'qiniu/http'
6
+
7
+ module Qiniu
8
+ module Fop
9
+ module Persistance
10
+
11
+ class PfopPolicy
12
+ include ADT::Policy
13
+
14
+ private
15
+ def initialize(bucket,
16
+ key,
17
+ fops,
18
+ notify_url)
19
+ @bucket = bucket
20
+ @key = key
21
+ @notify_url = notify_url
22
+
23
+ self.fops!(fops)
24
+ end # initialize
25
+
26
+ public
27
+ PARAMS = {
28
+ # 字符串类型参数
29
+ :bucket => "bucket",
30
+ :key => "key",
31
+ :fops => "fops",
32
+ :notify_url => "notifyURL",
33
+
34
+ # 数值类型参数
35
+ :force => "force"
36
+ } # PARAMS
37
+
38
+ PARAMS.each_pair do |key, fld|
39
+ attr_accessor key
40
+ end
41
+
42
+ def params
43
+ return PARAMS
44
+ end # params
45
+
46
+ def fops! (fops)
47
+ if fops.is_a?(Hash) then
48
+ fops = fops.values
49
+ end
50
+
51
+ if fops.is_a?(Array) then
52
+ new_fops = []
53
+ fops.each do |v|
54
+ if v.is_a?(ApiSpecification) then
55
+ new_fops.push(v.to_s)
56
+ end
57
+ end
58
+
59
+ @fops = new_fops.join(";")
60
+ else
61
+ @fops = fops.to_s
62
+ end
63
+ end # fops!
64
+
65
+ def force!
66
+ @force = 1
67
+ end # force!
68
+
69
+ alias :to_s :to_json
70
+ end # class PfopPolicy
71
+
72
+ class << self
73
+
74
+ API_HOST = 'http://api.qiniu.com'
75
+
76
+ PFOP_URL = API_HOST + '/pfop/'
77
+
78
+ def pfop (args)
79
+ ### 生成fop指令串
80
+ if args.is_a?(PfopPolicy) then
81
+ # PfopPolicy的各个字段按固定顺序组织
82
+ body = args.to_query_string()
83
+ elsif args.is_a?(Hash) then
84
+ # 无法保证固定字段顺序
85
+ body = HTTP.generate_query_string(args)
86
+ else
87
+ # 由调用者保证固定字段顺序
88
+ body = args.to_s
89
+ end
90
+
91
+ ### 发送请求
92
+ return HTTP.management_post(PFOP_URL, body)
93
+ end # pfop
94
+
95
+ PREFOP_URL = API_HOST + '/status/get/prefop?id='
96
+
97
+ def prefop (persistent_id)
98
+ ### 抽取persistentId
99
+ if persistent_id.is_a?(Hash) then
100
+ pid = persistent_id['persistentId']
101
+ else
102
+ pid = persistent_id.to_s
103
+ end
104
+
105
+ ### 发送请求
106
+ url = PREFOP_URL + pid
107
+ return HTTP.api_get(url)
108
+ end # prefop
109
+
110
+ def generate_p1_url (url, fop)
111
+ # 如果fop是ApiSpecification,则各字段按固定顺序组织,保证一致性
112
+ # 否则由调用者保证固定字段顺序
113
+ fop = CGI.escape(fop.to_s).gsub('+', '%20')
114
+
115
+ ### 生成url
116
+ return url + '?p/1/' + fop
117
+ end # generate_pl_url
118
+
119
+ end # class << self
120
+
121
+ end # module Persistance
122
+ end # module Fop
123
+ end # module Qiniu
@@ -97,19 +97,23 @@ module Qiniu
97
97
 
98
98
  def _call_binary_with_token(uptoken, url, data, content_type = nil, retry_times = 0)
99
99
  options = {
100
- :method => :post,
101
- :content_type => 'application/octet-stream',
102
- :upload_signature_token => uptoken
100
+ :headers => {
101
+ :content_type => 'application/octet-stream',
102
+ 'Authorization' => 'UpToken ' + uptoken
103
+ }
103
104
  }
104
- options[:content_type] = content_type if !content_type.nil? && !content_type.empty?
105
- code, data = http_request url, data, options
106
- unless Utils.is_response_ok?(code)
105
+ if !content_type.nil? && !content_type.empty? then
106
+ options[:headers][:content_type] = content_type
107
+ end
108
+
109
+ code, data, raw_headers = HTTP.api_post(url, data, options)
110
+ unless HTTP.is_response_ok?(code)
107
111
  retry_times += 1
108
112
  if Config.settings[:auto_reconnect] && retry_times < Config.settings[:max_retry_times]
109
113
  return _call_binary_with_token(uptoken, url, data, options[:content_type], retry_times)
110
114
  end
111
115
  end
112
- [code, data]
116
+ return code, data, raw_headers
113
117
  end # _call_binary_with_token
114
118
 
115
119
  def _mkblock(uptoken, block_size, body)
@@ -146,9 +150,12 @@ module Qiniu
146
150
  if result_length != body_length
147
151
  raise FileSeekReadError.new(fpath, block_index, seek_pos, body_length, result_length)
148
152
  end
149
- code, data = _mkblock(uptoken, block_size, body)
153
+
154
+ code, data, raw_headers = _mkblock(uptoken, block_size, body)
155
+ Utils.debug "Mkblk : #{code.inspect} #{data.inspect} #{raw_headers.inspect}"
156
+
150
157
  body_crc32 = Zlib.crc32(body)
151
- if Utils.is_response_ok?(code) && data["crc32"] == body_crc32
158
+ if HTTP.is_response_ok?(code) && data["crc32"] == body_crc32
152
159
  progress[:ctx] = data["ctx"]
153
160
  progress[:offset] = body_length
154
161
  progress[:restsize] = block_size - body_length
@@ -160,6 +167,7 @@ module Qiniu
160
167
  break
161
168
  elsif i == retry_times && data["crc32"] != body_crc32
162
169
  Log.logger.error %Q(Uploading block error. Expected crc32: #{body_crc32}, but got: #{data["crc32"]})
170
+ return code, data, raw_headers
163
171
  end
164
172
  end
165
173
  elsif progress[:offset] + progress[:restsize] != block_size
@@ -177,9 +185,12 @@ module Qiniu
177
185
  if result_length != body_length
178
186
  raise FileSeekReadError.new(fpath, block_index, seek_pos, body_length, result_length)
179
187
  end
180
- code, data = _putblock(progress[:host], uptoken, progress[:ctx], progress[:offset], body)
188
+
189
+ code, data, raw_headers = _putblock(progress[:host], uptoken, progress[:ctx], progress[:offset], body)
190
+ Utils.debug "Bput : #{code.inspect} #{data.inspect} #{raw_headers.inspect}"
191
+
181
192
  body_crc32 = Zlib.crc32(body)
182
- if Utils.is_response_ok?(code) && data["crc32"] == body_crc32
193
+ if HTTP.is_response_ok?(code) && data["crc32"] == body_crc32
183
194
  progress[:ctx] = data["ctx"]
184
195
  progress[:offset] += body_length
185
196
  progress[:restsize] -= body_length
@@ -191,11 +202,12 @@ module Qiniu
191
202
  break
192
203
  elsif i == retry_times && data["crc32"] != body_crc32
193
204
  Log.logger.error %Q(Uploading block error. Expected crc32: #{body_crc32}, but got: #{data["crc32"]})
205
+ return code, data, raw_headers
194
206
  end
195
207
  end
196
208
  end
197
209
  # return
198
- return [code, data]
210
+ return code, data, raw_headers
199
211
  end # _resumable_put_block
200
212
 
201
213
  def _block_count(fsize)
@@ -228,7 +240,7 @@ module Qiniu
228
240
  #code, data = _resumable_put_block(uptoken, fh, block_index, block_size, Config.settings[:chunk_size], progresses[block_index], Config.settings[:max_retry_times], chunk_notifier)
229
241
  # Put the whole block as a chunk
230
242
  code, data = _resumable_put_block(uptoken, fh, block_index, block_size, block_size, progresses[block_index], Config.settings[:max_retry_times], chunk_notifier)
231
- if Utils.is_response_ok?(code)
243
+ if HTTP.is_response_ok?(code)
232
244
  #checksums[block_index] = data["checksum"]
233
245
  checksums[block_index] = data["ctx"]
234
246
  if !block_notifier.nil? && block_notifier.respond_to?("notify")
@@ -254,7 +266,7 @@ module Qiniu
254
266
  path += '/mimeType/' + Utils.urlsafe_base64_encode(mime_type) if !mime_type.nil? && !mime_type.empty?
255
267
  path += '/meta/' + Utils.urlsafe_base64_encode(custom_meta) if !custom_meta.nil? && !custom_meta.empty?
256
268
  path += '/customer/' + customer if !customer.nil? && !customer.empty?
257
- callback_query_string = Utils.generate_query_string(callback_params) if !callback_params.nil? && !callback_params.empty?
269
+ callback_query_string = HTTP.generate_query_string(callback_params) if !callback_params.nil? && !callback_params.empty?
258
270
  path += '/params/' + Utils.urlsafe_base64_encode(callback_query_string) if !callback_query_string.nil? && !callback_query_string.empty?
259
271
  path += '/rotate/' + rotate if !rotate.nil? && rotate.to_i >= 0
260
272
  url = uphost + path
@@ -287,19 +299,20 @@ module Qiniu
287
299
  checksums = []
288
300
  block_count.times{checksums << ''}
289
301
 
290
- code, data = _resumable_put(uptoken, fh, checksums, progresses, block_notifier, chunk_notifier)
302
+ code, data, raw_headers = _resumable_put(uptoken, fh, checksums, progresses, block_notifier, chunk_notifier)
291
303
 
292
- if Utils.is_response_ok?(code)
304
+ if HTTP.is_response_ok?(code)
293
305
  uphost = data["host"]
294
306
  entry_uri = bucket + ':' + key
295
- code, data = _mkfile(uphost, uptoken, entry_uri, fsize, checksums, mime_type, custom_meta, customer, callback_params, rotate)
307
+ code, data, raw_headers = _mkfile(uphost, uptoken, entry_uri, fsize, checksums, mime_type, custom_meta, customer, callback_params, rotate)
308
+ Utils.debug "Mkfile : #{code.inspect} #{data.inspect} #{raw_headers.inspect}"
296
309
  end
297
310
 
298
- if Utils.is_response_ok?(code)
311
+ if HTTP.is_response_ok?(code)
299
312
  Utils.debug "File #{fh.path} {size: #{fsize}} successfully uploaded."
300
313
  end
301
314
 
302
- [code, data]
315
+ return code, data, raw_headers
303
316
  end # _resumable_upload
304
317
  end # self class
305
318
  end # module Storage
@@ -23,7 +23,7 @@ module Qiniu
23
23
  signature += '?' + query_string if !query_string.nil? && !query_string.empty?
24
24
  signature += "\n"
25
25
  if @params.is_a?(Hash)
26
- params_string = Utils.generate_query_string(@params)
26
+ params_string = HTTP.generate_query_string(@params)
27
27
  signature += params_string
28
28
  end
29
29
  signature
@@ -28,10 +28,20 @@ module Qiniu
28
28
  if callback_params.nil?
29
29
  callback_params = {:bucket => bucket, :key => key, :mime_type => mime_type}
30
30
  end
31
- callback_query_string = Utils.generate_query_string(callback_params)
31
+ callback_query_string = HTTP.generate_query_string(callback_params)
32
+
32
33
  url = Config.settings[:up_host] + '/upload'
34
+ post_data = {
35
+ :params => callback_query_string,
36
+ :action => action_params,
37
+ :file => File.new(local_file, 'rb'),
38
+ :multipart => true
39
+ }
40
+ if !uptoken.nil? then
41
+ post_data[:auth] = uptoken unless uptoken.nil?
42
+ end
33
43
 
34
- Utils.upload_multipart_data(url, local_file, action_params, callback_query_string, uptoken)
44
+ return HTTP.api_post(url, post_data)
35
45
  end # upload_with_token
36
46
 
37
47
  def upload_with_token_2(uptoken,
@@ -59,7 +69,7 @@ module Qiniu
59
69
  end
60
70
 
61
71
  ### 发送请求
62
- Utils.http_request url, post_data
72
+ HTTP.api_post(url, post_data)
63
73
  end # upload_with_token_2
64
74
 
65
75
  ### 授权举例
@@ -31,20 +31,13 @@ module Qiniu
31
31
  {}
32
32
  end
33
33
 
34
- def is_response_ok?(status_code)
35
- status_code/100 == 2
36
- end
37
-
38
- def response_error(status_code, errmsg)
39
- [status_code, {"error" => errmsg}]
40
- end
41
-
42
34
  def debug(msg)
43
35
  if Config.settings[:enable_debug]
44
36
  Log.logger.debug(msg)
45
37
  end
46
38
  end
47
39
 
40
+ ### 已过时,仅作为兼容接口保留
48
41
  def send_request_with url, data = nil, options = {}
49
42
  options[:method] = Config.settings[:method] unless options[:method]
50
43
  options[:content_type] = Config.settings[:content_type] unless options[:content_type]
@@ -69,7 +62,7 @@ module Qiniu
69
62
  response = RestClient.post(url, data, header_options)
70
63
  end
71
64
  code = response.respond_to?(:code) ? response.code.to_i : 0
72
- unless is_response_ok?(code)
65
+ unless HTTP.is_response_ok?(code)
73
66
  raise RequestFailed.new("Request Failed", response)
74
67
  else
75
68
  data = {}
@@ -80,6 +73,7 @@ module Qiniu
80
73
  [code, data, raw_headers]
81
74
  end # send_request_with
82
75
 
76
+ ### 已过时,仅作为兼容接口保留
83
77
  def http_request url, data = nil, options = {}
84
78
  retry_times = 0
85
79
  begin
@@ -107,23 +101,6 @@ module Qiniu
107
101
  end
108
102
  end
109
103
 
110
- def upload_multipart_data(url, filepath, action_string, callback_query_string = '', uptoken = nil)
111
- post_data = {
112
- :params => callback_query_string,
113
- :action => action_string,
114
- :file => File.new(filepath, 'rb'),
115
- :multipart => true
116
- }
117
- post_data[:auth] = uptoken unless uptoken.nil?
118
- http_request url, post_data
119
- end
120
-
121
- def generate_query_string(params)
122
- return params if params.is_a?(String)
123
- total_param = params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) }
124
- total_param.join("&")
125
- end
126
-
127
104
  def crc32checksum(filepath)
128
105
  File.open(filepath, "rb") { |f| Zlib.crc32 f.read }
129
106
  end
@@ -4,7 +4,7 @@ module Qiniu
4
4
  module Version
5
5
  MAJOR = 6
6
6
  MINOR = 2
7
- PATCH = 0
7
+ PATCH = 1
8
8
  # Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
9
9
  #
10
10
  # Example
@@ -3,8 +3,8 @@
3
3
  require File.expand_path('../lib/qiniu/version', __FILE__)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
- gem.authors = ["why404"]
7
- gem.email = ["why404@gmail.com"]
6
+ gem.authors = ["why404","BluntBlade"]
7
+ gem.email = ["sdk@qiniu.com"]
8
8
  gem.description = %q{Qiniu Resource (Cloud) Storage SDK for Ruby. See: http://developer.qiniu.com/docs/v6/sdk/ruby-sdk.html}
9
9
  gem.summary = %q{Qiniu Resource (Cloud) Storage SDK for Ruby}
10
10
  gem.homepage = "https://github.com/qiniu/ruby-sdk"
@@ -15,6 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.name = "qiniu"
16
16
  gem.require_paths = ["lib"]
17
17
  gem.version = Qiniu::Version.to_s
18
+ gem.license = "MIT"
18
19
 
19
20
  # specify any dependencies here; for example:
20
21
  gem.add_development_dependency "rake", ">= 0.9"
@@ -0,0 +1,89 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # vim: sw=2 ts=2
3
+
4
+ require 'spec_helper'
5
+ require 'qiniu/auth'
6
+ require 'qiniu'
7
+ require 'qiniu/fop'
8
+
9
+ module Qiniu
10
+ module Fop
11
+ module Persistance
12
+ describe Persistance do
13
+
14
+ before :all do
15
+ ### 复用RubySDK-Test-Storage空间
16
+ @bucket = 'RubySDK-Test-Storage'
17
+ @bucket = make_unique_bucket(@bucket)
18
+
19
+ ### 尝试创建空间
20
+ code, data, headers = Qiniu::Storage.make_a_public_bucket(@bucket)
21
+ puts code.inspect
22
+ puts data.inspect
23
+ puts headers.inspect
24
+
25
+ pic_fname = "image_logo_for_test.png"
26
+ @key = make_unique_key_in_bucket(pic_fname)
27
+
28
+ local_file = File.expand_path('../' + pic_fname, __FILE__)
29
+
30
+ ### 检查测试文件存在性
31
+ code, body, headers = Qiniu::Storage.stat(@bucket, @key)
32
+ if code == 404 || code == 612 then
33
+ # 文件不存在,尝试上传
34
+ pp = Qiniu::Auth.PutPolicy.new(@bucket, @key)
35
+ code, body, headers = Qiniu::Storage.upload_with_put_policy(
36
+ pp,
37
+ local_file
38
+ )
39
+ puts "Put a test file for Persistance cases"
40
+ puts code.inspect
41
+ puts body.inspect
42
+ puts header.inspect
43
+ end
44
+ end
45
+
46
+ after :all do
47
+ ### 不删除Bucket以备下次使用
48
+ end
49
+
50
+ context ".pfop" do
51
+ it "should works" do
52
+ pp = Persistance::PfopPolicy.new(
53
+ @bucket,
54
+ @key,
55
+ 'imageView2/1/w/80/h/80', # fops
56
+ 'www.baidu.com' # notify_url
57
+ )
58
+
59
+ code, data, headers = Qiniu::Fop::Persistance.pfop(pp)
60
+ code.should == 200
61
+ puts data.inspect
62
+ end
63
+ end
64
+
65
+ context ".prefop" do
66
+ it "should works" do
67
+ code, data, headers = Qiniu::Fop::Persistance.prefop('fakePersistentId')
68
+ code.should == 612
69
+ puts code.inspect
70
+ puts data.inspect
71
+ puts headers.inspect
72
+ end
73
+ end
74
+
75
+ context ".p1" do
76
+ it "should works" do
77
+ url = 'http://fake.qiniudn.com/fake.jpg'
78
+ fop = 'imageView2/1/w/80/h/80'
79
+ target_url = "#{url}?p/1/#{CGI.escape(fop).gsub('+', '%20')}"
80
+
81
+ p1_url = Qiniu::Fop::Persistance.generate_p1_url(url, fop)
82
+ p1_url.should == target_url
83
+ puts p1_url.inspect
84
+ end
85
+ end
86
+ end
87
+ end # module Persistance
88
+ end # module Fop
89
+ end # module Qiniu
metadata CHANGED
@@ -1,123 +1,124 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qiniu
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.2.0
4
+ version: 6.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - why404
8
+ - BluntBlade
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-04-02 00:00:00.000000000 Z
12
+ date: 2014-04-21 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake
15
16
  requirement: !ruby/object:Gem::Requirement
16
17
  requirements:
17
- - - '>='
18
+ - - ">="
18
19
  - !ruby/object:Gem::Version
19
20
  version: '0.9'
20
21
  type: :development
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
- - - '>='
25
+ - - ">="
25
26
  - !ruby/object:Gem::Version
26
27
  version: '0.9'
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: rspec
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
- - - '>='
32
+ - - ">="
32
33
  - !ruby/object:Gem::Version
33
34
  version: '2.11'
34
35
  type: :development
35
36
  prerelease: false
36
37
  version_requirements: !ruby/object:Gem::Requirement
37
38
  requirements:
38
- - - '>='
39
+ - - ">="
39
40
  - !ruby/object:Gem::Version
40
41
  version: '2.11'
41
42
  - !ruby/object:Gem::Dependency
42
43
  name: fakeweb
43
44
  requirement: !ruby/object:Gem::Requirement
44
45
  requirements:
45
- - - ~>
46
+ - - "~>"
46
47
  - !ruby/object:Gem::Version
47
48
  version: '1.3'
48
49
  type: :development
49
50
  prerelease: false
50
51
  version_requirements: !ruby/object:Gem::Requirement
51
52
  requirements:
52
- - - ~>
53
+ - - "~>"
53
54
  - !ruby/object:Gem::Version
54
55
  version: '1.3'
55
56
  - !ruby/object:Gem::Dependency
56
57
  name: json
57
58
  requirement: !ruby/object:Gem::Requirement
58
59
  requirements:
59
- - - ~>
60
+ - - "~>"
60
61
  - !ruby/object:Gem::Version
61
62
  version: '1.7'
62
63
  type: :runtime
63
64
  prerelease: false
64
65
  version_requirements: !ruby/object:Gem::Requirement
65
66
  requirements:
66
- - - ~>
67
+ - - "~>"
67
68
  - !ruby/object:Gem::Version
68
69
  version: '1.7'
69
70
  - !ruby/object:Gem::Dependency
70
71
  name: rest-client
71
72
  requirement: !ruby/object:Gem::Requirement
72
73
  requirements:
73
- - - ~>
74
+ - - "~>"
74
75
  - !ruby/object:Gem::Version
75
76
  version: '1.6'
76
77
  type: :runtime
77
78
  prerelease: false
78
79
  version_requirements: !ruby/object:Gem::Requirement
79
80
  requirements:
80
- - - ~>
81
+ - - "~>"
81
82
  - !ruby/object:Gem::Version
82
83
  version: '1.6'
83
84
  - !ruby/object:Gem::Dependency
84
85
  name: mime-types
85
86
  requirement: !ruby/object:Gem::Requirement
86
87
  requirements:
87
- - - ~>
88
+ - - "~>"
88
89
  - !ruby/object:Gem::Version
89
90
  version: '1.19'
90
91
  type: :runtime
91
92
  prerelease: false
92
93
  version_requirements: !ruby/object:Gem::Requirement
93
94
  requirements:
94
- - - ~>
95
+ - - "~>"
95
96
  - !ruby/object:Gem::Version
96
97
  version: '1.19'
97
98
  - !ruby/object:Gem::Dependency
98
99
  name: ruby-hmac
99
100
  requirement: !ruby/object:Gem::Requirement
100
101
  requirements:
101
- - - ~>
102
+ - - "~>"
102
103
  - !ruby/object:Gem::Version
103
104
  version: '0.4'
104
105
  type: :runtime
105
106
  prerelease: false
106
107
  version_requirements: !ruby/object:Gem::Requirement
107
108
  requirements:
108
- - - ~>
109
+ - - "~>"
109
110
  - !ruby/object:Gem::Version
110
111
  version: '0.4'
111
112
  description: 'Qiniu Resource (Cloud) Storage SDK for Ruby. See: http://developer.qiniu.com/docs/v6/sdk/ruby-sdk.html'
112
113
  email:
113
- - why404@gmail.com
114
+ - sdk@qiniu.com
114
115
  executables: []
115
116
  extensions: []
116
117
  extra_rdoc_files: []
117
118
  files:
118
- - .gitignore
119
- - .rspec
120
- - .travis.yml
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
121
122
  - CHANGELOG.md
122
123
  - Gemfile
123
124
  - Gemfile.lock
@@ -128,14 +129,17 @@ files:
128
129
  - lib/qiniu-rs.rb
129
130
  - lib/qiniu.rb
130
131
  - lib/qiniu/abstract.rb
132
+ - lib/qiniu/adt.rb
131
133
  - lib/qiniu/auth.rb
132
134
  - lib/qiniu/config.rb
133
135
  - lib/qiniu/exceptions.rb
134
136
  - lib/qiniu/fop.rb
137
+ - lib/qiniu/http.rb
135
138
  - lib/qiniu/image.rb
136
139
  - lib/qiniu/log.rb
137
140
  - lib/qiniu/management.rb
138
141
  - lib/qiniu/misc.rb
142
+ - lib/qiniu/pfop.rb
139
143
  - lib/qiniu/resumable_upload.rb
140
144
  - lib/qiniu/storage.rb
141
145
  - lib/qiniu/tokens/access_token.rb
@@ -152,6 +156,7 @@ files:
152
156
  - spec/qiniu/image_spec.rb
153
157
  - spec/qiniu/management_spec.rb
154
158
  - spec/qiniu/misc_spec.rb
159
+ - spec/qiniu/pfop_spec.rb
155
160
  - spec/qiniu/qiniu_spec.rb
156
161
  - spec/qiniu/tokens/qbox_token_spec.rb
157
162
  - spec/qiniu/upload_spec.rb
@@ -159,7 +164,8 @@ files:
159
164
  - spec/qiniu/version_spec.rb
160
165
  - spec/spec_helper.rb
161
166
  homepage: https://github.com/qiniu/ruby-sdk
162
- licenses: []
167
+ licenses:
168
+ - MIT
163
169
  metadata: {}
164
170
  post_install_message:
165
171
  rdoc_options: []
@@ -167,17 +173,17 @@ require_paths:
167
173
  - lib
168
174
  required_ruby_version: !ruby/object:Gem::Requirement
169
175
  requirements:
170
- - - '>='
176
+ - - ">="
171
177
  - !ruby/object:Gem::Version
172
178
  version: '0'
173
179
  required_rubygems_version: !ruby/object:Gem::Requirement
174
180
  requirements:
175
- - - '>='
181
+ - - ">="
176
182
  - !ruby/object:Gem::Version
177
183
  version: '0'
178
184
  requirements: []
179
185
  rubyforge_project:
180
- rubygems_version: 2.0.3
186
+ rubygems_version: 2.2.2
181
187
  signing_key:
182
188
  specification_version: 4
183
189
  summary: Qiniu Resource (Cloud) Storage SDK for Ruby
@@ -188,6 +194,7 @@ test_files:
188
194
  - spec/qiniu/image_spec.rb
189
195
  - spec/qiniu/management_spec.rb
190
196
  - spec/qiniu/misc_spec.rb
197
+ - spec/qiniu/pfop_spec.rb
191
198
  - spec/qiniu/qiniu_spec.rb
192
199
  - spec/qiniu/tokens/qbox_token_spec.rb
193
200
  - spec/qiniu/upload_spec.rb