Class: FileFM::Uploaders::Cloudfiles

Inherits:
Object
  • Object
show all
Defined in:
lib/filefm/uploaders/cloudfiles.rb

Class Method Summary collapse

Class Method Details

.upload(source, destination, options) ⇒ Object



5
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
50
51
52
53
54
55
# File 'lib/filefm/uploaders/cloudfiles.rb', line 5

def self.upload(source, destination, options)
  uri = URI.parse(destination)
  container = uri.host
  object = uri.path

  if (not options[:username] or not options[:password])
    raise "Invalid Credentials"
  end
  secure = options[:secure] == true 
  scheme = "https"
  username = options[:username]
  password = options[:password]

  #puts "#{scheme}://#{uri.host}/auth/v1.0"
  out = RestClient.get "#{scheme}://auth.api.rackspacecloud.com/v1.0", 'X-Auth-User' => username, 'X-Auth-Key' => password
  storage_url = out.headers[:x_storage_url]
  auth_token = out.headers[:x_auth_token]
  raise "Error authenticating" unless out.code == 204
  
  begin
    out = RestClient.get storage_url + "/#{container}", 'X-Auth-Token' => auth_token
  rescue
    raise "Error accessing the container: #{container}"  
  end

  if options[:progressbar]
    pbar = ProgressBar.new "Progress", 100
    fsize = File.size(source)
    count = 0
  end

  headers = { 'X-Auth-Token' => auth_token, 'Content-Type' => "application/json" }
  path = storage_url + "/#{container}#{object}"

  res = FileFM::StreamingUploader.put(
      path,
      :headers => { 'X-Auth-Token' => auth_token }, :file => File.open(source) 
  ) do |size|
      if block_given?
        yield size
      elsif options[:progressbar]
       count += size
       per = (100*count)/fsize 
       pbar.set per
      else
      end
    end
  if options[:progressbar]
    pbar.finish
  end
end