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
19
# File 'lib/asset_sync/storage.rb', line 16

def bucket
  # fixes: https://github.com/rumblelabs/asset_sync/issues/18
  @bucket ||= connection.directories.get(self.config.fog_directory, :prefix => self.config.assets_prefix)
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



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/asset_sync/storage.rb', line 63

def delete_extra_remote_files
  STDERR.puts "Fetching files to flag for delete"
  remote_files = get_remote_files
  # fixes: https://github.com/rumblelabs/asset_sync/issues/19
  from_remote_files_to_delete = remote_files - local_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



56
57
58
59
60
61
# File 'lib/asset_sync/storage.rb', line 56

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_local_filesObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/asset_sync/storage.rb', line 33

def get_local_files
  if self.config.manifest
    if File.exists?(self.config.manifest_path)
      yml = YAML.load(IO.read(self.config.manifest_path))
      STDERR.puts "Using: Manifest #{self.config.manifest_path}"
      return yml.values.map { |f| File.join(self.config.assets_prefix, f) }
    else
      STDERR.puts "Warning: manifest.yml not found at #{self.config.manifest_path}"
    end
  end
  STDERR.puts "Using: Directory Search of #{path}/#{self.config.assets_prefix}"
  Dir["#{path}/#{self.config.assets_prefix}/**/**"].map { |f| f[path.length+1,f.length-path.length] }
end

#get_remote_filesObject

Raises:



47
48
49
50
51
52
53
54
# File 'lib/asset_sync/storage.rb', line 47

def get_remote_files
  raise BucketNotFound.new("#{self.config.fog_provider} Bucket: #{self.config.fog_directory} not found.") unless bucket
  # fixes: https://github.com/rumblelabs/asset_sync/issues/16
  #        (work-around for https://github.com/fog/fog/issues/596)
  files = []
  bucket.files.each { |f| files << f.key }
  return files
end

#keep_existing_remote_files?Boolean

Returns:

  • (Boolean)


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

def keep_existing_remote_files?
  self.config.existing_remote_files?
end

#local_filesObject



29
30
31
# File 'lib/asset_sync/storage.rb', line 29

def local_files
  @local_files ||= get_local_files
end

#pathObject



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

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

#syncObject



133
134
135
136
137
138
# File 'lib/asset_sync/storage.rb', line 133

def sync
  # fixes: https://github.com/rumblelabs/asset_sync/issues/19
   upload_files
   delete_extra_remote_files unless keep_existing_remote_files?
   STDERR.puts "Done."
end

#upload_file(f) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/asset_sync/storage.rb', line 76

def upload_file(f)
  # TODO output files in debug logs as asset filename only.
  file = {
    :key => f,
    :body => File.open("#{path}/#{f}"),
    :public => true,
    :cache_control => "public, max-age=31557600",
    :expires => CGI.rfc1123_date(Time.now + 1.year)
  }

  gzipped = "#{path}/#{f}.gz"
  ignore = false

  if config.gzip? && File.extname(f) == ".gz"
    # Don't bother uploading gzipped assets if we are in gzip_compression mode
    # as we will overwrite file.css with file.css.gz if it exists.
    STDERR.puts "Ignoring: #{f}"
    ignore = true
  elsif config.gzip? && File.exists?(gzipped)
    original_size = File.size("#{path}/#{f}")
    gzipped_size  = File.size(gzipped)

    if gzipped_size < original_size
      percentage = ((gzipped_size.to_f/original_size.to_f)*100).round(2)
      ext = File.extname( f )[1..-1]
      mime = Mime::Type.lookup_by_extension( ext )
      file.merge!({
        :key => f,
        :body => File.open(gzipped),
        :content_type     => mime,
        :content_encoding => 'gzip'
      })
      STDERR.puts "Uploading: #{gzipped} in place of #{f} saving #{percentage}%"
    else
      percentage = ((original_size.to_f/gzipped_size.to_f)*100).round(2)
      STDERR.puts "Uploading: #{f} instead of #{gzipped} (compression increases this file by #{percentage}%)"
    end
  else
    STDERR.puts "Uploading: #{f}"
  end

  file = bucket.files.create( file ) unless ignore
end

#upload_filesObject



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/asset_sync/storage.rb', line 120

def upload_files
  # get a fresh list of remote files
  remote_files = get_remote_files
  # fixes: https://github.com/rumblelabs/asset_sync/issues/19
  local_files_to_upload = local_files - remote_files

  # Upload new files
  local_files_to_upload.each do |f|
    next unless File.file? "#{path}/#{f}" # Only files.
    upload_file f
  end
end