Class: BaiduPcs::Cli::FsCli

Inherits:
BaseCli show all
Defined in:
lib/baidu_pcs/cli/fs_cli.rb

Instance Method Summary collapse

Methods inherited from BaseCli

#config, #quota, #setup

Instance Method Details

#copy(from_rpath, to_rpath) ⇒ Object



78
79
80
# File 'lib/baidu_pcs/cli/fs_cli.rb', line 78

def copy(from_rpath, to_rpath)
  say BaiduPcs::Fs.copy(from_rpath, to_rpath).body
end

#delete(rpath) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/baidu_pcs/cli/fs_cli.rb', line 85

def delete(rpath)
  if options[:force] || yes?("Are you sure to delte #{rpath}?")
    say BaiduPcs::Fs.delete(rpath).body
  else
    say "Cancel to delete #{rpath}"
  end
end

#download(rpath) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/baidu_pcs/cli/fs_cli.rb', line 25

def download(rpath)
  opts = options.dup
  local_app_root = opts.delete(:tosync) ? BaiduPcs::Config.local_app_root : Dir.pwd
  local_path = "#{local_app_root}/#{rpath}"
  body = BaiduPcs::Fs.download(rpath, opts).body
  FileUtils.mkdir_p(File.dirname(local_path))
  if File.exists?(local_path)
    extname = File.extname(local_path)
    timestamp_ext = ".#{Time.now.strftime('%Y%m%d%H%M%S')}-#{rand(100)}#{extname}"  
    if extname.blank?
      local_path += timestamp_ext  
    else
      local_path.gsub!(extname, timestamp_ext)
    end
  end
  File.binwrite(local_path, body)
  say local_path
end

#list(rpath = nil) ⇒ Object



65
66
67
68
# File 'lib/baidu_pcs/cli/fs_cli.rb', line 65

def list(rpath=nil)
  res = BaiduPcs::Fs.list(rpath, options.dup)
  res.body[:list].each{|item| print_item(item) }
end

#meta(rpath) ⇒ Object



56
57
58
# File 'lib/baidu_pcs/cli/fs_cli.rb', line 56

def meta(rpath)
  say BaiduPcs::Fs.meta(rpath).body
end

#mkdir(rpath) ⇒ Object



51
52
53
# File 'lib/baidu_pcs/cli/fs_cli.rb', line 51

def mkdir(rpath)
  say BaiduPcs::Fs.mkdir(rpath).body
end

#move(from_rpath, to_rpath) ⇒ Object



72
73
74
# File 'lib/baidu_pcs/cli/fs_cli.rb', line 72

def move(from_rpath, to_rpath)
  say BaiduPcs::Fs.move(from_rpath, to_rpath).body
end

#search(keyword, rpath = nil) ⇒ Object



96
97
98
# File 'lib/baidu_pcs/cli/fs_cli.rb', line 96

def search(keyword, rpath=nil)
  say BaiduPcs::Fs.search(keyword, rpath, options).body
end

#streamurl(rpath) ⇒ Object



45
46
47
# File 'lib/baidu_pcs/cli/fs_cli.rb', line 45

def streamurl(rpath)
  say BaiduPcs::Fs.streamurl(rpath)
end

#upload(local_path, rpath = nil) ⇒ Object



18
19
20
21
# File 'lib/baidu_pcs/cli/fs_cli.rb', line 18

def upload(local_path, rpath=nil)
  res = BaiduPcs::Fs.upload(File.expand_path(local_path), rpath, options.dup)
  print_item res.body
end