From 091f7bcbeb43d4695ec84b906a162d940195add3 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 20 Sep 2014 10:13:14 +0000 Subject: extras/autoindex: integrate with TryGzipStatic 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. --- extras/autoindex.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'extras') 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 = %{%s} 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 } -- cgit v1.2.3-24-ge0c7