Class: Cloudify::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudify/storage.rb

Defined Under Namespace

Classes: BucketNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials, options) ⇒ Storage

Returns a new instance of Storage.



8
9
10
11
# File 'lib/cloudify/storage.rb', line 8

def initialize(credentials, options)
  self.credentials = credentials
  self.options = options
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



6
7
8
# File 'lib/cloudify/storage.rb', line 6

def credentials
  @credentials
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/cloudify/storage.rb', line 6

def options
  @options
end

Instance Method Details

#bucketObject

Raises:



17
18
19
20
21
# File 'lib/cloudify/storage.rb', line 17

def bucket
  @bucket ||= connection.directories.get(options[:assets_directory])
  raise BucketNotFound.new("Directory '#{options[:assets_directory]}' not found") unless @bucket
  @bucket
end

#connectionObject



13
14
15
# File 'lib/cloudify/storage.rb', line 13

def connection
  @connection ||= Fog::Storage.new(credentials)
end

#delete_unsynced_remote_filesObject



58
59
60
61
62
63
64
65
66
# File 'lib/cloudify/storage.rb', line 58

def delete_unsynced_remote_files
  STDERR.puts "Deleting remote files that no longer exist locally"
  bucket.files.each do |f|
    if files_to_delete.include?(f.etag)
      STDERR.puts "D #{f.key}"
      f.destroy
    end
  end
end

#files_to_deleteObject



37
38
39
# File 'lib/cloudify/storage.rb', line 37

def files_to_delete
  @files_to_delete ||= (local_files | remote_files) - (local_files & remote_files)
end

#force_deletion_sync?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/cloudify/storage.rb', line 23

def force_deletion_sync?
  options[:force_deletion_sync] == true
end

#local_filesObject



27
28
29
30
31
# File 'lib/cloudify/storage.rb', line 27

def local_files
  @local_files ||= Dir.glob("#{Rails.root.to_s}/public/**/*").map do |f|         
    Digest::MD5.hexdigest(File.read(f)) unless File.directory?(f)
  end
end

#remote_filesObject



33
34
35
# File 'lib/cloudify/storage.rb', line 33

def remote_files
  @remote_files ||= bucket.files.reload.map{ |f| f.etag }
end

#syncObject



68
69
70
71
72
# File 'lib/cloudify/storage.rb', line 68

def sync
  upload_new_and_changed_files
  delete_unsynced_remote_files if options[:force_deletion_sync] == true
  STDERR.puts "Done"
end

#upload_new_and_changed_filesObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cloudify/storage.rb', line 41

def upload_new_and_changed_files
  STDERR.puts "Uploading new and changed files"
  Dir.glob("public/**/*").each do |file|
    if File.file?(file)
      remote_file = file.gsub("public/", "")
      obj = bucket.files.head(remote_file)
      get_file = File.open(file)
      if !obj || (obj.etag != Digest::MD5.hexdigest(get_file.read))
        STDERR.print "U " + file
        Cloudify.invalidator << "/#{obj.key}" if obj
        f = bucket.files.create(:key => remote_file, :body => get_file, :public => true, :expires => Time.now)
        STDERR.puts " (" + f.etag + ")"
      end
    end
  end
end