Created
January 2, 2013 19:16
-
-
Save dariocravero/4437047 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/padrino-helpers/lib/padrino-helpers.rb b/padrino-helpers/lib/padrino-helpers.rb | |
index 0533540..c99ba39 100644 | |
--- a/padrino-helpers/lib/padrino-helpers.rb | |
+++ b/padrino-helpers/lib/padrino-helpers.rb | |
@@ -49,6 +49,12 @@ module Padrino | |
# | |
def registered(app) | |
app.set :default_builder, 'StandardFormBuilder' | |
+ app.set :fingerprinted_assets, {} | |
+ app.controllers 'fingerprinted-assets' do | |
+ get :asset, :map => '/fingerprinted-assets/*asset' do | |
+ send_file self.settings.fingerprinted_assets["/#{params[:asset].join('/')}"] | |
+ end | |
+ end | |
app.helpers Padrino::Helpers::OutputHelpers | |
app.helpers Padrino::Helpers::TagHelpers | |
app.helpers Padrino::Helpers::AssetTagHelpers | |
diff --git a/padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb b/padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb | |
index 5616c83..9d80e1a 100644 | |
--- a/padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb | |
+++ b/padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb | |
@@ -4,6 +4,7 @@ module Padrino | |
# Helpers related to producing assets (images,stylesheets,js,etc) within templates. | |
# | |
module AssetTagHelpers | |
+ | |
## | |
# Creates a div to display the flash of given type if it exists | |
# | |
@@ -348,8 +349,7 @@ module Padrino | |
ignore_extension = (asset_folder.to_s == kind.to_s) # don't append an extension | |
source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/ | |
result_path = is_absolute ? source : uri_root_path(asset_folder, source) | |
- timestamp = asset_timestamp(result_path, is_absolute) | |
- "#{result_path}#{timestamp}" | |
+ asset_timestamp(result_path, is_absolute) | |
end | |
private | |
@@ -369,15 +369,25 @@ module Padrino | |
# Returns the timestamp mtime for an asset | |
# | |
# @example | |
- # asset_timestamp("some/path/to/file.png") => "?154543678" | |
- # asset_timestamp("/some/absolute/path.png", true) => nil | |
+ # asset_timestamp("some/path/to/file.png") => "some/path/to/file-154543678.png" | |
+ # asset_timestamp("/some/absolute/path.png", true) => "/some/absolute/path.png" | |
# | |
def asset_timestamp(file_path, absolute=false) | |
return nil if file_path =~ /\?/ || (self.class.respond_to?(:asset_stamp) && !self.class.asset_stamp) | |
+ return file_path if absolute | |
+ | |
public_file_path = Padrino.root("public", file_path) if Padrino.respond_to?(:root) | |
- stamp = File.mtime(public_file_path).to_i if public_file_path && File.exist?(public_file_path) | |
- stamp ||= Time.now.to_i unless absolute | |
- "?#{stamp}" if stamp | |
+ stamp = Digest::MD5.hexdigest(File.read(public_file_path)) if public_file_path && File.exist?(public_file_path) | |
+ stamp ||= Digest::MD5.hexdigest(File.read(file_path)) | |
+ extension = File.extname(file_path) | |
+ asset = "#{file_path.chomp(extension)}-#{stamp}#{extension}" | |
+ | |
+ app = Padrino.mounted_apps.first.app_obj | |
+ unless app.settings.fingerprinted_assets.include? asset | |
+ app.settings.fingerprinted_assets[asset] = public_file_path || file_path | |
+ end | |
+ | |
+ "/fingerprinted-assets#{asset}" | |
end | |
### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment