Class: Lhj::Command::OSS::Upload

Inherits:
Lhj::Command::OSS show all
Defined in:
lib/lhj/command/oss/upload.rb

Overview

OSS file upload

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Lhj::Command

#auto_spin, #begin_title, #run, #stop

Constructor Details

#initialize(argv) ⇒ Upload

Returns a new instance of Upload.



29
30
31
32
33
34
# File 'lib/lhj/command/oss/upload.rb', line 29

def initialize(argv)
  @current_path = argv.shift_argument || Dir.pwd
  @tag = argv.option('tag')
  @cli = HighLine.new
  super
end

Class Method Details

.optionsObject



23
24
25
26
27
# File 'lib/lhj/command/oss/upload.rb', line 23

def self.options
  [
    %w[--tag 标签]
  ]
end

Instance Method Details

#handleObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lhj/command/oss/upload.rb', line 36

def handle
  file_list = []
  Dir.glob("#{@current_path}/*").sort { |a, b| File.basename(a) <=> File.basename(b) }.each_with_index do |f, i|
    file_list << f
    puts "#{i}.#{File.basename(f)}".yellow
  end
  idx = @cli.ask('请选择上传文件序号: '.green)
  idx_arr = idx.split(',').map(&:strip).filter { |v| v.length.positive? }
  results = []
  idx_arr.each do |i|
    ma = /(?<begin>[0-9]*)\D*-\D*(?<end>[0-9]*)/.match(i)
    results << file_list[ma[:begin].to_i..ma[:end].to_i] if ma
    results << file_list[i.to_i] unless ma
  end
  results = results.flatten
  results.each do |f|
    file_name = File.basename(f)
    file_name = rename_oss_key(file_name) if results.length == 1
    puts "上传的文件名:#{file_name}".yellow
    upload(f, file_name)
  end
end