pindo 4.7.7 → 4.7.9

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
  SHA256:
3
- metadata.gz: 76171ef66d029d57c3591ee9a61116a4c6f3949fa6ea6e2127aba8df8518507f
4
- data.tar.gz: 122afc9e684b1d41f62d52813e58df79d5db509c41b0767eb544a3782309f30b
3
+ metadata.gz: 6aaeca7f069b61265bf65ea62d49bf8b3d3e64ccf2755475a68c37afa54b12fb
4
+ data.tar.gz: 980495658d7accae82e4bf4f11913892edfb0efeaae49438e9003f9a67aed5f2
5
5
  SHA512:
6
- metadata.gz: ea060cef3c2070db870091c96fa34607bd68068e77e1e21ea289344354770353c229c2bce0f07cb4d6300b585228695e7db24a3fd78cc83bfd5a3f416a7b66ed
7
- data.tar.gz: 6170b21c179abf3d5192d402437f4105c2466324ac1a003d58d7409cb91ed4629a9759e74c68d645c745a912136dfadd6eae48d7cd7c9247ed183670fa38648d
6
+ metadata.gz: f89ffccfccdd56678fe98cac69f9eb621276a264aa93bd7bdd55d78995220cfc25b199c6f1da3b4ebf7a03e7b3a86b75568e8a252da3089c2211f088c0195e33
7
+ data.tar.gz: 566ff17b4b945007869f8acdce6d5e7bb10ec688973f82ad92aeb7e10dd12e9ddf4fb570424b3892813af57601d821ac867c586a3dbc9b967b7e0d9e5763ba50
@@ -20,6 +20,10 @@ module Pindo
20
20
  attr_accessor :default_url
21
21
  attr_accessor :attach_url
22
22
 
23
+ attr_accessor :upload_binary_file
24
+ attr_accessor :file_size
25
+ attr_accessor :progress_bar
26
+
23
27
 
24
28
  def initialize()
25
29
 
@@ -68,15 +72,17 @@ module Pindo
68
72
 
69
73
  def upload_file(binary_file:nil, isAttach:false)
70
74
 
71
- extension = File.extname(binary_file)
72
- filename = File.basename(binary_file)
73
- filesize = File.size(binary_file)
75
+ @upload_binary_file = binary_file
76
+ @file_size = File.size(@upload_binary_file)
77
+ @progress_bar = PgyerUploadProgressBar.new(upload_total_size:@file_size)
78
+
79
+ extension = File.extname(@upload_binary_file)
80
+ filename = File.basename(@upload_binary_file)
74
81
  size_level = 1024 * 1024
75
- file_bytes = File.binread(binary_file)
82
+ file_bytes = File.binread(@upload_binary_file)
76
83
  checksum = Digest::MD5.hexdigest(file_bytes)
77
84
  file_uuid = SecureRandom.uuid
78
- total_m = sprintf("%.2f", 1.00 * filesize / 1024 /1024 )
79
-
85
+ total_m = sprintf("%.2f", 1.00 * @file_size / 1024 /1024 )
80
86
 
81
87
 
82
88
  upload_path = @default_url
@@ -86,92 +92,160 @@ module Pindo
86
92
  content_disposition = "attachment; filename=#{filename}"
87
93
  end
88
94
 
89
- object_key = upload_path + file_uuid + extension
95
+ upload_path_key = upload_path + file_uuid + extension
90
96
 
91
- puts "文件路径: #{binary_file}"
97
+ task_num = 10
98
+
99
+ puts "文件路径: #{@upload_binary_file}"
92
100
  puts "文件大小: #{total_m}M"
93
- puts "上传路径: #{object_key}"
101
+ puts "上传路径: #{upload_path_key}"
102
+ puts "切片大小: 5 MB"
103
+ puts "最大线程数: #{task_num}"
94
104
  puts
95
105
 
96
-
97
- @upload_total_size = filesize
98
-
99
-
100
106
  upload_result = nil
101
107
 
102
- file_size_param = 1.00 * filesize / 1024 /1024
103
- result_data = post_upload_url_req(upload_key:object_key, file_size:file_size_param.ceil)
108
+ file_size_param = 1.00 * @file_size / 1024 /1024
109
+ result_data = post_upload_url_req(upload_path_key:upload_path_key, file_ceil_size:file_size_param.ceil)
104
110
 
105
111
  upload_id= result_data["data"]["uploadId"]
106
- upload_params_list = result_data["data"]["uploadParamsList"]
112
+ @upload_params_list = result_data["data"]["uploadParamsList"]
107
113
 
