Class: Cloudify::Storage
- Inherits:
-
Object
- Object
- Cloudify::Storage
- Defined in:
- lib/cloudify/storage.rb
Defined Under Namespace
Classes: BucketNotFound
Instance Attribute Summary collapse
-
#credentials ⇒ Object
Returns the value of attribute credentials.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #bucket ⇒ Object
- #connection ⇒ Object
- #delete_unsynced_remote_files ⇒ Object
- #files_to_delete ⇒ Object
- #force_deletion_sync? ⇒ Boolean
-
#initialize(credentials, options) ⇒ Storage
constructor
A new instance of Storage.
- #local_files ⇒ Object
- #remote_files ⇒ Object
- #sync ⇒ Object
- #upload_new_and_changed_files ⇒ Object
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, ) self.credentials = credentials self. = end |
Instance Attribute Details
#credentials ⇒ Object
Returns the value of attribute credentials.
6 7 8 |
# File 'lib/cloudify/storage.rb', line 6 def credentials @credentials end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/cloudify/storage.rb', line 6 def @options end |
Instance Method Details
#bucket ⇒ Object
17 18 19 20 21 |
# File 'lib/cloudify/storage.rb', line 17 def bucket @bucket ||= connection.directories.get([:assets_directory]) raise BucketNotFound.new("Directory '#{[:assets_directory]}' not found") unless @bucket @bucket end |
#connection ⇒ Object
13 14 15 |
# File 'lib/cloudify/storage.rb', line 13 def connection @connection ||= Fog::Storage.new(credentials) end |
#delete_unsynced_remote_files ⇒ Object
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_delete ⇒ Object
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
23 24 25 |
# File 'lib/cloudify/storage.rb', line 23 def force_deletion_sync? [:force_deletion_sync] == true end |
#local_files ⇒ Object
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_files ⇒ Object
33 34 35 |
# File 'lib/cloudify/storage.rb', line 33 def remote_files @remote_files ||= bucket.files.reload.map{ |f| f.etag } end |
#sync ⇒ Object
68 69 70 71 72 |
# File 'lib/cloudify/storage.rb', line 68 def sync upload_new_and_changed_files delete_unsynced_remote_files if [:force_deletion_sync] == true STDERR.puts "Done" end |
#upload_new_and_changed_files ⇒ Object
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 |