class Baidupan::Cmd::FsCmd
Public Instance Methods
batch_upload(ldir, rdir, file_pattern="*")
click to toggle source
# 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
copy(from_rpath, to_rpath)
click to toggle source
# File lib/baidupan/cmd/fs_cmd.rb, line 130 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)
click to toggle source
# File lib/baidupan/cmd/fs_cmd.rb, line 141 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)
click to toggle source
# File lib/baidupan/cmd/fs_cmd.rb, line 81 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)
click to toggle source
# 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
mkdir(rpath)
click to toggle source
# File lib/baidupan/cmd/fs_cmd.rb, line 115 def mkdir(rpath) print_item Baidupan::FsCmd.mkdir(rpath).body end
mv(from_rpath, to_rpath)
click to toggle source
# File lib/baidupan/cmd/fs_cmd.rb, line 120 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
print_item(item)
click to toggle source
# File lib/baidupan/cmd/fs_cmd.rb, line 11 def print_item(item) new_items = [] new_items << item[:fs_id] new_items << "#{item[:path].sub(Baidupan::Config.app_root + '/', '')}#{"/" if item[:isdir] == 1}" new_items << "#{Time.at(item[:mtime])}" print_in_columns new_items end
quota()
click to toggle source
# File lib/baidupan/cmd/fs_cmd.rb, line 152 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)
click to toggle source
# File lib/baidupan/cmd/fs_cmd.rb, line 109 def thumbnail(rpath) opts = options.dup say Baidupan::FsCmd.thumbnail(rpath, opts) end
upload(lpath, rpath=nil)
click to toggle source
# 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
url(rpath)
click to toggle source
# File lib/baidupan/cmd/fs_cmd.rb, line 101 def url(rpath) say Baidupan::FsCmd.url(rpath) end