Class: ChocolateRain::FtpWorker

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
lib/chocolate_rain/ftp_worker.rb

Class Method Summary collapse

Class Method Details

.do_workObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chocolate_rain/ftp_worker.rb', line 6

def self.do_work
  @@ftp_creds = YAML.load(File.open("config/fms_config.yml"))["#{Rails.env}"]

  @local_files = []

  ftp = Net::FTP.new
  ftp.passive = true

  puts "Connection"
  ftp.connect(@@ftp_creds["url"], 21)
  ftp.(@@ftp_creds["username"], @@ftp_creds["password"])
  puts "logged in"

  ftp.chdir(@@ftp_creds["directory"])
  puts "chdir"
  filenames = ftp.nlst("*.#{@@ftp_creds["format"]}")
  puts "got files"
  # loop by index
  filenames.each_index do |i|
    begin
      f = File.open("#{Rails.root}/tmp/#{i}.#{@@ftp_creds["format"]}")
      ftp.getbinaryfile(filenames[i], f)
      v = ChocolateRain::YVideo.new()
      v.filename = i 
      v.file_object = f
      v.title = i
      @local_files << "#{i}.#{@@ftp_creds["format"]}"
    rescue Exception => e
      puts "Error in FTP: #{e.message}"
    end
  end

  filenames.each do |filename|
    # ftp.delete("#{filename}")
  end

  @local_files.each do |f|
    begin 
      v.save
    rescue Exception => e
      puts "Exception: #{e.message}"
    end
  end
end