Class: AssetSync::Storage
- Inherits:
-
Object
- Object
- AssetSync::Storage
- Defined in:
- lib/asset_sync/storage.rb
Defined Under Namespace
Classes: BucketNotFound
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #bucket ⇒ Object
- #connection ⇒ Object
- #delete_extra_remote_files ⇒ Object
- #delete_file(f, remote_files_to_delete) ⇒ Object
- #get_remote_files ⇒ Object
-
#initialize(cfg) ⇒ Storage
constructor
A new instance of Storage.
- #keep_existing_remote_files? ⇒ Boolean
- #local_files ⇒ Object
- #path ⇒ Object
- #sync ⇒ Object
- #upload_file(f) ⇒ Object
- #upload_files ⇒ Object
Constructor Details
#initialize(cfg) ⇒ Storage
Returns a new instance of Storage.
8 9 10 |
# File 'lib/asset_sync/storage.rb', line 8 def initialize(cfg) @config = cfg end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
6 7 8 |
# File 'lib/asset_sync/storage.rb', line 6 def config @config end |
Instance Method Details
#bucket ⇒ Object
16 17 18 |
# File 'lib/asset_sync/storage.rb', line 16 def bucket @bucket ||= connection.directories.get(self.config.fog_directory) end |
#connection ⇒ Object
12 13 14 |
# File 'lib/asset_sync/storage.rb', line 12 def connection @connection ||= Fog::Storage.new(self.config.) end |
#delete_extra_remote_files ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/asset_sync/storage.rb', line 44 def delete_extra_remote_files STDERR.puts "Fetching files to flag for delete" remote_files = get_remote_files from_remote_files_to_delete = (local_files | remote_files) - (local_files & remote_files) STDERR.puts "Flagging #{from_remote_files_to_delete.size} file(s) for deletion" # Delete unneeded remote files bucket.files.each do |f| delete_file(f, from_remote_files_to_delete) end end |
#delete_file(f, remote_files_to_delete) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/asset_sync/storage.rb', line 37 def delete_file(f, remote_files_to_delete) if remote_files_to_delete.include?(f.key) STDERR.puts "Deleting: #{f.key}" f.destroy end end |
#get_remote_files ⇒ Object
32 33 34 35 |
# File 'lib/asset_sync/storage.rb', line 32 def get_remote_files raise BucketNotFound.new("Fog directory: #{self.config.fog_directory} not found.") unless bucket return bucket.files.map { |f| f.key } end |
#keep_existing_remote_files? ⇒ Boolean
20 21 22 |
# File 'lib/asset_sync/storage.rb', line 20 def keep_existing_remote_files? self.config.existing_remote_files? end |
#local_files ⇒ Object
28 29 30 |
# File 'lib/asset_sync/storage.rb', line 28 def local_files Dir["#{path}/assets/**/**"].map { |f| f[path.length+1,f.length-path.length] } end |
#path ⇒ Object
24 25 26 |
# File 'lib/asset_sync/storage.rb', line 24 def path "#{Rails.root.to_s}/public" end |
#sync ⇒ Object
78 79 80 81 82 |
# File 'lib/asset_sync/storage.rb', line 78 def sync delete_extra_remote_files unless keep_existing_remote_files? upload_files STDERR.puts "Done." end |
#upload_file(f) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/asset_sync/storage.rb', line 56 def upload_file(f) STDERR.puts "Uploading: #{f}" file = bucket.files.create( :key => "#{f}", :body => File.open("#{path}/#{f}"), :public => true, :cache_control => "max-age=31557600" ) end |
#upload_files ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/asset_sync/storage.rb', line 66 def upload_files # get a fresh list of remote files remote_files = get_remote_files local_files_to_upload = (remote_files | local_files) - (remote_files & local_files) # Upload new files local_files_to_upload.each do |f| next unless File.file? "#{path}/#{f}" # Only files. upload_file f end end |