108
-
109
114
 
110
115
  Funlog.instance.fancyinfo_start("开始上传...")
116
+ @upload_eTags = []
117
+
118
+ # single_task_upload_data_req( )
111
119
 
112
- eTags = []
113
- eTags = single_task_upload_data_req(binary_file:binary_file, upload_params_list:upload_params_list)
114
120
 
115
- result_data = post_upload_finish_req(upload_key:object_key, upload_id:upload_id, eTags:eTags)
121
+ multi_task_upload_data_req(task_num:task_num)
116
122
 
123
+ result_data = post_upload_finish_req(upload_path_key:upload_path_key, upload_id:upload_id, eTags:@upload_eTags)
124
+ puts result_data
117
125
  if result_data["code"] == 200
118
- upload_result = object_key
119
- # set_progress(filesize, filesize)
120
- Funlog.instance.fancyinfo_success("文件#{binary_file} 上传成功! 😎😎😎")
126
+ upload_result = upload_path_key
127
+ Funlog.instance.fancyinfo_success("文件#{@upload_binary_file} 上传成功! 😎😎😎")
121
128
  else
122
129
  upload_result = nil
123
- Funlog.instance.fancyinfo_error("文件#{binary_file} 上传失败! 😭😭😭")
130
+ Funlog.instance.fancyinfo_error("文件#{@upload_binary_file} 上传失败! 😭😭😭")
124
131
  end
125
132
 
126
133
  return upload_result
127
134
 
128
135
  end
129
136
 
130
- def single_task_upload_data_req(binary_file:nil, upload_params_list:nil)
131
- filesize = File.size(binary_file)
132
- eTags = []
133
- if !upload_params_list.nil? && upload_params_list.length > 0
134
- for i in 0..upload_params_list.length-1 do
135
- part_result = single_task_upload_part_data_req(filesize:filesize, binary_file:binary_file, upload_url:upload_params_list[i]["signedUrl"], part_no:upload_params_list[i]["partNo"])
136
- eTag_item = { partNumber: upload_params_list[i]["partNo"], tag: part_result}
137
- eTags << eTag_item
137
+
138
+ def multi_task_upload_data_req(task_num:1)
139
+
140
+
141
+ for i in 0..@upload_params_list.length-1 do
142
+ @upload_params_list[i]["retryCount"] = 3
143
+ end
144
+
145
+ while @upload_params_list.size > 0
146
+ upload_params_list_temp = []
147
+ #每次最大5个线程上传
148
+ for i in 1..task_num do
149
+ upload_params_list_temp << @upload_params_list.shift
150
+ end
151
+ hydra = Typhoeus::Hydra.new
152
+ while upload_params_list_temp.size > 0
153
+ upload_params_item = upload_params_list_temp.shift
154
+ unless upload_params_item.nil?
155
+ single_task_upload_part_data_req(upload_params_item:upload_params_item, hydra_handle:hydra)
156
+ end
138
157
  end
158
+ hydra.run
159
+ end
160
+
161
+ end
162
+
163
+ def single_task_upload_data_req()
164
+
165
+ for i in 0..@upload_params_list.length-1 do
166
+ @upload_params_list[i]["retryCount"] = 3
139
167
  end
140
168
 
141
- return eTags
169
+ while @upload_params_list.size > 0
170
+ upload_params_item = @upload_params_list.shift
171
+ single_task_upload_part_data_req(upload_params_item:upload_params_item)
172
+ end
173
+
174
+
175
+ return @eTags
142
176
  end
143
177
 
144
- def single_task_upload_part_data_req(filesize:nil, binary_file:nil, upload_url:nil, part_no:nil)
178
+ def single_task_upload_part_data_req(upload_params_item:nil, hydra_handle:nil)
145
179
 
146
- # puts "start upload part #{part_no} -----"
180
+ upload_url = upload_params_item["signedUrl"]
181
+ part_no = upload_params_item["partNo"]
147
182
 
148
183
  file_size_ele = 1024 * 1024 * 5 #5M
149
184
  start_position = file_size_ele * (part_no -1)
150
- if part_no * file_size_ele > filesize
151
- read_length = filesize - start_position
185
+ if part_no * file_size_ele > @file_size
186
+ read_length = @file_size - start_position
152
187
  else
153
188
  read_length = file_size_ele
154
189
  end
155
190
 
156
- file = File.open(binary_file, "rb")
191
+ file = File.open(@upload_binary_file, "rb")
157
192
  file.seek(start_position)
158
193
  put_data = file.read(read_length)
159
194
 
