directory_listing 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sinatra/directory_listing/layout.rb +10 -9
- data/lib/sinatra/directory_listing/page.rb +22 -0
- data/lib/sinatra/directory_listing/resource.rb +18 -15
- data/lib/sinatra/directory_listing/version.rb +1 -1
- data/lib/sinatra/directory_listing.rb +45 -39
- data/test/public/stylesheets/styles.css +3 -0
- metadata +3 -2
@@ -4,29 +4,30 @@ module Sinatra
|
|
4
4
|
LAYOUT = <<-EOF
|
5
5
|
<html>
|
6
6
|
<head>
|
7
|
-
<title>Index of <%=
|
7
|
+
<title>Index of <%= page.current_page %>, sorted <%= page.sort_item_display %> <%= page.sort_direction_display %></title>
|
8
8
|
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
|
9
|
-
<%=
|
9
|
+
<%= page.stylesheet %>
|
10
10
|
</head>
|
11
11
|
<body>
|
12
|
-
<h1>Index of <%=
|
13
|
-
<%=
|
12
|
+
<h1>Index of <%= page.current_page %></h1>
|
13
|
+
<%= page.back_to_link %>
|
14
14
|
<br><br>
|
15
15
|
|
16
16
|
<table>
|
17
17
|
<tr>
|
18
|
-
<th><a href='<%=
|
19
|
-
<th><a href='<%=
|
20
|
-
<th><a href='<%=
|
18
|
+
<th><a href='<%= page.file_sort_link %>'>File</a></th>
|
19
|
+
<th><a href='<%= page.mtime_sort_link %>'>Last modified</a></th>
|
20
|
+
<th><a href='<%= page.size_sort_link %>'>Size</a></th>
|
21
21
|
</tr>
|
22
|
-
<%=
|
22
|
+
<%= page.files_html %>
|
23
23
|
</table>
|
24
24
|
|
25
25
|
<br>
|
26
|
-
<a><%=
|
26
|
+
<a><%= page.readme %></a>
|
27
27
|
</body>
|
28
28
|
</html>
|
29
29
|
EOF
|
30
30
|
|
31
31
|
end
|
32
32
|
end
|
33
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Page
|
2
|
+
|
3
|
+
attr_accessor :should_list_invisibles,
|
4
|
+
:last_modified_format,
|
5
|
+
:filename_truncate_length,
|
6
|
+
:stylesheet,
|
7
|
+
:readme,
|
8
|
+
:public_folder,
|
9
|
+
:request_path,
|
10
|
+
:request_params,
|
11
|
+
:current_page,
|
12
|
+
:back_to_link,
|
13
|
+
:files_html,
|
14
|
+
:sort_item,
|
15
|
+
:sort_item_display,
|
16
|
+
:sort_direction,
|
17
|
+
:sort_direction_display,
|
18
|
+
:file_sort_link,
|
19
|
+
:mtime_sort_link,
|
20
|
+
:size_sort_link
|
21
|
+
|
22
|
+
end
|
@@ -1,13 +1,16 @@
|
|
1
1
|
class Resource
|
2
|
-
|
2
|
+
|
3
|
+
require_relative 'page.rb'
|
4
|
+
|
3
5
|
##
|
4
6
|
# Class definition for a single resource to be listed.
|
5
7
|
# Each resource object has accessors for its file name, regular name,
|
6
8
|
# size and mtime, as well as those components wrapped in html.
|
9
|
+
|
10
|
+
attr_accessor :file, :page, :name_html, :mtime, :mtime_html, :size, :size_html
|
7
11
|
|
8
|
-
|
9
|
-
|
10
|
-
def initialize(file)
|
12
|
+
def initialize(file, page)
|
13
|
+
@page = page
|
11
14
|
@file = file
|
12
15
|
@name_html = set_name(file)
|
13
16
|
@mtime, @mtime_html = set_mtime(file)
|
@@ -20,8 +23,8 @@ class Resource
|
|
20
23
|
# Returns the mtime as a Time object so it can be sorted.
|
21
24
|
|
22
25
|
def set_mtime(file)
|
23
|
-
f = File.join(File.join(
|
24
|
-
html = "\t<td>#{File.mtime(f).strftime(
|
26
|
+
f = File.join(File.join(@page.public_folder, URI.unescape(@page.request_path)), file)
|
27
|
+
html = "\t<td>#{File.mtime(f).strftime(@page.last_modified_format)}</td>"
|
25
28
|
return [File.mtime(f), html]
|
26
29
|
end
|
27
30
|
|
@@ -33,7 +36,7 @@ class Resource
|
|
33
36
|
def set_size(file)
|
34
37
|
html = ""
|
35
38
|
size = ''
|
36
|
-
f = File.join(File.join(
|
39
|
+
f = File.join(File.join(@page.public_folder, URI.unescape(@page.request_path)), file)
|
37
40
|
if File.directory?(f)
|
38
41
|
size = 0
|
39
42
|
html = "\t<td>-</td>"
|
@@ -55,32 +58,32 @@ class Resource
|
|
55
58
|
# URI.unescape seems to work best to decode uris.
|
56
59
|
|
57
60
|
file = URI.unescape(file)
|
58
|
-
file_truncated = file.truncate(
|
61
|
+
file_truncated = file.truncate(@page.filename_truncate_length, '...')
|
59
62
|
|
60
63
|
##
|
61
64
|
# If the requested resource is in the root public directory, the link is
|
62
65
|
# just the resource itself without the public directory path as well.
|
63
66
|
|
64
|
-
requested = Pathname.new(URI.unescape(
|
65
|
-
pub_folder = Pathname.new(
|
67
|
+
requested = Pathname.new(URI.unescape(@page.request_path)).cleanpath
|
68
|
+
pub_folder = Pathname.new(@page.public_folder).cleanpath
|
66
69
|
if requested.eql?(pub_folder)
|
67
70
|
link = file
|
68
71
|
else
|
69
|
-
link = File.join(
|
72
|
+
link = File.join(@page.request_path, file)
|
70
73
|
end
|
71
74
|
|
72
75
|
##
|
73
76
|
# Add a class of "dir" to directories and "file" to files.
|
74
77
|
|
75
78
|
html = ""
|
76
|
-
if File.directory?(URI.unescape(File.join(
|
79
|
+
if File.directory?(URI.unescape(File.join(@page.public_folder, link)))
|
77
80
|
html << "\t<td class='dir'>"
|
78
81
|
|
79
82
|
##
|
80
83
|
# Append the sorting information if the current directory is sorted
|
81
84
|
|
82
|
-
if
|
83
|
-
link << "?sortby=" +
|
85
|
+
if @page.request_params["sortby"] && @page.request_params["direction"]
|
86
|
+
link << "?sortby=" + @page.request_params["sortby"] + "&direction=" + @page.request_params["direction"]
|
84
87
|
end
|
85
88
|
else
|
86
89
|
html << "\t<td class='file'>"
|
@@ -104,7 +107,7 @@ class Resource
|
|
104
107
|
|
105
108
|
def wrap
|
106
109
|
html = ""
|
107
|
-
if
|
110
|
+
if @page.should_list_invisibles == true
|
108
111
|
html << "\n\t<tr>
|
109
112
|
#{@name_html}
|
110
113
|
#{@mtime_html}
|
@@ -100,6 +100,7 @@ module Sinatra
|
|
100
100
|
require_relative 'directory_listing/version.rb'
|
101
101
|
require_relative 'directory_listing/layout.rb'
|
102
102
|
require_relative 'directory_listing/resource.rb'
|
103
|
+
require_relative 'directory_listing/page.rb'
|
103
104
|
|
104
105
|
##
|
105
106
|
# Generate the page.
|
@@ -116,26 +117,31 @@ module Sinatra
|
|
116
117
|
:stylesheet => "",
|
117
118
|
:readme => ""
|
118
119
|
}.merge(o)
|
120
|
+
|
121
|
+
##
|
122
|
+
# Create a page object.
|
123
|
+
|
124
|
+
page = Page.new
|
119
125
|
|
120
|
-
|
121
|
-
|
122
|
-
|
126
|
+
page.should_list_invisibles = options[:should_list_invisibles]
|
127
|
+
page.last_modified_format = options[:last_modified_format]
|
128
|
+
page.filename_truncate_length = options[:filename_truncate_length]
|
123
129
|
|
124
130
|
##
|
125
131
|
# Get the public folder, request path, and parameters and
|
126
132
|
# store in globals to be used by the Resource class.
|
127
133
|
|
128
|
-
|
129
|
-
|
130
|
-
|
134
|
+
page.public_folder = settings.public_folder
|
135
|
+
page.request_path = request.path
|
136
|
+
page.request_params = request.params
|
131
137
|
|
132
138
|
##
|
133
139
|
# Start generating strings to be injected into the erb template
|
134
140
|
|
135
|
-
|
136
|
-
|
141
|
+
page.current_page = URI.unescape(request.path)
|
142
|
+
page.readme = options[:readme] if options[:readme]
|
137
143
|
if options[:stylesheet]
|
138
|
-
|
144
|
+
page.stylesheet = "<link rel='stylesheet' type='text/css' href='/#{options[:stylesheet].sub(/^[\/]*/,"")}'>"
|
139
145
|
end
|
140
146
|
|
141
147
|
##
|
@@ -144,12 +150,12 @@ module Sinatra
|
|
144
150
|
|
145
151
|
if URI.unescape(request.path) != "/"
|
146
152
|
back_link = Pathname.new(request.path).parent
|
147
|
-
if
|
148
|
-
back_link = back_link.to_s + "?sortby=" +
|
153
|
+
if page.request_params["sortby"] && page.request_params["direction"]
|
154
|
+
back_link = back_link.to_s + "?sortby=" + page.request_params["sortby"] + "&direction=" + page.request_params["direction"]
|
149
155
|
end
|
150
|
-
|
156
|
+
page.back_to_link = "<a href='#{back_link}'>← Parent directory</a>"
|
151
157
|
else
|
152
|
-
|
158
|
+
page.back_to_link = "<a>Root directory</a>"
|
153
159
|
end
|
154
160
|
|
155
161
|
##
|
@@ -163,9 +169,9 @@ module Sinatra
|
|
163
169
|
##
|
164
170
|
# If the only thing in the array are invisible files, display a "No files" listing.
|
165
171
|
|
166
|
-
|
172
|
+
page.files_html = ""
|
167
173
|
if files == [".", ".."]
|
168
|
-
|
174
|
+
page.files_html << "
|
169
175
|
<tr>
|
170
176
|
<th>No files.</th>
|
171
177
|
<th>-</th>
|
@@ -178,17 +184,17 @@ module Sinatra
|
|
178
184
|
|
179
185
|
resources = Array.new
|
180
186
|
Dir.foreach(File.join(settings.public_folder, URI.unescape(request.path))) do |resource|
|
181
|
-
resources.push(Resource.new(resource))
|
187
|
+
resources.push(Resource.new(resource, page))
|
182
188
|
end
|
183
189
|
|
184
190
|
##
|
185
191
|
# Get the sortby and direction parameters ("file" and "ascending", by
|
186
192
|
# default).
|
187
193
|
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
194
|
+
page.sort_item = "file"
|
195
|
+
page.sort_direction = "ascending"
|
196
|
+
page.sort_item = page.request_params["sortby"] if page.request_params["sortby"]
|
197
|
+
page.sort_direction = page.request_params["direction"] if page.request_params["direction"]
|
192
198
|
|
193
199
|
##
|
194
200
|
# Sort the resources.
|
@@ -196,55 +202,55 @@ module Sinatra
|
|
196
202
|
# or "size"), and whether to sort in order ("ascending") or reverse
|
197
203
|
# ("descending").
|
198
204
|
|
199
|
-
sorted_resources = Resource.sort(resources,
|
205
|
+
sorted_resources = Resource.sort(resources, page.sort_item, page.sort_direction)
|
200
206
|
|
201
207
|
##
|
202
208
|
# Set display variables and sort links based on sorting variables
|
203
209
|
|
204
210
|
file_link_dir = mtime_link_dir = sortby_link_dir = "ascending"
|
205
211
|
|
206
|
-
case
|
212
|
+
case page.sort_item
|
207
213
|
when "file"
|
208
|
-
|
209
|
-
case
|
214
|
+
page.sort_item_display = "alphabetically"
|
215
|
+
case page.sort_direction
|
210
216
|
when "ascending"
|
211
|
-
|
217
|
+
page.sort_direction_display = ""
|
212
218
|
file_link_dir = "descending"
|
213
219
|
when "descending"
|
214
|
-
|
220
|
+
page.sort_direction_display = "reversed"
|
215
221
|
file_link_dir = "ascending"
|
216
222
|
end
|
217
223
|
when "mtime"
|
218
|
-
|
219
|
-
case
|
224
|
+
page.sort_item_display = "by modification date"
|
225
|
+
case page.sort_direction
|
220
226
|
when "ascending"
|
221
|
-
|
227
|
+
page.sort_direction_display = "oldest to newest"
|
222
228
|
mtime_link_dir = "descending"
|
223
229
|
when "descending"
|
224
|
-
|
230
|
+
page.sort_direction_display = "newest to oldest"
|
225
231
|
mtime_link_dir = "ascending"
|
226
232
|
end
|
227
233
|
when "size"
|
228
|
-
|
229
|
-
case
|
234
|
+
page.sort_item_display = "by size"
|
235
|
+
case page.sort_direction
|
230
236
|
when "ascending"
|
231
|
-
|
237
|
+
page.sort_direction_display = "smallest to largest"
|
232
238
|
sortby_link_dir = "descending"
|
233
239
|
when "descending"
|
234
|
-
|
240
|
+
page.sort_direction_display = "largest to smallest"
|
235
241
|
sortby_link_dir = "ascending"
|
236
242
|
end
|
237
243
|
end
|
238
244
|
|
239
|
-
|
240
|
-
|
241
|
-
|
245
|
+
page.file_sort_link = "?sortby=file&direction=#{file_link_dir}"
|
246
|
+
page.mtime_sort_link = "?sortby=mtime&direction=#{mtime_link_dir}"
|
247
|
+
page.size_sort_link = "?sortby=size&direction=#{sortby_link_dir}"
|
242
248
|
|
243
249
|
##
|
244
250
|
# Finally, generate the html from the array of Resources.
|
245
251
|
|
246
252
|
sorted_resources.each do |resource|
|
247
|
-
|
253
|
+
page.files_html << resource.wrap
|
248
254
|
end
|
249
255
|
end
|
250
256
|
|
@@ -252,7 +258,7 @@ module Sinatra
|
|
252
258
|
# Generate and return the complete page from the erb template.
|
253
259
|
|
254
260
|
erb = ERB.new(LAYOUT)
|
255
|
-
erb.result
|
261
|
+
erb.result(binding)
|
256
262
|
end
|
257
263
|
|
258
264
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: directory_listing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
12
|
+
date: 2013-09-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: filesize
|
@@ -123,6 +123,7 @@ files:
|
|
123
123
|
- directory_listing.gemspec
|
124
124
|
- lib/sinatra/directory_listing.rb
|
125
125
|
- lib/sinatra/directory_listing/layout.rb
|
126
|
+
- lib/sinatra/directory_listing/page.rb
|
126
127
|
- lib/sinatra/directory_listing/resource.rb
|
127
128
|
- lib/sinatra/directory_listing/version.rb
|
128
129
|
- test/public/1234.txt
|