class Qiniu2Upyun::Migrating

Attributes

from[R]
migrated_count[R]
record[R]
skipped_count[R]
to[R]

Public Class Methods

new(from, to) click to toggle source
# File lib/qiniu2upyun/migrating.rb, line 5
def initialize(from, to)
  @from           = from
  @to             = to
  @record         = Record.new
  @migrated_count = 0
  @skipped_count  = 0
end

Public Instance Methods

perform!() click to toggle source
# File lib/qiniu2upyun/migrating.rb, line 13
def perform!
  if from.is_a?(Qiniu) && to.is_a?(Upyun)
    from_qiniu_to_upyun
  end
end

Private Instance Methods

cut_items(items, key) click to toggle source
# File lib/qiniu2upyun/migrating.rb, line 82
def cut_items(items, key)
  index = items.index { |item| item['key'] == key }

  index ? items.drop(index+1) : items
end
from_qiniu_to_upyun() click to toggle source
# File lib/qiniu2upyun/migrating.rb, line 21
def from_qiniu_to_upyun
  record.recording do |init_marker, init_key|
    time = Time.now

    if init_key
      sources = get_sources(init_marker)

      marker, items = sources['marker'], cut_items(sources['items'], init_key)

      perform_items(items, init_marker)
    else
      marker = init_marker
    end

    while marker && (_marker = marker.dup)
      sources = get_sources(marker)

      marker, items = sources['marker'], sources['items']

      perform_items(items, _marker)
    end

    puts "\nComplete, migrated %s files and skipped %s files in %.2f seconds.\n\n" %
      [migrated_count.to_s.green, skipped_count.to_s.red, Time.now-time]
  end
end
get_sources(marker) click to toggle source
# File lib/qiniu2upyun/migrating.rb, line 78
def get_sources(marker)
  marker == Record::START_MARKER ? sources = from.list : sources = from.list(marker: marker)
end
perform_item(key) click to toggle source
# File lib/qiniu2upyun/migrating.rb, line 56
def perform_item(key)
  if to.exist?(key)
    puts "\nFetch '#{key}` from #{from.name}:".red
  
    puts "    Already exists in #{to.name} and skip migrating.".red

    false
  else
    puts "\nFetch '#{key}` from #{from.name}:".green

    time = Time.now

    data = from.download(key)

    to.upload(key, data)

    puts ("%60s" % "(#{data.length}B/#{(Time.now-time).round(2)}s)").green

    true
  end
end
perform_items(items, marker) click to toggle source
# File lib/qiniu2upyun/migrating.rb, line 48
def perform_items(items, marker)
  items.each do |item|
    record.set(marker, item['key']) do
      perform_item(item['key']) ? @migrated_count+=1 : @skipped_count+=1
    end
  end
end