lib/juicer/cache_buster.rb in juicer-1.0.22 vs lib/juicer/cache_buster.rb in juicer-1.1.0
- old
+ new
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
+require 'digest/md5'
module Juicer
#
# Assists in creating filenames that reflect the last change to the file. These
# kinds of filenames are useful when serving static content through a web server.
@@ -78,11 +79,11 @@
# See <tt>#hard</tt> and <tt>#soft</tt> for explanation of the parameter
# argument.
#
def self.path(file, type = :soft, parameter = DEFAULT_PARAMETER)
return file if file =~ /data:.*;base64/
- type = [:soft, :hard, :rails].include?(type) ? type : :soft
+ type = [:soft, :hard, :rails, :md5].include?(type) ? type : :soft
parameter = nil if type == :rails
file = self.clean(file, parameter)
filename = file.split("?").first
raise ArgumentError.new("#{file} could not be found") unless File.exists?(filename)
mtime = File.mtime(filename).to_i
@@ -90,16 +91,30 @@
if type == :soft
parameter = "#{parameter}=".sub(/^=$/, '')
return "#{file}#{file.index('?') ? '&' : '?'}#{parameter}#{mtime}"
elsif type == :rails
return "#{file}#{file.index('?') ? '' : "?#{mtime}"}"
+ elsif type == :md5
+ md5 = Digest::MD5.hexdigest(File.read(filename))
+ return file.sub(/(\.[^\.]+$)/, "-#{parameter}#{md5}" + '\1')
end
file.sub(/(\.[^\.]+$)/, "-#{parameter}#{mtime}" + '\1')
end
#
+ # Add a md5 cache buster to a filename. The parameter is an optional prefix
+ # that is added before the md5 digest. It results in filenames of the form:
+ # <tt>file-[parameter name][md5].suffix</tt>, ie
+ # <tt>images/logo-cb4fdbd4c637ad377adf0fc0c88f6854b3.png</tt> which is the case for the default
+ # parameter name "cb" (as in *c*ache *b*uster).
+ #
+ def self.md5(file, parameter = DEFAULT_PARAMETER)
+ self.path(file, :md5, parameter)
+ end
+
+ #
# Add a hard cache buster to a filename. The parameter is an optional prefix
# that is added before the mtime timestamp. It results in filenames of the form:
# <tt>file-[parameter name][timestamp].suffix</tt>, ie
# <tt>images/logo-cb1234567890.png</tt> which is the case for the default
# parameter name "cb" (as in *c*ache *b*uster).
@@ -138,10 +153,10 @@
else
query_param = "#{parameter}="
new_file = file.sub(/#{query_param}\d+&?/, "").sub(/(\?|&)$/, "")
return new_file unless new_file == file
- file.sub(/-#{parameter}\d+(\.\w+)($|\?)/, '\1\2')
+ file.sub(/-#{parameter}[0-9a-f]+(\.\w+)($|\?)/, '\1\2')
end
end
end
end