Class: Pindo::PgyerUploadClient::PgyerUploadProgressBar

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(upload_total_size: nil, draw_char: '>') ⇒ PgyerUploadProgressBar

Returns a new instance of PgyerUploadProgressBar.



457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/pindo/client/pgyeruploadclient.rb', line 457

def initialize(upload_total_size:nil, draw_char:'>')
  @upload_total_size = upload_total_size
  @draw_char = draw_char
  @last_update_time = (Time.now.to_f * 1000).to_i #毫秒

  @complete_size = 0
  @update_ing_size = {}
  @is_done = false
  
  # 添加互斥锁来保护进度条更新
  @mutex = Mutex.new
end

Instance Attribute Details

#complete_sizeObject

Returns the value of attribute complete_size.



451
452
453
# File 'lib/pindo/client/pgyeruploadclient.rb', line 451

def complete_size
  @complete_size
end

#draw_charObject

Returns the value of attribute draw_char.



450
451
452
# File 'lib/pindo/client/pgyeruploadclient.rb', line 450

def draw_char
  @draw_char
end

#is_doneObject

Returns the value of attribute is_done.



455
456
457
# File 'lib/pindo/client/pgyeruploadclient.rb', line 455

def is_done
  @is_done
end

#last_update_timeObject

Returns the value of attribute last_update_time.



453
454
455
# File 'lib/pindo/client/pgyeruploadclient.rb', line 453

def last_update_time
  @last_update_time
end

#update_ing_sizeObject

Returns the value of attribute update_ing_size.



454
455
456
# File 'lib/pindo/client/pgyeruploadclient.rb', line 454

def update_ing_size
  @update_ing_size
end

#upload_total_sizeObject

Returns the value of attribute upload_total_size.



452
453
454
# File 'lib/pindo/client/pgyeruploadclient.rb', line 452

def upload_total_size
  @upload_total_size
end

Instance Method Details

#complete_upload_index(upload_part: nil, complete_size: nil) ⇒ Object



482
483
484
485
486
487
# File 'lib/pindo/client/pgyeruploadclient.rb', line 482

def complete_upload_index(upload_part:nil, complete_size:nil)
    @mutex.synchronize do
        @complete_size = @complete_size + complete_size
        @update_ing_size[upload_part] = 0
    end
end

#delete_upload_index(upload_part: nil) ⇒ Object



476
477
478
479
480
# File 'lib/pindo/client/pgyeruploadclient.rb', line 476

def delete_upload_index(upload_part:nil)
    @mutex.synchronize do
        @update_ing_size[upload_part] = 0
    end
end

#update_upload_index(upload_part: nil, upload_size: nil) ⇒ Object



470
471
472
473
474
# File 'lib/pindo/client/pgyeruploadclient.rb', line 470

def update_upload_index(upload_part:nil, upload_size:nil)
    @mutex.synchronize do
        @update_ing_size[upload_part] = upload_size
    end
end

#update_upload_progressObject



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/pindo/client/pgyeruploadclient.rb', line 489

def update_upload_progress()
    time_now = (Time.now.to_f * 1000).to_i #毫秒
    if time_now - @last_update_time > 80
        @mutex.synchronize do
            @last_update_time = time_now
            total_num = @upload_total_size
            index_num = @complete_size
            @update_ing_size.each do |key, value|
               index_num = index_num + value
            end

            progress_str = sprintf("%.2f", 100.0 * index_num / total_num )
            total_size = sprintf("%.2f", 1.00 * total_num / 1024 /1024 )
            upload_size = sprintf("%.2f", 1.00 * index_num / 1024 /1024 )
            index = 40.0 * index_num / total_num
            upload_message = "已上传:#{upload_size}MB|#{progress_str}\%【" + (@draw_char * (index/1).floor).ljust(40.0, '_') +  "】Total:#{total_size}MB"
            Funlog.instance.fancyinfo_update(upload_message)
            if index_num == total_num && !@is_done
              @is_done = true
              Funlog.instance.fancyinfo_success(upload_message)
            end
        end
    end
end