160
-
161
- request = nil
195
+ request = create_req(upload_url:upload_url, body_data:put_data, read_length:read_length)
196
+
197
+ request.on_progress do |dltotal, dlnow, ultotal, ulnow|
198
+ if ulnow
199
+ @progress_bar.update_upload_index(upload_part:part_no, upload_size:ulnow)
200
+ @progress_bar.update_upload_progress()
201
+ end
202
+ end
203
+
204
+ eTag_item = nil
205
+
206
+ request.on_complete do |response|
207
+ if response.success?
208
+ @progress_bar.complete_upload_index(upload_part:part_no, complete_size:read_length)
209
+ etag = response.headers["ETag"]
210
+ eTag_item = { partNumber: part_no, tag: etag}
211
+ @upload_eTags << eTag_item
212
+ else
213
+ @progress_bar.delete_upload_index(upload_part:part_no)
214
+ upload_params_item["retryCount"] = upload_params_item["retryCount"] - 1
215
+ if upload_params_item["retryCount"] > 0
216
+ @upload_params_list.push(upload_params_item)
217
+ else
218
+ Funlog.instance.fancyinfo_error("文件#{@upload_binary_file} 上传失败! 😭😭😭")
219
+ raise Informative, "上传文件失败"
220
+ end
221
+ end
222
+ end
223
+
224
+ if hydra_handle.nil?
225
+ request.run
226
+ else
227
+ hydra_handle.queue(request)
228
+ end
162
229
 
230
+ return eTag_item
231
+
232
+ end
233
+
234
+ def create_req(upload_url:nil, body_data:nil, read_length:nil)
235
+
236
+ request = nil
163
237
  proxy_options = {
164
- proxy: ENV['http_proxy'] || ENV['https_proxy'],
238
+ proxy: ENV['http_proxy'] || ENV['https_proxy'],
165
239
  proxyuserpwd: "#{ENV['HTTP_PROXY_USER']}:#{ENV['HTTP_PROXY_PASSWORD']}"
166
240
  }
167
-
241
+
168
242
  if proxy_options[:proxy]
