♻️ Clippy pass
parent
bfe07fddc3
commit
a3e76cf120
|
@ -70,7 +70,7 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
|
||||||
ptth_req.method,
|
ptth_req.method,
|
||||||
&ptth_req.uri,
|
&ptth_req.uri,
|
||||||
&ptth_req.headers,
|
&ptth_req.headers,
|
||||||
state.hidden_path.as_ref ().map (|p| p.as_path ())
|
state.hidden_path.as_deref ()
|
||||||
).await;
|
).await;
|
||||||
|
|
||||||
let mut resp = Response::builder ()
|
let mut resp = Response::builder ()
|
||||||
|
@ -116,7 +116,7 @@ async fn main () -> Result <(), Box <dyn Error>> {
|
||||||
},
|
},
|
||||||
handlebars,
|
handlebars,
|
||||||
server_info: crate::file_server::ServerInfo {
|
server_info: crate::file_server::ServerInfo {
|
||||||
server_name: config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()).clone (),
|
server_name: config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()),
|
||||||
},
|
},
|
||||||
hidden_path: Some (path),
|
hidden_path: Some (path),
|
||||||
});
|
});
|
||||||
|
|
|
@ -30,7 +30,7 @@ pub fn load_public <
|
||||||
> (
|
> (
|
||||||
config_file_path: P
|
config_file_path: P
|
||||||
) -> T {
|
) -> T {
|
||||||
let mut f = File::open (&config_file_path).unwrap_or_else (|_| panic! ("Can't open {:?}", config_file_path));
|
let f = File::open (&config_file_path).unwrap_or_else (|_| panic! ("Can't open {:?}", config_file_path));
|
||||||
load_inner (f)
|
load_inner (f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ pub fn load <
|
||||||
) -> T {
|
) -> T {
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
|
||||||
let mut f = File::open (&config_file_path).unwrap_or_else (|_| panic! ("Can't open {:?}", config_file_path));
|
let f = File::open (&config_file_path).unwrap_or_else (|_| panic! ("Can't open {:?}", config_file_path));
|
||||||
|
|
||||||
let mode = f.metadata ().unwrap ().permissions ().mode ();
|
let mode = f.metadata ().unwrap ().permissions ().mode ();
|
||||||
assert_eq! (mode, CONFIG_PERMISSIONS_MODE, "Config file has bad permissions mode, it should be octal 0600");
|
assert_eq! (mode, CONFIG_PERMISSIONS_MODE, "Config file has bad permissions mode, it should be octal 0600");
|
||||||
|
|
|
@ -146,6 +146,34 @@ fn check_range (range_str: Option <&str>, file_len: u64)
|
||||||
PartialContent (start..end)
|
PartialContent (start..end)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_icon (file_name: &str) -> &'static str {
|
||||||
|
// Because my editor actually doesn't render these
|
||||||
|
|
||||||
|
let video = "🎞️";
|
||||||
|
let picture = "📷";
|
||||||
|
let file = "📄";
|
||||||
|
|
||||||
|
if
|
||||||
|
file_name.ends_with (".mp4") ||
|
||||||
|
file_name.ends_with (".avi") ||
|
||||||
|
file_name.ends_with (".mkv") ||
|
||||||
|
file_name.ends_with (".webm")
|
||||||
|
{
|
||||||
|
video
|
||||||
|
}
|
||||||
|
else if
|
||||||
|
file_name.ends_with (".jpg") ||
|
||||||
|
file_name.ends_with (".jpeg") ||
|
||||||
|
file_name.ends_with (".png") ||
|
||||||
|
file_name.ends_with (".bmp")
|
||||||
|
{
|
||||||
|
picture
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn read_dir_entry (entry: DirEntry) -> TemplateDirEntry
|
async fn read_dir_entry (entry: DirEntry) -> TemplateDirEntry
|
||||||
{
|
{
|
||||||
let file_name = match entry.file_name ().into_string () {
|
let file_name = match entry.file_name ().into_string () {
|
||||||
|
@ -174,37 +202,13 @@ async fn read_dir_entry (entry: DirEntry) -> TemplateDirEntry
|
||||||
|
|
||||||
let (trailing_slash, icon, size) = {
|
let (trailing_slash, icon, size) = {
|
||||||
let t = metadata.file_type ();
|
let t = metadata.file_type ();
|
||||||
|
let icon_folder = "📁";
|
||||||
|
|
||||||
if t.is_dir () {
|
if t.is_dir () {
|
||||||
("/", "📁", "".into ())
|
("/", icon_folder, "".into ())
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let icon = if file_name.ends_with (".mp4") {
|
("", get_icon (&file_name), pretty_print_bytes (metadata.len ()).into ())
|
||||||
"🎞️"
|
|
||||||
}
|
|
||||||
else if file_name.ends_with (".avi") {
|
|
||||||
"🎞️"
|
|
||||||
}
|
|
||||||
else if file_name.ends_with (".mkv") {
|
|
||||||
"🎞️"
|
|
||||||
}
|
|
||||||
else if file_name.ends_with (".jpg") {
|
|
||||||
"📷"
|
|
||||||
}
|
|
||||||
else if file_name.ends_with (".jpeg") {
|
|
||||||
"📷"
|
|
||||||
}
|
|
||||||
else if file_name.ends_with (".png") {
|
|
||||||
"📷"
|
|
||||||
}
|
|
||||||
else if file_name.ends_with (".bmp") {
|
|
||||||
"📷"
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
"📄"
|
|
||||||
};
|
|
||||||
|
|
||||||
("", icon, pretty_print_bytes (metadata.len ()).into ())
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -385,7 +389,7 @@ fn render_markdown (bytes: &[u8], out: &mut String) -> Result <(), MarkdownError
|
||||||
use pulldown_cmark::{Parser, Options, html};
|
use pulldown_cmark::{Parser, Options, html};
|
||||||
|
|
||||||
let markdown_input = match std::str::from_utf8 (bytes) {
|
let markdown_input = match std::str::from_utf8 (bytes) {
|
||||||
Err (_) => return Err (MarkdownError::FileIsNotUtf8),
|
Err (_) => return Err (MarkdownError::NotUtf8),
|
||||||
Ok (x) => x,
|
Ok (x) => x,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -430,9 +434,9 @@ struct ServeFileParams {
|
||||||
|
|
||||||
#[derive (Debug, PartialEq)]
|
#[derive (Debug, PartialEq)]
|
||||||
enum MarkdownError {
|
enum MarkdownError {
|
||||||
FileIsTooBig,
|
TooBig,
|
||||||
FileIsNotMarkdown,
|
// NotMarkdown,
|
||||||
FileIsNotUtf8,
|
NotUtf8,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Debug, PartialEq)]
|
#[derive (Debug, PartialEq)]
|
||||||
|
@ -511,7 +515,7 @@ async fn internal_serve_all (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let has_trailing_slash = path_s.is_empty () || path_s.ends_with ("/");
|
let has_trailing_slash = path_s.is_empty () || path_s.ends_with ('/');
|
||||||
|
|
||||||
if let Ok (dir) = read_dir (&full_path).await {
|
if let Ok (dir) = read_dir (&full_path).await {
|
||||||
if ! has_trailing_slash {
|
if ! has_trailing_slash {
|
||||||
|
@ -548,7 +552,7 @@ async fn internal_serve_all (
|
||||||
if uri.query () == Some ("as_markdown") {
|
if uri.query () == Some ("as_markdown") {
|
||||||
const MAX_BUF_SIZE: u32 = 1_000_000;
|
const MAX_BUF_SIZE: u32 = 1_000_000;
|
||||||
if file_len > MAX_BUF_SIZE.try_into ().unwrap () {
|
if file_len > MAX_BUF_SIZE.try_into ().unwrap () {
|
||||||
MarkdownErr (MarkdownError::FileIsTooBig)
|
MarkdownErr (MarkdownError::TooBig)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let mut buffer = vec! [0u8; MAX_BUF_SIZE.try_into ().unwrap ()];
|
let mut buffer = vec! [0u8; MAX_BUF_SIZE.try_into ().unwrap ()];
|
||||||
|
@ -632,9 +636,9 @@ pub async fn serve_all (
|
||||||
range_requested,
|
range_requested,
|
||||||
}) => serve_file (file.into_inner (), send_body, range, range_requested).await,
|
}) => serve_file (file.into_inner (), send_body, range, range_requested).await,
|
||||||
MarkdownErr (e) => match e {
|
MarkdownErr (e) => match e {
|
||||||
MarkdownError::FileIsTooBig => serve_error (StatusCode::InternalServerError, "File is too big to preview as Markdown"),
|
MarkdownError::TooBig => serve_error (StatusCode::InternalServerError, "File is too big to preview as Markdown"),
|
||||||
MarkdownError::FileIsNotMarkdown => serve_error (StatusCode::BadRequest, "File is not Markdown"),
|
//MarkdownError::NotMarkdown => serve_error (StatusCode::BadRequest, "File is not Markdown"),
|
||||||
MarkdownError::FileIsNotUtf8 => serve_error (StatusCode::BadRequest, "File is not UTF-8"),
|
MarkdownError::NotUtf8 => serve_error (StatusCode::BadRequest, "File is not UTF-8"),
|
||||||
},
|
},
|
||||||
MarkdownPreview (s) => serve_html (s),
|
MarkdownPreview (s) => serve_html (s),
|
||||||
}
|
}
|
||||||
|
@ -695,6 +699,29 @@ mod tests {
|
||||||
StatusCode,
|
StatusCode,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn icons () {
|
||||||
|
let video = "🎞️";
|
||||||
|
let picture = "📷";
|
||||||
|
let file = "📄";
|
||||||
|
|
||||||
|
for (input, expected) in vec! [
|
||||||
|
("copying_is_not_theft.mp4", video),
|
||||||
|
("copying_is_not_theft.avi", video),
|
||||||
|
("copying_is_not_theft.mkv", video),
|
||||||
|
("copying_is_not_theft.webm", video),
|
||||||
|
("lolcats.jpg", picture),
|
||||||
|
("lolcats.jpeg", picture),
|
||||||
|
("lolcats.png", picture),
|
||||||
|
("lolcats.bmp", picture),
|
||||||
|
("ptth.log", file),
|
||||||
|
("README.md", file),
|
||||||
|
("todo.txt", file),
|
||||||
|
].into_iter () {
|
||||||
|
assert_eq! (super::get_icon (input), expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_range_header () {
|
fn parse_range_header () {
|
||||||
for (input, expected) in vec! [
|
for (input, expected) in vec! [
|
||||||
|
|
|
@ -70,7 +70,7 @@ async fn handle_req_resp <'a> (
|
||||||
parts.method,
|
parts.method,
|
||||||
&parts.uri,
|
&parts.uri,
|
||||||
&parts.headers,
|
&parts.headers,
|
||||||
state.hidden_path.as_ref ().map (|p| p.as_path ())
|
state.hidden_path.as_deref ()
|
||||||
).await;
|
).await;
|
||||||
|
|
||||||
let mut resp_req = state.client
|
let mut resp_req = state.client
|
||||||
|
@ -139,7 +139,7 @@ pub async fn run_server (
|
||||||
)
|
)
|
||||||
-> Result <(), Box <dyn Error>>
|
-> Result <(), Box <dyn Error>>
|
||||||
{
|
{
|
||||||
let asset_root = asset_root.unwrap_or_else (|| PathBuf::new ());
|
let asset_root = asset_root.unwrap_or_else (PathBuf::new);
|
||||||
|
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue