]> git.draconx.ca Git - homepage.git/blobdiff - lib/imgresize.rb
Don't regenerate smaller images so often.
[homepage.git] / lib / imgresize.rb
index e539f371b0c22a23368bcc2d3db8c5756f17b38e..b14530bc5278e0e2c4821206f1dc289a263550c8 100644 (file)
@@ -20,6 +20,27 @@ class ImgResize < Nanoc::Filter
     identifier :imgresize
     type       :binary
 
+    def fetch_from_cache(filename, params)
+        return unless params[:cache]
+
+        cachefile = File.join(@config[:output_dir], params[:cache])
+        begin
+            s = File.stat(cachefile)
+        rescue Errno::ENOENT
+            return
+        end
+
+        return unless s.mtime >= File.stat(filename).mtime
+
+        begin
+            File.link(cachefile, output_filename)
+        rescue
+            FileUtils.copy_file(cachefile, output_filename)
+        end
+
+        return true
+    end
+
     def run(filename, params = {})
         w = if params[:width] then params[:width].to_i end
         h = if params[:height] then params[:height].to_i end
@@ -32,6 +53,8 @@ class ImgResize < Nanoc::Filter
         end
         args << output_filename
 
-        system('gm', 'convert', *args)
+        unless fetch_from_cache(filename, params)
+            system('gm', 'convert', *args)
+        end
     end
 end