169
243
  request = Typhoeus::Request.new(
170
244
  upload_url,
171
245
  method: :put,
172
246
  proxy: proxy_options[:proxy],
173
247
  proxyuserpwd: proxy_options[:proxyuserpwd],
174
- :body => put_data,
248
+ :body => body_data,
175
249
  :headers => {
176
250
  'Content-Type' => 'application/octet-stream',
177
251
  'token' => @token['token'],
@@ -182,48 +256,25 @@ module Pindo
182
256
  request = Typhoeus::Request.new(
183
257
  upload_url,
184
258
  method: :put,
185
- :body => put_data,
259
+ :body => body_data,
186
260
  :headers => {
187
261
  'Content-Type' => 'application/octet-stream',
188
262
  'token' => @token['token'],
189
263
  'Content-Length' => read_length.to_s
190
264
  }
191
265
  )
192
- end
193
-
194
-
195
- @upload_num = start_position
196
- request.on_progress do |dltotal, dlnow, ultotal, ulnow|
197
- # puts "dltotal (#{dltotal}), dlnow (#{dlnow}), ultotal (#{ultotal}), ulnow (#{ulnow})"
198
- if ulnow
199
- @upload_num = start_position + ulnow
200
- set_progress(@upload_num, filesize)
201
- end
202
- end
203
266
 
204
- return_data = nil
205
- request.on_complete do |response|
206
- if response.success?
207
- # puts "on_complete success"
208
- return_data = response.headers["ETag"]
209
- else
210
- # puts "on_complete fail"
211
- end
212
267
  end
213
-
214
- request.run
215
-
216
- return return_data
217
-
268
+ return request
218
269
  end
219
270
 
220
- def post_upload_finish_req(upload_key:nil, upload_id:nil, eTags:nil)
271
+ def post_upload_finish_req(upload_path_key:nil, upload_id:nil, eTags:nil)
221
272
 
222
273
  boss_url = @baseurl + @request_config["multi_signed_url_upload"]
223
274
 
224
275
  body_params = {
225
276
  functionName:"finish",
226
- fileKey:upload_key,
277
+ fileKey:upload_path_key,
227
278
  uploadId:upload_id,
228
279
  tags:eTags
229
280
  }
@@ -247,16 +298,14 @@ module Pindo
247
298
  end
248
299
 
249
300
 
250
- def post_upload_url_req(upload_key:nil, file_size:nil)
301
+ def post_upload_url_req(upload_path_key:nil, file_ceil_size:nil)
251
302
 
252
- # puts "post_upload_url_req start ..."
253
- # puts "file_size: #{file_size} MB"
254
303
  boss_url = @baseurl + @request_config["multi_signed_url_upload"]
255
304
 
256
305
  body_params = {
257
306
  functionName:"start",
258
- fileKey:upload_key,
259
- fileSize:file_size
307
+ fileKey:upload_path_key,
308
+ fileSize:file_ceil_size
260
309
  }
261
310
 
262
311
  con = HttpClient.create_instance_with_proxy
@@ -273,19 +322,71 @@ module Pindo
273
322
  result_data = JSON.parse(res.body)
274
323
  end
275
324
 
276
- # puts JSON.pretty_generate(result_data)
277
-
278
325
  return result_data
279
326
 
280
327
  end
281
328
 
282
- def set_progress(index_num, total_num, char ='>' )
283
- progress_str = sprintf("%.2f", 100.0 * index_num / total_num )
284
- total_size = sprintf("%.2f", 1.00 * total_num / 1024 /1024 )
285
- index = 40.0 * index_num / total_num
286
- upload_message = "已上传:#{progress_str}\%【" + (char * (index/1).floor).ljust(40.0, '_') + "】Total: #{total_size} M"
287
- Funlog.instance.fancyinfo_update(upload_message)
329
+ class PgyerUploadProgressBar
330
+
331
+ attr_accessor :draw_char
332
+ attr_accessor :complete_size
333
+ attr_accessor :upload_total_size
334
+ attr_accessor :last_update_time
335
+ attr_accessor :update_ing_size
336
+
337
+ def initialize(upload_total_size:nil, draw_char:'>')
338
+
339
+
340
+ @upload_total_size = upload_total_size
341
+ @draw_char = draw_char
342
+ @last_update_time = (Time.now.to_f * 1000).to_i #毫秒
343
+
344
+ @complete_size = 0
345
+ @update_ing_size = {}
346
+ end
347
+
348
+ def update_upload_index(upload_part:nil, upload_size:nil)
349
+ @update_ing_size[upload_part] = upload_size
350
+ end
351
+
352
+ def delete_upload_index(upload_part:nil)
353
+ @update_ing_size[upload_part] = 0
354
+ end
355
+
356
+ def complete_upload_index(upload_part:nil, complete_size:nil)
357
+ @complete_size = @complete_size + complete_size
358
+ @update_ing_size[upload_part] = 0
359
+ end
360
+
361
+ def update_upload_progress()
362
+ time_now = (Time.now.to_f * 1000).to_i #毫秒
363
+ if time_now - @last_update_time > 80
364
+ @last_update_time = time_now
365
+ total_num = @upload_total_size
366
+ index_num = @complete_size
367
+ @update_ing_size.each do |key, value|
368
+ index_num = index_num + value
369
+ end
370
+
371
+ progress_str = sprintf("%.2f", 100.0 * index_num / total_num )
372
+ total_size = sprintf("%.2f", 1.00 * total_num / 1024 /1024 )
373
+ upload_size = sprintf("%.2f", 1.00 * index_num / 1024 /1024 )
374
+ index = 40.0 * index_num / total_num
375
+ upload_message = "已上传:#{upload_size}MB|#{progress_str}\%【" + (@draw_char * (index/1).floor).ljust(40.0, '_') + "】Total:#{total_size}MB"
376
+ Funlog.instance.fancyinfo_update(upload_message)
377
+ end
378
+ end
379
+
288
380
  end
289
381
 
382
+ # def set_progress(index_num, total_num, char ='>' )
383
+ # progress_str = sprintf("%.2f", 100.0 * index_num / total_num )
384
+ # total_size = sprintf("%.2f", 1.00 * total_num / 1024 /1024 )
385
+ # upload_size = sprintf("%.2f", 1.00 * index_num / 1024 /1024 )
386
+ # index = 40.0 * index_num / total_num
387
+ # upload_message = "已上传:#{upload_size}MB|#{progress_str}\%【" + (char * (index/1).floor).ljust(40.0, '_') + "】Total:#{total_size}MB"
388
+ # Funlog.instance.fancyinfo_update(upload_message)
389
+ # end
390
+
290
391
  end
291
392
  end
data/lib/pindo/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Pindo
2
2
 
3
- VERSION = "4.7.7"
3
+ VERSION = "4.7.9"
4
4
 
5
5
  class VersionCheck
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pindo
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.7.7
4
+ version: 4.7.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - wade
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-22 00:00:00.000000000 Z
11
+ date: 2024-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: claide