Class: Rack::Git::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/git/directory.rb

Constant Summary collapse

DIR_FILE =
"<tr><td class='name'><a href='%s'>%s</a></td><td class='size'>%s</td><td class='type'>%s</td><td class='mtime'>%s</td></tr>"
DIR_PAGE =
<<-PAGE
<html><head>
<title>%s</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type='text/css'>
table { width:100%%; }
.name { text-align:left; }
.size, .mtime { text-align:right; }
.type { width:11em; }
.mtime { width:15em; }
</style>
</head><body>
<h1>%s</h1>
<hr />
<table>
<tr>
<th class='name'>Name</th>
<th class='size'>Size</th>
<th class='type'>Type</th>
<th class='mtime'>Last Modified</th>
</tr>
%s
</table>
<hr />
</body></html>
PAGE
F =
::File
FILESIZE_FORMAT =

Stolen from Ramaze

[
  ['%.1fT', 1 << 40],
  ['%.1fG', 1 << 30],
  ['%.1fM', 1 << 20],
  ['%.1fK', 1 << 10],
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, app = nil) ⇒ Directory

Returns a new instance of Directory.



47
48
49
50
51
# File 'lib/rack/git/directory.rb', line 47

def initialize(root, app=nil)
  @root = root
  @app = app || Rack::Git::File.new(@root)
  @repo = Grit::Repo.new(@root)
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



44
45
46
# File 'lib/rack/git/directory.rb', line 44

def files
  @files
end

#pathObject

Returns the value of attribute path.



45
46
47
# File 'lib/rack/git/directory.rb', line 45

def path
  @path
end

#rootObject

Returns the value of attribute root.



45
46
47
# File 'lib/rack/git/directory.rb', line 45

def root
  @root
end

Instance Method Details

#_call(env) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rack/git/directory.rb', line 59

def _call(env)
  @env = env
  @branch_name = env['PATH_PREFIX'] || "master"
  @script_name = env['SCRIPT_NAME']
  @path_info = Utils.unescape(env['PATH_INFO']).sub(/\/$/,"").sub(/^\//,"")

  if forbidden = check_forbidden
    forbidden
  else
    list_path
  end
end

#call(env) ⇒ Object



53
54
55
# File 'lib/rack/git/directory.rb', line 53

def call(env)
  dup._call(env)
end

#check_forbiddenObject



72
73
74
75
76
77
78
79
80
# File 'lib/rack/git/directory.rb', line 72

def check_forbidden
  return unless @path_info.include? ".."

  body = "Forbidden\n"
  size = Rack::Utils.bytesize(body)
  return [403, {"Content-Type" => "text/plain",
      "Content-Length" => size.to_s,
      "X-Cascade" => "pass"}, [body]]
end

#eachObject



135
136
137
138
139
140
# File 'lib/rack/git/directory.rb', line 135

def each
  show_path = @path_info.sub(/^#{@root}/,'')
  files = @files.map{|f| DIR_FILE % f }*"\n"
  page  = DIR_PAGE % [ show_path, show_path , files ]
  page.each_line{|l| yield l }
end

#entity_not_foundObject



127
128
129
130
131
132
133
# File 'lib/rack/git/directory.rb', line 127

def entity_not_found
  body = "Entity not found: #{@path_info}\n"
  size = Rack::Utils.bytesize(body)
  return [404, {"Content-Type" => "text/plain",
      "Content-Length" => size.to_s,
      "X-Cascade" => "pass"}, [body]]
end

#filesize_format(int) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/rack/git/directory.rb', line 151

def filesize_format(int)
  FILESIZE_FORMAT.each do |format, size|
    return format % (int.to_f / size) if int >= size
  end

  int.to_s + 'B'
end

#list_directoryObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rack/git/directory.rb', line 82

def list_directory
  @files = [['../','Parent Directory','','','']]

  #context.list(@path ,"HEAD") do |name,dirent,lock,abs_path|
  #  basename = F.basename(name)
  #  ext = F.extname(name)
  #
  #  url       = F.join(@script_name, @path_info, basename)
  #  size      = dirent.size
  #  type      = dirent.directory? ? 'directory' : Mime.mime_type(ext)
  #  size      = dirent.directory? ? '-' : filesize_format(size)
  #  mtime     = dirent.time2.httpdate
  #  url      << '/'  if dirent.directory?
  #  basename << '/'  if dirent.directory?
  #
  #  @files << [ url, basename, size, type, mtime ]
  #end
  return [ 200, {'Content-Type'=>'text/html; charset=utf-8'}, self ]
end

#list_pathObject

TODO: add correct response if not readable, not sure if 404 is the best

option


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rack/git/directory.rb', line 110

def list_path

  history = @repo.log(@branch_name, @path_info, :max_count => 1, :skip => 0)
  if history[0] == nil
    return entity_not_found
  end
  entry   = history[0].tree / @path_info

  if entry.is_a?(Grit::Blob)
    @app.call(@env)
  elsif  entry.is_a?(Grit::Tree)
    list_directory
  else
    entity_not_found
  end
end