class Backup::FileItem::Cloud

Attributes

backet[R]
key[R]
provider[R]
secret[R]
timeout[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/backup/file_item/cloud.rb, line 8
def initialize(args = {})
  puts_fail "Empty hash in Cloud initialize method" if args.empty?

  [:key, :secret, :bucket].each do |arg|
    puts_fail "'#{arg.to_s.green}' should not be empty" if args[arg].nil?
    instance_eval %{@#{arg} = args[:#{arg}]}
  end
  @timeout = 60

  try_to_connect_with_cloud
end

Public Instance Methods

create_directory_once(*directories) click to toggle source
# File lib/backup/file_item/cloud.rb, line 24
def create_directory_once(*directories)
end
create_file_once(file, data) click to toggle source
# File lib/backup/file_item/cloud.rb, line 47
def create_file_once(file, data)
  try_to_work_with_cloud do
    #@directory.files.create(
    #  :key => delete_slashes(file),
    #  :body => data
    #)
    @connection.put_object(
      @bucket,
      delete_slashes(file),
      data
    )
  end
end
delete_dir(directory) click to toggle source
# File lib/backup/file_item/cloud.rb, line 34
def delete_dir directory
  try_to_work_with_cloud do
    path = delete_slashes(directory)

    files = @directory.files.all(
      :prefix => path,
      :max_keys => 1_000_000_000
    ).map do |file|
      file.destroy
    end
  end
end
delete_file(path) click to toggle source
# File lib/backup/file_item/cloud.rb, line 27
def delete_file path
  try_to_work_with_cloud do
    file = delete_slashes(path)
    @directory.files.get(file).destroy
  end
end
dir(path, mask = "*") click to toggle source
# File lib/backup/file_item/cloud.rb, line 78
def dir(path, mask = "*")
  path = delete_slashes(path)
  mask = mask.gsub('.', '\.').gsub('*', '[^\/]')

  files = @directory.files.all(
    :prefix => path,
    :max_keys => 1_000_000_000
  ).map(&:key)

  files.map do |item|
    match = item.match(/^#{path}\/([^\/]+#{mask}).*$/)
    match[1] if match
  end.compact.uniq
end
exists?(file) click to toggle source
# File lib/backup/file_item/cloud.rb, line 61
def exists? file
  try_to_work_with_cloud do
    !@directory.files.all(
      :prefix => file,
      :max_keys => 1
    ).nil?
  end
end
read_file(file) click to toggle source
# File lib/backup/file_item/cloud.rb, line 70
def read_file(file)
  try_to_work_with_cloud do
    file = delete_slashes(file)
    remote_file = @directory.files.get(file)
    remote_file.body if remote_file
  end
end
timeout=(time) click to toggle source
# File lib/backup/file_item/cloud.rb, line 20
def timeout= time
  @timeout = time.to_i
end

Private Instance Methods

delete_slashes(str) click to toggle source
# File lib/backup/file_item/cloud.rb, line 95
def delete_slashes(str)
  str.chop! if str =~ /\/$/
  str = str[1, str.length] if str =~ /^\//
  str
end
try_to_connect_with_cloud() click to toggle source
# File lib/backup/file_item/cloud.rb, line 112
def try_to_connect_with_cloud
  begin
    @connection = ::Fog::Storage.new(
      :provider => 'AWS',
      :aws_secret_access_key => @secret,
      :aws_access_key_id => @key
    )

    @directory = @connection.directories.get(@bucket)
  rescue Exception => e
    puts_verbose e.message
    puts_fail "403 Forbidden"
  end

  puts_fail "Bucket '#{@bucket}' is not exists." if @directory.nil?
end
try_to_work_with_cloud() { || ... } click to toggle source
# File lib/backup/file_item/cloud.rb, line 101
def try_to_work_with_cloud(&block)
  begin
    yield
  rescue Exception => e
    sleep @timeout
    try_to_connect_with_cloud 

    yield
  end
end