Class: Pindo::PgyerUploadClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/client/pgyeruploadclient.rb

Defined Under Namespace

Classes: PgyerUploadProgressBar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePgyerUploadClient

Returns a new instance of PgyerUploadClient.



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/pindo/client/pgyeruploadclient.rb', line 28

def initialize()

    begin

      @pgyer_token_file = File.join(File::expand_path(Pindoconfig.instance.pindo_dir), ".pgyer_token")
      config_file = File.join(File::expand_path(Pindoconfig.instance.pindo_common_configdir), "pgyer_client_config.json")
      config_json = JSON.parse(File.read(config_file))

      @region = config_json["region"]
      @bucket_name = config_json["bucket_name"]
      @default_url = config_json["default_url"]
      @attach_url = config_json["attach_url"]

      @use_local_wechat_url = config_json["use_local_wechat_url"]
      @baseurl = config_json["pgyerapps_base_url"]
      @request_config = config_json["pgyerapps_req_config"]
      @pgyer_aes_key = config_json["pgyerapps_aes_key"]

      @token = load_token
      
      # 添加互斥锁用于线程安全
      @upload_eTags_mutex = Mutex.new
      @tasks_queue_mutex = Mutex.new
      @active_tasks_mutex = Mutex.new
      @upload_failed_mutex = Mutex.new  # 为上传失败状态添加互斥锁
      @upload_failed = false

    rescue => error
        raise Informative,  "PgyerUploadClient 初始化失败!"
    end

end

Instance Attribute Details

#attach_urlObject

Returns the value of attribute attach_url.



21
22
23
# File 'lib/pindo/client/pgyeruploadclient.rb', line 21

def attach_url
  @attach_url
end

#bucket_nameObject

Returns the value of attribute bucket_name.



18
19
20
# File 'lib/pindo/client/pgyeruploadclient.rb', line 18

def bucket_name
  @bucket_name
end

#default_urlObject

Returns the value of attribute default_url.



20
21
22
# File 'lib/pindo/client/pgyeruploadclient.rb', line 20

def default_url
  @default_url
end

#file_sizeObject

Returns the value of attribute file_size.



24
25
26
# File 'lib/pindo/client/pgyeruploadclient.rb', line 24

def file_size
  @file_size
end

#progress_barObject

Returns the value of attribute progress_bar.



25
26
27
# File 'lib/pindo/client/pgyeruploadclient.rb', line 25

def progress_bar
  @progress_bar
end

#regionObject

Returns the value of attribute region.



17
18
19
# File 'lib/pindo/client/pgyeruploadclient.rb', line 17

def region
  @region
end

#tokenObject

Returns the value of attribute token.



15
16
17
# File 'lib/pindo/client/pgyeruploadclient.rb', line 15

def token
  @token
end

#upload_binary_fileObject

Returns the value of attribute upload_binary_file.



23
24
25
# File 'lib/pindo/client/pgyeruploadclient.rb', line 23

def upload_binary_file
  @upload_binary_file
end

Instance Method Details

#cleanup_worker_threadsObject

清理所有工作线程



209
210
211
212
213
214
215
# File 'lib/pindo/client/pgyeruploadclient.rb', line 209

def cleanup_worker_threads
  @worker_threads.each do |thread|
    # 尝试安全终止线程
    thread.exit if thread.alive?
  end
  @worker_threads.clear
end

#continuous_upload_data_req(concurrency: 1) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/pindo/client/pgyeruploadclient.rb', line 217

def continuous_upload_data_req(concurrency:1)
    # 初始化活动任务计数和条件变量
    @active_tasks = 0
    @task_complete_cv = ConditionVariable.new
    
    # 初始化连续上传,最多启动concurrency个并发任务
    start_tasks = [concurrency, @tasks_queue.size].min
    start_tasks.times { schedule_next_task }
    
    # 设置超时保护
    timeout_seconds = 300  # 5分钟超时
    start_time = Time.now
    
    # 等待所有任务完成
    @tasks_queue_mutex.synchronize do
        while (@active_tasks > 0 || !@tasks_queue.empty?) && !upload_failed?
            # 添加超时保护
            remaining_time = timeout_seconds - (Time.now - start_time)
            if remaining_time <= 0
              set_upload_failed("上传任务超时")
              break
            end
            
            # 等待任务完成通知,最多等待30秒
            @task_complete_cv.wait(@tasks_queue_mutex, [remaining_time, 30].min)
        end
    end
    
    # 检查所有分片是否都上传成功
    if @upload_eTags.length != @expected_parts && !upload_failed?
        set_upload_failed("部分分片上传失败,已上传#{@upload_eTags.length}/#{@expected_parts}")
    end
