Class: CloudStorageSync::Storage
- Inherits:
-
Object
- Object
- CloudStorageSync::Storage
- Defined in:
- lib/cloud-storage-sync/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
- #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/cloud-storage-sync/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/cloud-storage-sync/storage.rb', line 6 def credentials @credentials end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/cloud-storage-sync/storage.rb', line 6 def @options end |
Instance Method Details
#bucket ⇒ Object
17 18 19 20 21 |
# File 'lib/cloud-storage-sync/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/cloud-storage-sync/storage.rb', line 13 def connection @connection ||= Fog::Storage.new(credentials) end |
#delete_unsynced_remote_files ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cloud-storage-sync/storage.rb', line 54 def delete_unsynced_remote_files STDERR.puts "Deleting remote files that no longer exist locally" files_to_delete = (local_files | remote_files) - (local_files & remote_files) bucket.files.each do |f| if files_to_delete.include?(f.etag) STDERR.puts "D #{f.key}" f.destroy end end end |
#force_deletion_sync? ⇒ Boolean
23 24 25 |
# File 'lib/cloud-storage-sync/storage.rb', line 23 def force_deletion_sync? [:force_deletion_sync] == true end |
#local_files ⇒ Object
27 28 29 |
# File 'lib/cloud-storage-sync/storage.rb', line 27 def local_files @local_files ||= Dir.glob("#{Rails.root.to_s}/public/**/*").map{ |f| Digest::MD5.hexdigest(File.read(f)) } end |
#remote_files ⇒ Object
31 32 33 |
# File 'lib/cloud-storage-sync/storage.rb', line 31 def remote_files @remote_files ||= bucket.files.map{ |f| f.etag } end |
#sync ⇒ Object
65 66 67 68 69 |
# File 'lib/cloud-storage-sync/storage.rb', line 65 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cloud-storage-sync/storage.rb', line 35 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/", "") begin obj = bucket.files.get(remote_file) rescue obj = nil end if !obj || (obj.etag != Digest::MD5.hexdigest(File.read(file))) STDERR.print "U " + file f = bucket.files.create(:key => remote_file, :body => File.open(file), :public => true) STDERR.puts " (" + f.etag + ")" end end end end |