about summary refs log tree commit homepage
path: root/extras
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2014-09-20 10:13:14 +0000
committerEric Wong <e@80x24.org>2014-09-20 10:13:14 +0000
commit091f7bcbeb43d4695ec84b906a162d940195add3 (patch)
treedbb289c49878d718e47e2ef42d80454d188614fa /extras
parentd7383b15d3628d56ec0f46c501214d888865acf8 (diff)
downloadyahns-091f7bcbeb43d4695ec84b906a162d940195add3.tar.gz
This keeps autoindex-generated indices from being cluttered with
redundant .gz files while still showing stuff like tar.gz files
without a plain .tar companion.
Diffstat (limited to 'extras')
-rw-r--r--extras/autoindex.rb20
1 files changed, 18 insertions, 2 deletions
diff --git a/extras/autoindex.rb b/extras/autoindex.rb
index 0fe2f53..2305c3c 100644
--- a/extras/autoindex.rb
+++ b/extras/autoindex.rb
@@ -13,12 +13,13 @@ class Autoindex
   FN = %{<a href="%s">%s</a>}
   TFMT = "%Y-%m-%d %H:%M"
 
-  def initialize(app, index = %w(index.html))
+  def initialize(app, index = %w(index.html), skip_gzip_static = true)
     app.respond_to?(:root) or raise ArgumentError,
        "wrapped app #{app.inspect} does not respond to :root"
     @app = app
     @root = app.root
     @index = index
+    @skip_gz = skip_gzip_static
   end
 
   def redirect_slash(env)
@@ -67,6 +68,7 @@ class Autoindex
       # generate the index, show directories first
       dirs = []
       files = []
+      ngz_idx = {} if @skip_gz # used to avoid redundant stat()
       dir.each do |base|
         case base
         when "."
@@ -92,8 +94,22 @@ class Autoindex
         entry << (" " * pad) if pad > 0
         entry << st.mtime.strftime(TFMT)
         entry << sprintf("% 8s", human_size(st))
+        entry = [name, entry]
 
-        (st.directory? ? dirs : files) << [ name,  entry ]
+        if st.directory?
+          dirs << entry
+        elsif ngz_idx
+          ngz_idx[name] = entry
+        else
+          files << entry
+        end
+      end
+
+      if ngz_idx
+        ngz_idx.each do |name, entry|
+          ngz_path = name.dup.gsub!(/\.gz\z/, '')
+          ngz_idx.include?(ngz_path) or files << entry
+        end
       end
 
       dirs.sort! { |(a,_),(b)| a <=> b }.map! { |(_,ent)| ent }