Class: Backup::FileItem::Cloud

Inherits:
Base
  • Object
show all
Defined in:
lib/backup/file_item/cloud.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#file_hash, #semantic_path, #stat

Constructor Details

#initialize(args = {}) ⇒ Cloud

Returns a new instance of Cloud.



8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#backetObject (readonly)

Returns the value of attribute backet.



6
7
8
# File 'lib/backup/file_item/cloud.rb', line 6

def backet
  @backet
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/backup/file_item/cloud.rb', line 6

def key
  @key
end

#providerObject (readonly)

Returns the value of attribute provider.



6
7
8
# File 'lib/backup/file_item/cloud.rb', line 6

def provider
  @provider
end

#secretObject (readonly)

Returns the value of attribute secret.



6
7
8
# File 'lib/backup/file_item/cloud.rb', line 6

def secret
  @secret
end

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/backup/file_item/cloud.rb', line 6

def timeout
  @timeout
end

Instance Method Details

#create_directory_once(*directories) ⇒ Object



24
25
# File 'lib/backup/file_item/cloud.rb', line 24

def create_directory_once(*directories)
end

#create_file_once(file, data) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



27
28
29
30
31
32
# 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 = "*") ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
# 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) ⇒ Object



70
71
72
73
74
75
76
# 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