Class: AssetSync::Storage

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

Defined Under Namespace

Classes: BucketNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#configObject

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#bucketObject



16
17
18
# File 'lib/asset_sync/storage.rb', line 16

def bucket
  @bucket ||= connection.directories.get(self.config.fog_directory)
end

#connectionObject



12
13
14
# File 'lib/asset_sync/storage.rb', line 12

def connection
  @connection ||= Fog::Storage.new(self.config.fog_options)
end

#delete_extra_remote_filesObject



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_filesObject

Raises:



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

Returns:

  • (Boolean)


20
21
22
# File 'lib/asset_sync/storage.rb', line 20

def keep_existing_remote_files?
  self.config.existing_remote_files?
end

#local_filesObject



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

#pathObject



24
25
26
# File 'lib/asset_sync/storage.rb', line 24

def path
  "#{Rails.root.to_s}/public"
end

#syncObject



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_filesObject



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