end

#create_req(upload_url: nil, body_data: nil, read_length: nil) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/pindo/client/pgyeruploadclient.rb', line 345

def create_req(upload_url:nil, body_data:nil, read_length:nil)

    request = nil
    proxy_options = {
      proxy: ENV['http_proxy'] || ENV['https_proxy'],
      proxyuserpwd: "#{ENV['HTTP_PROXY_USER']}:#{ENV['HTTP_PROXY_PASSWORD']}"
    }

    if proxy_options[:proxy]
      request = Typhoeus::Request.new(
        upload_url,
        method: :put,
        proxy: proxy_options[:proxy],
        proxyuserpwd: proxy_options[:proxyuserpwd],
        :body => body_data,
        :headers => {
          'Content-Type' => 'application/octet-stream',
          'token' => @token['token'],
          'Content-Length' => read_length.to_s
        }
      )
    else
      request = Typhoeus::Request.new(
        upload_url,
        method: :put,
        :body => body_data,
        :headers => {
          'Content-Type' => 'application/octet-stream',
          'token' => @token['token'],
          'Content-Length' => read_length.to_s
        }
      )

    end
    return request
end

#load_tokenObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pindo/client/pgyeruploadclient.rb', line 61

def load_token()
    token = nil
    if File.exist?(@pgyer_token_file)
        begin
          data = File.read(@pgyer_token_file)

          data_string = AESHelper::aes_128_ecb_decrypt(@pgyer_aes_key, data)
          temp_token = data_string
          temp_token = JSON.parse(data_string)
          if !temp_token.nil? && !temp_token["token"].nil? && !temp_token["username"].nil?
              token = temp_token
          end
        rescue => error
            raise Informative,  "PgyerUploadClient 加载pgyer token 失败!!!"
        end
    end
    return token
end

#post_upload_finish_req(upload_path_key: nil, upload_id: nil, eTags: nil) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/pindo/client/pgyeruploadclient.rb', line 382

def post_upload_finish_req(upload_path_key:nil, upload_id:nil, eTags:nil)

    boss_url = @baseurl + @request_config["multi_signed_url_upload"]

    body_params = {
        functionName:"finish",
        fileKey:upload_path_key,
        uploadId:upload_id,
        tags:eTags
    }

    begin
      con = HttpClient.create_instance_with_proxy
      res = con.post do |req|
          req.url boss_url
          req.headers['Content-Type'] = 'application/json'
          req.headers['token'] = @token["token"]
          req.body = body_params.to_json
          req.options.timeout = 120  # 设置2分钟超时
      end

      result_data = nil
      if !res.body.nil?
          result_data = JSON.parse(res.body)
      end

      return result_data
    rescue => e
      Funlog.instance.fancyinfo_error("完成上传请求失败: #{e.message}")
      return nil
    end
end

#post_upload_url_req(upload_path_key: nil, file_ceil_size: nil) ⇒ Object



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/pindo/client/pgyeruploadclient.rb', line 416

def post_upload_url_req(upload_path_key:nil, file_ceil_size:nil)

    boss_url = @baseurl + @request_config["multi_signed_url_upload"]

    body_params = {
        functionName:"start",
        fileKey:upload_path_key,
        fileSize:file_ceil_size
    }

    begin
      con = HttpClient.create_instance_with_proxy
      res = con.post do |req|
          req.url boss_url
          req.headers['Content-Type'] = 'application/json'
          req.headers['token'] = @token["token"]
          req.body = body_params.to_json
          req.options.timeout = 60  # 设置1分钟超时
      end

      result_data = nil
      if !res.body.nil?
          result_data = JSON.parse(res.body)
      end

      return result_data
    rescue => e
      Funlog.instance.fancyinfo_error("获取上传URL失败: #{e.message}")
      return nil
    end
