♻️ Move emoji icons into one place

main
_ 2020-11-29 22:12:25 +00:00
parent 028970cdf0
commit b43a6c2e4b
1 changed files with 22 additions and 23 deletions

View File

@ -51,11 +51,20 @@ use ptth_core::{
}; };
pub mod errors; pub mod errors;
use errors::{ use errors::{
FileServerError, FileServerError,
MarkdownError, MarkdownError,
}; };
mod emoji {
pub const VIDEO: &str = "\u{1f39e}\u{fe0f}";
pub const PICTURE: &str = "\u{1f4f7}";
pub const FILE: &str = "\u{1f4c4}";
pub const FOLDER: &str = "\u{1f4c1}";
pub const ERROR: &str = "\u{26a0}\u{fe0f}";
}
#[derive (Debug, Serialize)] #[derive (Debug, Serialize)]
pub struct ServerInfo { pub struct ServerInfo {
pub server_name: String, pub server_name: String,
@ -157,19 +166,13 @@ fn check_range (range_str: Option <&str>, file_len: u64)
} }
fn get_icon (file_name: &str) -> &'static str { fn get_icon (file_name: &str) -> &'static str {
// Because my editor actually doesn't render these
let video = "\u{1f39e}\u{fe0f}";
let picture = "\u{1f4f7}";
let file = "\u{1f4c4}";
if if
file_name.ends_with (".mp4") || file_name.ends_with (".mp4") ||
file_name.ends_with (".avi") || file_name.ends_with (".avi") ||
file_name.ends_with (".mkv") || file_name.ends_with (".mkv") ||
file_name.ends_with (".webm") file_name.ends_with (".webm")
{ {
video emoji::VIDEO
} }
else if else if
file_name.ends_with (".jpg") || file_name.ends_with (".jpg") ||
@ -177,10 +180,10 @@ fn get_icon (file_name: &str) -> &'static str {
file_name.ends_with (".png") || file_name.ends_with (".png") ||
file_name.ends_with (".bmp") file_name.ends_with (".bmp")
{ {
picture emoji::PICTURE
} }
else { else {
file emoji::FILE
} }
} }
@ -194,7 +197,7 @@ 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 () {
Ok (x) => x, Ok (x) => x,
Err (_) => return TemplateDirEntry { Err (_) => return TemplateDirEntry {
icon: "\u{26a0}\u{fe0f}", icon: emoji::ERROR,
trailing_slash: "", trailing_slash: "",
file_name: "File / directory name is not UTF-8".into (), file_name: "File / directory name is not UTF-8".into (),
encoded_file_name: "".into (), encoded_file_name: "".into (),
@ -206,7 +209,7 @@ async fn read_dir_entry (entry: DirEntry) -> TemplateDirEntry
let metadata = match entry.metadata ().await { let metadata = match entry.metadata ().await {
Ok (x) => x, Ok (x) => x,
Err (_) => return TemplateDirEntry { Err (_) => return TemplateDirEntry {
icon: "\u{26a0}\u{fe0f}", icon: emoji::ERROR,
trailing_slash: "", trailing_slash: "",
file_name: "Could not fetch metadata".into (), file_name: "Could not fetch metadata".into (),
encoded_file_name: "".into (), encoded_file_name: "".into (),
@ -217,10 +220,9 @@ 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 = "\u{1f4c1}";
if t.is_dir () { if t.is_dir () {
("/", icon_folder, "".into ()) ("/", emoji::FOLDER, "".into ())
} }
else { else {
("", get_icon (&file_name), pretty_print_bytes (metadata.len ()).into ()) ("", get_icon (&file_name), pretty_print_bytes (metadata.len ()).into ())
@ -391,14 +393,6 @@ fn serve_error (
resp resp
} }
fn serve_307 (location: String) -> Response {
let mut resp = Response::default ();
resp.status_code (StatusCode::TemporaryRedirect);
resp.header ("location".to_string (), location.into_bytes ());
resp.body_bytes (b"Redirecting...".to_vec ());
resp
}
fn render_markdown (bytes: &[u8], out: &mut String) -> Result <(), MarkdownError> { fn render_markdown (bytes: &[u8], out: &mut String) -> Result <(), MarkdownError> {
use pulldown_cmark::{Parser, Options, html}; use pulldown_cmark::{Parser, Options, html};
@ -660,8 +654,13 @@ pub async fn serve_all (
.header ("content-range".to_string (), format! ("bytes */{}", file_len).into_bytes ()); .header ("content-range".to_string (), format! ("bytes */{}", file_len).into_bytes ());
resp resp
}, },
Redirect (location) => serve_307 (location), Redirect (location) => {
let mut resp = Response::default ();
resp.status_code (StatusCode::TemporaryRedirect);
resp.header ("location".to_string (), location.into_bytes ());
resp.body_bytes (b"Redirecting...".to_vec ());
resp
},
Root => serve_root (handlebars, server_info).await?, Root => serve_root (handlebars, server_info).await?,
ServeDir (ServeDirParams { ServeDir (ServeDirParams {
path, path,