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



46
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
# File 'lib/baidupan/cmd/fs_cmd.rb', line 46

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

#copy(from_rpath, to_rpath) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/baidupan/cmd/fs_cmd.rb', line 129

def copy(from_rpath, to_rpath)
  to_rpath += File.basename(from_rpath) if to_rpath[-1] == '/'                                        
  say "from_rpath不能和to_rpath相同" and return if from_rpath == to_rpath

  body = Baidupan::FsCmd.copy(from_rpath, to_rpath).body
  say "success to cp file #{body[:extra][:list][0][:from]} ---> #{body[:extra][:list][0][:to]}"    
end

#delete(rpath) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/baidupan/cmd/fs_cmd.rb', line 140

def delete(rpath)
  if options[:force] || yes?("Are you sure to delte #{rpath}?")
    response = Baidupan::FsCmd.delete(rpath).response
    say "success to delete  #{rpath}"if response.success?
  else
    say "Cancel to delete #{rpath}"
  end
end

#download(rpath, lpath = nil) ⇒ Object



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

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



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

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

#mkdir(rpath) ⇒ Object



114
115
116
# File 'lib/baidupan/cmd/fs_cmd.rb', line 114

def mkdir(rpath)
  print_item Baidupan::FsCmd.mkdir(rpath).body
end

#mv(from_rpath, to_rpath) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/baidupan/cmd/fs_cmd.rb', line 119

def mv(from_rpath, to_rpath)
  to_rpath += File.basename(from_rpath) if to_rpath[-1] == '/'                                        
  say "from_rpath不能和to_rpath相同" and return if from_rpath == to_rpath
  
  body = Baidupan::FsCmd.move(from_rpath, to_rpath).body

  say "success to mv file #{body[:extra][:list][0][:from]} ---> #{body[:extra][:list][0][:to]}"
end

#quotaObject



151
152
153
154
155
# File 'lib/baidupan/cmd/fs_cmd.rb', line 151

def quota
  body = Baidupan::FsCmd.quota.body
  say "总空间为:#{body[:quota]*1.0/1024**3}G"
  say "已用: #{body[:used]*1.0/1024**3}G"
end

#thumbnail(rpath) ⇒ Object



108
109
110
111
# File 'lib/baidupan/cmd/fs_cmd.rb', line 108

def thumbnail(rpath)
  opts = options.dup
  say Baidupan::FsCmd.thumbnail(rpath, opts)
end

#upload(lpath, rpath = nil) ⇒ Object



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

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

#url(rpath) ⇒ Object



100
101
102
# File 'lib/baidupan/cmd/fs_cmd.rb', line 100

def url(rpath)
  say Baidupan::FsCmd.url(rpath)
end