end

#process_upload_task(upload_params_item) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/pindo/client/pgyeruploadclient.rb', line 286

def process_upload_task(upload_params_item)
    upload_url = upload_params_item["signedUrl"]
    part_no = upload_params_item["partNo"]
    
    file_size_ele = 1024 * 1024 * 5  #5M
    start_position = file_size_ele * (part_no - 1)
    if part_no * file_size_ele > @file_size
        read_length = @file_size - start_position
    else
        read_length = file_size_ele
    end
    
    file = File.open(@upload_binary_file, "rb")
    begin
        file.seek(start_position)
        put_data = file.read(read_length)
        
        request = create_req(upload_url:upload_url, body_data:put_data, read_length:read_length)
        
        # 设置上传进度回调
        upload_size_last = 0
        request.on_progress do |dltotal, dlnow, ultotal, ulnow|
            if ulnow && ulnow > upload_size_last
                upload_size_last = ulnow
                @progress_bar.update_upload_index(upload_part:part_no, upload_size:ulnow)
                @progress_bar.update_upload_progress()
            end
        end
        
        # 设置请求超时
        request.options[:timeout] = 300  # 5分钟超时
        
        # 执行请求并等待完成
        response = request.run
        
        # 处理响应结果
        if response.success?
            @progress_bar.complete_upload_index(upload_part:part_no, complete_size:read_length)
            etag = response.headers["ETag"]
            if etag.nil? || etag.empty?
              raise "服务器返回的ETag为空"
            end
            eTag_item = { partNumber: part_no, tag: etag}
            @upload_eTags_mutex.synchronize { @upload_eTags << eTag_item }
        else
            @progress_bar.delete_upload_index(upload_part:part_no)
            upload_params_item["retryCount"] = upload_params_item["retryCount"] - 1
            if upload_params_item["retryCount"] > 0
                # 重试任务
                @tasks_queue_mutex.synchronize { @tasks_queue.push(upload_params_item) }
            else
                set_upload_failed("文件#{@upload_binary_file} 分片#{part_no}上传失败: HTTP #{response.code}")
            end
        end
    ensure
        file.close
    end
end

#schedule_next_taskObject



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/pindo/client/pgyeruploadclient.rb', line 251

def schedule_next_task
    # 检查是否应该停止调度
    return if upload_failed?
    
    # 尝试从队列中获取下一个任务
    @tasks_queue_mutex.synchronize do
        unless @tasks_queue.empty?
            upload_params_item = @tasks_queue.pop
            @active_tasks_mutex.synchronize { @active_tasks += 1 }
            
            # 异步处理任务,不阻塞主线程
            worker_thread = Thread.new do
                begin
                    process_upload_task(upload_params_item)
                rescue => e
                    # 捕获并记录任务处理过程中的异常
                    set_upload_failed("处理分片#{upload_params_item["partNo"]}时出错: #{e.message}")
                ensure
                    # 任务完成后,减少活动任务计数并通知等待线程
                    @active_tasks_mutex.synchronize { @active_tasks -= 1 }
                    
                    # 如果队列不为空,调度下一个任务
                    schedule_next_task if !upload_failed?
                    
                    # 通知等待线程任务已完成
                    @tasks_queue_mutex.synchronize { @task_complete_cv.broadcast }
                end
            end
            
            # 保存线程引用以便后续清理
            @worker_threads << worker_thread
        end
    end
end

#set_upload_failed(error_msg = nil) ⇒ Object

安全地设置上传失败状态



201
202
203
204
205
206
# File 'lib/pindo/client/pgyeruploadclient.rb', line 201

def set_upload_failed(error_msg = nil)
  @upload_failed_mutex.synchronize do 
    @upload_failed = true
    Funlog.instance.fancyinfo_error("上传失败: #{error_msg}") if error_msg
  end
end

#upload_failed?Boolean

安全地检查上传失败状态

Returns:

  • (Boolean)


196
197
198
# File 'lib/pindo/client/pgyeruploadclient.rb', line 196

def upload_failed?
  @upload_failed_mutex.synchronize { @upload_failed }
