1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
use actix_web::{fs, HttpRequest, HttpResponse, Result};
use bytesize::ByteSize;
use htmlescape::encode_minimal as escape_html_entity;
use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET};
use std::cmp::Ordering;
use std::fmt::Write as FmtWrite;
use std::io;
use std::path::Path;
use std::str::FromStr;
use crate::config;
#[derive(Clone, Copy, Debug)]
pub enum SortingMethods {
Natural,
Alpha,
DirsFirst,
}
#[derive(PartialEq)]
enum EntryType {
Directory,
File,
}
impl PartialOrd for EntryType {
fn partial_cmp(&self, other: &EntryType) -> Option<Ordering> {
match (self, other) {
(EntryType::Directory, EntryType::File) => Some(Ordering::Less),
(EntryType::File, EntryType::Directory) => Some(Ordering::Greater),
_ => Some(Ordering::Equal),
}
}
}
struct Entry {
name: String,
entry_type: EntryType,
link: String,
size: Option<bytesize::ByteSize>,
}
impl Entry {
fn new(
name: String,
entry_type: EntryType,
link: String,
size: Option<bytesize::ByteSize>,
) -> Self {
Entry {
name,
entry_type,
link,
size,
}
}
}
impl FromStr for SortingMethods {
type Err = ();
fn from_str(s: &str) -> Result<SortingMethods, ()> {
match s {
"natural" => Ok(SortingMethods::Natural),
"alpha" => Ok(SortingMethods::Alpha),
"dirsfirst" => Ok(SortingMethods::DirsFirst),
_ => Err(()),
}
}
}
pub fn file_handler(req: &HttpRequest<config::MiniserveConfig>) -> Result<fs::NamedFile> {
let path = &req.state().path;
Ok(fs::NamedFile::open(path)?)
}
// ↓ Adapted from https://docs.rs/actix-web/0.7.13/src/actix_web/fs.rs.html#564
pub fn directory_listing<S>(
dir: &fs::Directory,
req: &HttpRequest<S>,
skip_symlinks: bool,
random_route: Option<String>,
sort_method: SortingMethods,
reverse_sort: bool,
) -> Result<HttpResponse, io::Error> {
let index_of = format!("Index of {}", req.path());
let mut body = String::new();
let base = Path::new(req.path());
let random_route = format!("/{}", random_route.unwrap_or_default());
if let Some(parent) = base.parent() {
if req.path() != random_route {
let _ = write!(
body,
"<tr><td><a class=\"root\" href=\"{}\">..</a></td><td></td></tr>",
parent.display()
);
}
}
let mut entries: Vec<Entry> = Vec::new();
for entry in dir.path.read_dir()? {
if dir.is_visible(&entry) {
let entry = entry.unwrap();
let p = match entry.path().strip_prefix(&dir.path) {
Ok(p) => base.join(p),
Err(_) => continue,
};
// show file url as relative to static path
let file_url =
utf8_percent_encode(&p.to_string_lossy(), DEFAULT_ENCODE_SET).to_string();
// " -- " & -- & ' -- ' < -- < > -- >
let file_name = escape_html_entity(&entry.file_name().to_string_lossy());
// if file is a directory, add '/' to the end of the name
if let Ok(metadata) = entry.metadata() {
if skip_symlinks && metadata.file_type().is_symlink() {
continue;
}
if metadata.is_dir() {
entries.push(Entry::new(file_name, EntryType::Directory, file_url, None));
} else {
entries.push(Entry::new(
file_name,
EntryType::File,
file_url,
Some(ByteSize::b(metadata.len())),
));
}
} else {
continue;
}
}
}
match sort_method {
SortingMethods::Natural => entries
.sort_by(|e1, e2| alphanumeric_sort::compare_str(e1.name.clone(), e2.name.clone())),
SortingMethods::Alpha => {
entries.sort_by(|e1, e2| e1.entry_type.partial_cmp(&e2.entry_type).unwrap());
entries.sort_by_key(|e| e.name.clone())
}
SortingMethods::DirsFirst => {
entries.sort_by_key(|e| e.name.clone());
entries.sort_by(|e1, e2| e1.entry_type.partial_cmp(&e2.entry_type).unwrap());
}
};
if reverse_sort {
entries.reverse();
}
for entry in entries {
match entry.entry_type {
EntryType::Directory => {
let _ = write!(
body,
"<tr><td><a class=\"directory\" href=\"{}\">{}/</a></td><td></td></tr>",
entry.link, entry.name
);
}
EntryType::File => {
let _ = write!(
body,
"<tr><td><a class=\"file\" href=\"{}\">{}</a></td><td>{}</td></tr>",
entry.link,
entry.name,
entry.size.unwrap()
);
}
}
}
let html = format!(
"<html>\
<head>\
<title>{}</title>\
<style>\
body {{\
margin: 0;\
font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto,\"Helvetica Neue\", Helvetica, Arial, sans-serif;\
font-weight: 300;\
color: #444444;\
padding: 0.125rem;\
}}\
table {{\
width: 100%;\
background: white;\
border: 0;\
table-layout: auto;\
}}\
table thead {{\
background: #efefef;\
}}\
table tr th,\
table tr td {{\
padding: 0.5625rem 0.625rem;\
font-size: 0.875rem;\
color: #777c82;\
text-align: left;\
line-height: 1.125rem;\
}}\
table thead tr th {{\
padding: 0.5rem 0.625rem 0.625rem;\
font-weight: bold;\
color: #444444;\
}}\
table tr:nth-child(even) {{\
background: #f6f6f6;\
}}\
a {{\
text-decoration: none;\
color: #3498db;\
}}\
a.root, a.root:visited {{\
font-weight: bold;\
color: #777c82;\
}}\
a.directory {{\
font-weight: bold;\
}}\
a:hover {{\
text-decoration: underline;\
}}\
a:visited {{\
color: #8e44ad;\
}}\
@media (max-width: 600px) {{\
h1 {{\
font-size: 1.375em;\
}}\
}}\
@media (max-width: 400px) {{\
h1 {{\
font-size: 1.375em;\
}}\
}}\
</style>\
</head>\
<body><h1>{}</h1>\
<table>\
<thead><th>Name</th><th>Size</th></thead>\
<tbody>\
{}\
</tbody></table></body>\n</html>",
index_of, index_of, body
);
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(html))
}
|