Class: Cloudsync::Backend::CloudFiles

Inherits:
Base
  • Object
show all
Defined in:
lib/cloudsync/backend/cloudfiles.rb

Constant Summary

Constants inherited from Base

Base::BUCKET_LIMIT, Base::CONTAINER_LIMIT, Base::OBJECT_LIMIT

Instance Attribute Summary

Attributes inherited from Base

#name, #store, #sync_manager, #upload_prefix

Instance Method Summary collapse

Methods inherited from Base

#copy, #needs_update?, #to_s

Constructor Details

#initialize(opts = {}) ⇒ CloudFiles

Returns a new instance of CloudFiles.



6
7
8
9
10
11
# File 'lib/cloudsync/backend/cloudfiles.rb', line 6

def initialize(opts={})
  @store = ::CloudFiles::Connection.new \
            :username => opts[:username],
            :api_key  => opts[:password]
  super
end

Instance Method Details

#delete(file, delete_container_if_empty = true) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cloudsync/backend/cloudfiles.rb', line 65

def delete(file, delete_container_if_empty=true)
  $LOGGER.info("Deleting file #{file}")
  return if dry_run?

  container = @store.container(file.container)
  
  container.delete_object(file.path)

  if delete_container_if_empty
    container.refresh
    if container.empty?
      $LOGGER.info("Deleting empty container '#{container.name}'")
      @store.delete_container(container.name)
    end
  end

rescue NoSuchContainerException, NoSuchObjectException => e
  $LOGGER.error("Failed to delete file #{file}, container #{container.name}")
end

#download(file) ⇒ Object



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

def download(file)
  start_time = Time.now
  $LOGGER.info("Downloading file #{file}")

  tempfile = file.tempfile

  if !dry_run?
    if obj = get_obj_from_store(file)
      obj.save_to_filename(tempfile.path)
      tempfile.close
    else
      $LOGGER.error("Error downloading file #{file}")
      tempfile.unlink and return nil
    end
  end

  $LOGGER.debug("Finished downloading file #{file} from #{self} (#{Time.now - start_time})")
  tempfile
end

#files_to_sync(upload_prefix = "") ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cloudsync/backend/cloudfiles.rb', line 45

def files_to_sync(upload_prefix="")
  $LOGGER.info("Getting files to sync [#{self}]")
  
  files = []
  containers_to_sync(upload_prefix) do |container|
    container = get_or_create_container(container)
    objects_from_container(container, remove_container_name(upload_prefix)) do |path, hash|
      next if hash[:content_type] == "application/directory"

      file = Cloudsync::File.from_cf_info(container, path, hash, self.to_s)
      if block_given?
        yield file
      else
        files << file
      end
    end
    files
  end
end

#get_file_from_store(file) ⇒ Object



85
86
87
# File 'lib/cloudsync/backend/cloudfiles.rb', line 85

def get_file_from_store(file)
  Cloudsync::File.from_cf_obj( get_obj_from_store(file), self.to_s )
end

#put(file, local_file_path) ⇒ Object

Put the contents of the path #local_file_path# into the Cloudsync::File object #file#



35
36
37
38
39
40
41
42
43
# File 'lib/cloudsync/backend/cloudfiles.rb', line 35

def put(file, local_file_path)
  start_time = Time.now
  $LOGGER.info("Putting #{file} to #{self} (#{file.full_upload_path}).")
  return if dry_run?

  get_or_create_obj_from_store(file).
    load_from_filename(local_file_path)
  $LOGGER.debug("Finished putting #{file} to #{self} (#{Time.now - start_time}s)")
end