end

#upload_file(binary_file: nil, isAttach: false) ⇒ Object

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/pindo/client/pgyeruploadclient.rb', line 80

def upload_file(binary_file:nil, isAttach:false)

  raise Informative, "上传文件不能为空" if binary_file.nil? || !File.exist?(binary_file)
  
  @upload_binary_file = binary_file
  @file_size = File.size(@upload_binary_file)
  @progress_bar = PgyerUploadProgressBar.new(upload_total_size:@file_size)
  @upload_failed = false  # 重置上传失败标志

  extension = File.extname(@upload_binary_file)
  filename = File.basename(@upload_binary_file)
  size_level = 1024 * 1024
  file_bytes = File.binread(@upload_binary_file)
  checksum = Digest::MD5.hexdigest(file_bytes)
  file_uuid = SecureRandom.uuid
  total_m = sprintf("%.2f", 1.00 * @file_size / 1024 /1024 )


  upload_path = @default_url
  content_disposition = nil
  if isAttach
      upload_path = @default_url + @attach_url
      content_disposition = "attachment; filename=#{filename}"
  end

  upload_path_key = upload_path + file_uuid + extension

  task_num = 10

  puts "文件路径: #{@upload_binary_file}"
  puts "文件大小: #{total_m}M"
  puts "上传路径: #{upload_path_key}"
  puts
  puts "切片大小: 5MB"

  upload_result = nil

  begin
    file_size_param = 1.00 * @file_size / 1024 /1024
    result_data = post_upload_url_req(upload_path_key:upload_path_key, file_ceil_size:file_size_param.ceil)
    
    if result_data.nil? || !result_data.has_key?("data") || !result_data["data"].has_key?("uploadId")
      raise Informative, "获取上传ID失败,请检查网络或服务器状态"
    end
    
    upload_id = result_data["data"]["uploadId"]
    # 创建统一的任务队列
    @tasks_queue = Queue.new
    @worker_threads = []
    upload_params_list = result_data["data"]["uploadParamsList"]
    upload_item_num = upload_params_list.length
    @expected_parts = upload_item_num  # 保存预期的分片数量
    task_num = upload_item_num
    retry_count = 5
    
    # 合理限制线程数
    if task_num < 2
      task_num = 2
    end

    if task_num > 30
      task_num = 30
    end

    # 根据系统CPU核心数自动调整线程数
    available_cores = Etc.respond_to?(:nprocessors) ? Etc.nprocessors : 4
    task_num = [task_num, available_cores * 2].min
    
    # 设置重试次数并将所有任务加入队列
    upload_params_list.each do |item|
      item["retryCount"] = retry_count
      @tasks_queue.push(item)
    end

    puts "切分个数: #{upload_item_num}"
    puts "线程个数: #{task_num}"
    puts "重试次数: #{retry_count}"
    puts

    Funlog.instance.fancyinfo_start("开始上传...")
    @upload_eTags = []
    @active_tasks = 0  # 跟踪活动任务数量

    continuous_upload_data_req(concurrency:task_num)
    
    # 检查上传是否全部成功
    if upload_failed? || @upload_eTags.length != @expected_parts
        upload_result = nil
        Funlog.instance.fancyinfo_error("文件#{@upload_binary_file} 上传失败! 😭😭😭")
        return upload_result
    end

    result_data = post_upload_finish_req(upload_path_key:upload_path_key, upload_id:upload_id, eTags:@upload_eTags)

    if result_data && result_data["code"] == 200
        upload_result = upload_path_key
        Funlog.instance.fancyinfo_success("文件#{@upload_binary_file} 上传成功! 😎😎😎")
    else
        upload_result = nil
        error_msg = result_data && result_data["msg"] ? result_data["msg"] : "未知错误"
        Funlog.instance.fancyinfo_error("文件#{@upload_binary_file} 上传失败: #{error_msg} 😭😭😭")
    end
    
  rescue => e
    upload_result = nil
    Funlog.instance.fancyinfo_error("文件上传过程发生异常: #{e.message} 😭😭😭")
  ensure
    # 确保所有工作线程都被清理
    cleanup_worker_threads
  end

  return upload_result

end