Class: AssetSync::Storage
- Inherits:
-
Object
- Object
- AssetSync::Storage
- Defined in:
- lib/asset_sync/storage.rb
Defined Under Namespace
Classes: BucketNotFound
Constant Summary collapse
- REGEXP_FINGERPRINTED_FILES =
/^(.*)\/([^-]+)-[^\.]+\.([^\.]+)$/
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #always_upload_files ⇒ Object
- #bucket ⇒ Object
- #connection ⇒ Object
- #delete_extra_remote_files ⇒ Object
- #delete_file(f, remote_files_to_delete) ⇒ Object
- #files_to_invalidate ⇒ Object
- #files_with_custom_headers ⇒ Object
- #get_local_files ⇒ Object
- #get_remote_files ⇒ Object
- #ignored_files ⇒ Object
-
#initialize(cfg) ⇒ Storage
constructor
A new instance of Storage.
- #keep_existing_remote_files? ⇒ Boolean
- #local_files ⇒ Object
- #log(msg) ⇒ Object
- #path ⇒ Object
- #sync ⇒ Object
- #upload_file(f) ⇒ Object
- #upload_files ⇒ Object
Constructor Details
#initialize(cfg) ⇒ Storage
Returns a new instance of Storage.
10 11 12 |
# File 'lib/asset_sync/storage.rb', line 10 def initialize(cfg) @config = cfg end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
8 9 10 |
# File 'lib/asset_sync/storage.rb', line 8 def config @config end |
Instance Method Details
#always_upload_files ⇒ Object
43 44 45 |
# File 'lib/asset_sync/storage.rb', line 43 def always_upload_files (self.config.always_upload) end |
#bucket ⇒ Object
18 19 20 21 |
# File 'lib/asset_sync/storage.rb', line 18 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 |
#connection ⇒ Object
14 15 16 |
# File 'lib/asset_sync/storage.rb', line 14 def connection @connection ||= Fog::Storage.new(self.config.) end |
#delete_extra_remote_files ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/asset_sync/storage.rb', line 100 def delete_extra_remote_files log "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 - ignored_files - always_upload_files log "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
93 94 95 96 97 98 |
# File 'lib/asset_sync/storage.rb', line 93 def delete_file(f, remote_files_to_delete) if remote_files_to_delete.include?(f.key) log "Deleting: #{f.key}" f.destroy end end |
#files_to_invalidate ⇒ Object
51 52 53 |
# File 'lib/asset_sync/storage.rb', line 51 def files_to_invalidate self.config.invalidate.map { |filename| File.join("/", self.config.assets_prefix, filename) } end |
#files_with_custom_headers ⇒ Object
47 48 49 |
# File 'lib/asset_sync/storage.rb', line 47 def files_with_custom_headers self.config.custom_headers.inject({}) { |h,(k, v)| h[File.join(self.config.assets_prefix, k)] = v; h; } end |
#get_local_files ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/asset_sync/storage.rb', line 55 def get_local_files if self.config.manifest if ActionView::Base.respond_to?(:assets_manifest) log "Using: Rails 4.0 manifest access" manifest = Sprockets::Manifest.new(ActionView::Base.assets_manifest.environment, ActionView::Base.assets_manifest.dir) return manifest.assets.values.map { |f| File.join(self.config.assets_prefix, f) } elsif File.exist?(self.config.manifest_path) log "Using: Manifest #{self.config.manifest_path}" yml = YAML.load(IO.read(self.config.manifest_path)) return yml.map do |original, compiled| # Upload font originals and compiled if original =~ /^.+(eot|svg|ttf|woff)$/ [original, compiled] else compiled end end.flatten.map { |f| File.join(self.config.assets_prefix, f) }.uniq! else log "Warning: Manifest could not be found" end end log "Using: Directory Search of #{path}/#{self.config.assets_prefix}" Dir.chdir(path) do to_load = self.config.assets_prefix.present? ? "#{self.config.assets_prefix}/**/**" : '**/**' Dir[to_load] end end |
#get_remote_files ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/asset_sync/storage.rb', line 84 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 |
#ignored_files ⇒ Object
35 36 37 |
# File 'lib/asset_sync/storage.rb', line 35 def ignored_files (self.config.ignored_files) end |
#keep_existing_remote_files? ⇒ Boolean
27 28 29 |
# File 'lib/asset_sync/storage.rb', line 27 def keep_existing_remote_files? self.config.existing_remote_files? end |
#local_files ⇒ Object
39 40 41 |
# File 'lib/asset_sync/storage.rb', line 39 def local_files @local_files ||= get_local_files.uniq end |
#log(msg) ⇒ Object
23 24 25 |
# File 'lib/asset_sync/storage.rb', line 23 def log(msg) AssetSync.log(msg) end |
#path ⇒ Object
31 32 33 |
# File 'lib/asset_sync/storage.rb', line 31 def path self.config.public_path end |
#sync ⇒ Object
218 219 220 221 222 223 224 |
# File 'lib/asset_sync/storage.rb', line 218 def sync # fixes: https://github.com/rumblelabs/asset_sync/issues/19 log "AssetSync: Syncing." upload_files delete_extra_remote_files unless keep_existing_remote_files? log "AssetSync: Done." end |
#upload_file(f) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/asset_sync/storage.rb', line 113 def upload_file(f) # TODO output files in debug logs as asset filename only. one_year = 31557600 ext = File.extname(f)[1..-1] mime = MultiMime.lookup(ext) file = { :key => f, :body => File.open("#{path}/#{f}"), :public => true, :content_type => mime } uncompressed_filename = f.sub(/\.gz\z/, '') basename = File.basename(uncompressed_filename, File.extname(uncompressed_filename)) if /-[0-9a-fA-F]{32,}$/.match(basename) file.merge!({ :cache_control => "public, max-age=#{one_year}", :expires => CGI.rfc1123_date(Time.now + one_year) }) end # overwrite headers if applicable, you probably shouldn't specific key/body, but cache-control headers etc. if files_with_custom_headers.has_key? f file.merge! files_with_custom_headers[f] log "Overwriting #{f} with custom headers #{files_with_custom_headers[f].to_s}" elsif key = self.config.custom_headers.keys.detect {|k| f.match(Regexp.new(k))} headers = {} self.config.custom_headers[key].each do |k, value| headers[k.to_sym] = value end file.merge! headers log "Overwriting matching file #{f} with custom headers #{headers.to_s}" end 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. log "Ignoring: #{f}" ignore = true elsif config.gzip? && File.exist?(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) file.merge!({ :key => f, :body => File.open(gzipped), :content_encoding => 'gzip' }) log "Uploading: #{gzipped} in place of #{f} saving #{percentage}%" else percentage = ((original_size.to_f/gzipped_size.to_f)*100).round(2) log "Uploading: #{f} instead of #{gzipped} (compression increases this file by #{percentage}%)" end else if !config.gzip? && File.extname(f) == ".gz" # set content encoding for gzipped files this allows cloudfront to properly handle requests with Accept-Encoding # http://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html uncompressed_filename = f[0..-4] ext = File.extname(uncompressed_filename)[1..-1] mime = MultiMime.lookup(ext) file.merge!({ :content_type => mime, :content_encoding => 'gzip' }) end log "Uploading: #{f}" end if config.aws? && config.aws_rrs? file.merge!({ :storage_class => 'REDUCED_REDUNDANCY' }) end file = bucket.files.create( file ) unless ignore end |
#upload_files ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/asset_sync/storage.rb', line 197 def upload_files # get a fresh list of remote files remote_files = ignore_existing_remote_files? ? [] : get_remote_files # fixes: https://github.com/rumblelabs/asset_sync/issues/19 local_files_to_upload = local_files - ignored_files - remote_files + always_upload_files local_files_to_upload = (local_files_to_upload + get_non_fingerprinted(local_files_to_upload)).uniq # Upload new files local_files_to_upload.each do |f| next unless File.file? "#{path}/#{f}" # Only files. upload_file f end if self.config.cdn_distribution_id && files_to_invalidate.any? log "Invalidating Files" cdn ||= Fog::CDN.new(self.config..except(:region)) data = cdn.post_invalidation(self.config.cdn_distribution_id, files_to_invalidate) log "Invalidation id: #{data.body["Id"]}" end end |