Class: MyMediaFTP

Inherits:
Net::FTP
  • Object
show all
Defined in:
lib/mymedia_ftp.rb

Instance Method Summary collapse

Constructor Details

#initialize(s = nil, host: '127.0.0.1', user: 'user', password: '1234', port: 21, debug: false) ⇒ MyMediaFTP

Returns a new instance of MyMediaFTP.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mymedia_ftp.rb', line 11

def initialize(s=nil, host: '127.0.0.1', user: 'user', password: '1234', 
               port: 21, debug: false)

  if s then
    
    r = s.match(/(?<user>\w+):(?<password>\w+)@(?<host>[^:]+)(?:\:(?<port>\d+))?/)
    h = r.named_captures.map {|k,v| [k.to_sym, v]}.to_h
    puts 'h: ' + h.inspect if debug
    user, password, host = h.values.take(3)
    port = h[:port] if h[:port]
    
  end
  
  @debug = debug

  @curdir = '/'
  super()
  connect(host, port)
  (user, password)

end

Instance Method Details

#chdir(dir) ⇒ Object Also known as: cd



33
34
35
36
# File 'lib/mymedia_ftp.rb', line 33

def chdir(dir)
  super(dir)
  @curdir = pwd
end

#cp(src = '', dest = '', direction = :inbound, &blk) ⇒ Object



40
41
42
43
44
45
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
# File 'lib/mymedia_ftp.rb', line 40

def cp(src='', dest='', direction=:inbound, &blk)

  return outbound_cp(src, dest) if direction == :outbound
  
  puts 'cp: ' + src.inspect if @debug
  chdir File.dirname(src)
  FileUtils.mkdir_p dest
  Dir.chdir  dest

  files = list_filenames(src)

  puts 'copying ...'

  files.each do |h|

    name, type = h[:name], h[:type]
    
    puts name
    
    if type == :file then
      begin
        getbinaryfile name, name.downcase.gsub(/ +/,'-')
      rescue Net::FTPPermError => e
        puts 'e: ' + e.inspect
      end
    else
      cp_dir(name, &blk)
    end
    blk.call(name, type) if block_given?
  end

end

#delete(filename) ⇒ Object Also known as: rm



73
74
75
76
# File 'lib/mymedia_ftp.rb', line 73

def delete(filename)
  super(filename)
  'file deleted'
end

#list_filenames(s = @curdir+'/*') ⇒ Object Also known as: ls



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mymedia_ftp.rb', line 78

def list_filenames(s=@curdir+'/*')
  
  if @debug
    puts 'inside list_filenames' 
    puts 's: ' + s.inspect
  end
  
  src = File.dirname(s)

  raw_q = File.basename(s)
  puts 'raw_q: ' + raw_q.inspect if @debug
  
  q = raw_q.gsub('.','\.').gsub('*','.*').gsub('?','.?')\
      .sub(/[^\*\?\.]$/,'.*')

  list(src).inject([]) do |r, x| 

    raw_attr, _, owner, group, filesize, month, day, time, filename = \
        x.split(/ +/,9)
    type = raw_attr =~ /d/ ? :directory : :file
    filename[/^#{q}$/] ? r << {name: filename, type: type} : r
    r
  end

end

#mv(src = '', dest = '') ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/mymedia_ftp.rb', line 106

def mv(src='', dest='')

  puts 'moving ...'
  cp(src, dest) do |file, type| 
    type == :file ? delete(file) : rmdir(file)
  end

end