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
# 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) ⇒ Object



130
131
132
133
134
135
136
# 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) ⇒ Object



141
142
143
144
145
146
147
148
# 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) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ 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

#mkdir(rpath) ⇒ Object



115
116
117
# 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) ⇒ Object



120
121
122
123
124
125
126
127
# 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

#quotaObject



152
153
154
155
156
# 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) ⇒ Object



109
110
111
112
# 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) ⇒ 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

#url(rpath) ⇒ Object



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

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