Class: Baidupan::Cmd::FsCmd

Inherits:
Base
  • Object
show all
Defined in:
lib/baidupan/cmd/fs_cmd.rb

Instance Method Summary collapse

Methods inherited from Base

#config, #setup, #show_config

Instance Method Details

#batch_upload(ldir, rdir, file_pattern = "*") ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/baidupan/cmd/fs_cmd.rb', line 47

def batch_upload(ldir, rdir, file_pattern="*")
  opts = options.dup
  old_ldir = ldir
  
  if opts[:recursive]
    ldir = File.join(ldir, "**")
    opts.delete(:recursive)
  end


  files = Dir.glob(File.join(ldir, file_pattern)).select{|f| File.file?(f)}
  
  if options[:show]
    files.each{|file| puts file}
    say "total #{files.size} files"
    return
  end

  count = 0
  origin_rdir = rdir
  current_dir = Regexp.new("^#{File.join(old_ldir, '')}")
  
  files.each do |file|
    dirname = File.dirname(file.gsub(current_dir, ''))
    dirname = '' if dirname == '.'

    rdir = File.join(origin_rdir, dirname)
    Baidupan::FsCmd.upload(file, rdir, opts)
    say file
    count += 1
  end
  say "total upload #{count} files"
end

#download(rpath, lpath = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/baidupan/cmd/fs_cmd.rb', line 82

def download(rpath, lpath=nil)
  lpath = (lpath||rpath).dup
  res = Baidupan::FsCmd.download(rpath, lpath, options.dup)
  
  if File.exists?(lpath)
    extname = File.extname(lpath)
    timestamp_name = "_#{Time.now.strftime(Baidupan::Config.time_format)}_#{rand(10)}#{extname}"

    if extname.empty?
      lpath += timestamp_name
    else
      lpath.gsub!(extname, timestamp_name)
    end
  end

  File.binwrite(lpath, res.body)
  say "download and save at'#{lpath}'..."
end

#list(rpath = nil) ⇒ Object



21
22
23
24
25
26
# File 'lib/baidupan/cmd/fs_cmd.rb', line 21

def list(rpath=nil)
 res = Baidupan::FsCmd.list(rpath)
 res.body[:list].each do |item|
   print_item(item)
 end
end

#upload(lpath, rpath = nil) ⇒ Object



33
34
35
36
# File 'lib/baidupan/cmd/fs_cmd.rb', line 33

def upload(lpath, rpath=nil)
  res = Baidupan::FsCmd.upload(lpath, rpath, options.dup)
  print_item res.body
end