Sha256: e66f5e456265fb8423270cf0b0508c27de99d88a341b5503962509c8f07a1741

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

require 'yarn/logging'

module Yarn
  class DirectoryLister

    include Logging

    def self.list(path)
      response = []
      response << <<-EOS
<html><head><title>Directory Listing</title></head><body><h1>Directory Listing</h1><table cellpadding='4'><thead><td><b>Filename</b></td><td><b>Size</b></></thead><tbody>
        EOS

      real_path = File.join(".",path)
      dir = Dir.entries(real_path).sort

      dir.each do |entry|
        size = ""
        if entry == "."
          url = ""
          name = "."
        elsif entry == ".."
          next if ["/", ""].include?(path)
          path_arr = path.split("/")
          if path_arr.size == 1
            url = ""
          else
            url = path_arr[0..path_arr.size-2].join("/")
          end
          name = ".."
        elsif File.exist?(File.join(real_path,entry))
          url = ["/", ""].include?(path) ? entry : "#{path}/#{entry}"
          name = entry
          entry_path = "#{real_path}/#{entry}"
          unless File.directory?(entry_path)
            size = format_size File.stat("#{real_path}/#{entry}").size
          end
        else
          next
        end

        url = "/#{url}"

        response << "<tr><td><a href=\"#{url}\">#{name}</a></td><td>#{size}</td></tr>"
      end

      response << ["</tbody>", "</table", "</body>", "</html>"]

      return response
    end

    def self.format_size(size)
      count = 0
      while  size >= 1024 and count < 4
        size /= 1024.0
        count += 1
      end
      format("%.2f",size) + %w(B KB MB GB TB)[count]
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yarn-0.1.0 lib/yarn/directory_lister.rb
yarn-0.0.9 lib/yarn/directory_lister.rb
yarn-0.0.2 lib/yarn/directory_lister.rb
yarn-0.0.1 lib/yarn/directory_lister.rb