Class: FileUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/captured/file_uploader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ FileUploader

Returns a new instance of FileUploader.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/captured/file_uploader.rb', line 10

def initialize(options)
  @growl_path = options[:growl_path] || "/usr/local/bin/growlnotify"
  @config = YAML.load_file(options[:config_file])
  case @config['upload']['type']
  when"eval"
    @upload_proc = eval_proc
  when"scp"
    @upload_proc = scp_proc
  when"ftp"
  else
    raise "Invalid Type"
  end
end

Class Method Details

.upload(file, options) ⇒ Object



6
7
8
# File 'lib/captured/file_uploader.rb', line 6

def self.upload(file, options)
  self.new(options).process_upload(file)
end

Instance Method Details

#eval_procObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/captured/file_uploader.rb', line 24

def eval_proc
  lambda do |file, remote_name|
    remote_path = nil
    unless eval @config['upload']['command']
      raise "Upload failed: Bad Eval in config file"
    end
    # if the eval defines remote_path we will copy that to the clipboard
    # otherwise we compute it ouselves
    remote_path || "#{@config['upload']['url']}#{remote_name}"
  end
end

#growl(msg, image = "#{File.dirname(File.expand_path(__FILE__))}/../../resources/red_x.png") ⇒ Object



64
65
66
67
68
69
# File 'lib/captured/file_uploader.rb', line 64

def growl(msg, image = "#{File.dirname(File.expand_path(__FILE__))}/../../resources/red_x.png")
  puts "grr: #{msg}"
  if File.exists? @growl_path
    raise "Growl Failed" unless system("#{@growl_path} -t 'Captured' -m '#{msg}' --image '#{image}'")
  end
end

#process_upload(file) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/captured/file_uploader.rb', line 51

def process_upload(file)
  remote_name = Digest::MD5.hexdigest(file+Time.now.to_i.to_s) +  File.extname(file)
  growl("Processing Upload", "#{File.dirname(File.expand_path(__FILE__))}/../../resources/action_run.png")
  remote_path = @upload_proc.call(file, remote_name)
  puts "Uploaded '#{file}' to '#{remote_path}'"
  raise "Copy Failed" unless system("echo '#{remote_path}' | /usr/bin/pbcopy")
  growl("Upload Succeeded", "#{File.dirname(File.expand_path(__FILE__))}/../../resources/green_check.png")
rescue => e 
  puts e
  puts e.backtrace
  growl(e)
end

#scp_procObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/captured/file_uploader.rb', line 36

def scp_proc
  require 'net/scp'
  require 'etc'
  settings = @config['upload']
  lambda do |file, remote_name|
    Net::SCP.upload!(settings['host'],
                     settings['user'] || Etc.getlogin,
                     file,
                     settings['path']+remote_name,
                     :password => settings[:password])

    "#{@config['upload']['url']}#{remote_name}"
  end
end