diff --git a/Cargo.lock b/Cargo.lock index 21101c1..c4189be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1293,8 +1293,10 @@ dependencies = [ "lazy_static", "linkify", "log", + "mime_guess", "qrcode-generator", "rand", + "rust-embed", "sanitize-filename", "serde", "serde_json", @@ -1702,6 +1704,40 @@ version = "0.6.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" +[[package]] +name = "rust-embed" +version = "6.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "283ffe2f866869428c92e0d61c2f35dfb4355293cdfdc48f49e895c15f1333d1" +dependencies = [ + "rust-embed-impl", + "rust-embed-utils", + "walkdir", +] + +[[package]] +name = "rust-embed-impl" +version = "6.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31ab23d42d71fb9be1b643fe6765d292c5e14d46912d13f3ae2815ca048ea04d" +dependencies = [ + "proc-macro2", + "quote", + "rust-embed-utils", + "syn", + "walkdir", +] + +[[package]] +name = "rust-embed-utils" +version = "7.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1669d81dfabd1b5f8e2856b8bbe146c6192b0ba22162edc738ac0a5de18f054" +dependencies = [ + "sha2", + "walkdir", +] + [[package]] name = "rustc_version" version = "0.4.0" @@ -1820,6 +1856,17 @@ dependencies = [ "digest", ] +[[package]] +name = "sha2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "signal-hook-registry" version = "1.4.0" diff --git a/Cargo.toml b/Cargo.toml index 19a334a..8adc0ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,8 @@ actix-web-httpauth = "0.6.0" lazy_static = "1.4.0" syntect = "5.0" qrcode-generator = "4.1.6" +rust-embed = "6.4.2" +mime_guess = "2.0.4" [profile.release] lto = true diff --git a/README.MD b/README.MD index 610cad0..e9756d8 100644 --- a/README.MD +++ b/README.MD @@ -18,7 +18,7 @@ Or install from Cargo: And run with your custom configuration: -`microbin --port 8080 --highlightsyntax --editable` +`microbin --port 8080 --public-path https://myserver.net --highlightsyntax --editable` ### Features - Is very small diff --git a/src/endpoints/info.rs b/src/endpoints/info.rs index bbdc4fe..7b8bb27 100644 --- a/src/endpoints/info.rs +++ b/src/endpoints/info.rs @@ -33,7 +33,7 @@ pub async fn info(data: web::Data) -> HttpResponse { args: &ARGS, pastas: &pastas, status: &String::from(status), - version_string: &String::from("1.2.0-20221029"), + version_string: &String::from("1.2.0-20221101"), message: &String::from(message), } .render() diff --git a/src/endpoints/static_resources.rs b/src/endpoints/static_resources.rs index 728d617..d314416 100644 --- a/src/endpoints/static_resources.rs +++ b/src/endpoints/static_resources.rs @@ -1,36 +1,63 @@ -use actix_web::{get, web, HttpResponse}; -use askama::Template; -use std::marker::PhantomData; +use actix_web::{web, App, HttpResponse, HttpServer, Responder}; +use mime_guess::from_path; +use rust_embed::RustEmbed; -#[derive(Template)] -#[template(path = "water.css", escape = "none")] -struct WaterCSS<'a> { - _marker: PhantomData<&'a ()>, -} +#[derive(RustEmbed)] +#[folder = "templates/assets/"] +struct Asset; -#[derive(Template)] -#[template(path = "favicon.svg", escape = "none")] -struct Favicon<'a> { - _marker: PhantomData<&'a ()>, -} - -#[get("/static/{resource}")] -pub async fn static_resources(resource_id: web::Path) -> HttpResponse { - match resource_id.into_inner().as_str() { - "water.css" => HttpResponse::Ok().content_type("text/css").body( - WaterCSS { - _marker: Default::default(), - } - .render() - .unwrap(), - ), - "favicon.svg" => HttpResponse::Ok().content_type("image/svg+xml").body( - Favicon { - _marker: Default::default(), - } - .render() - .unwrap(), - ), - _ => HttpResponse::NotFound().content_type("text/html").finish(), +fn handle_embedded_file(path: &str) -> HttpResponse { + match Asset::get(path) { + Some(content) => HttpResponse::Ok() + .content_type(from_path(path).first_or_octet_stream().as_ref()) + .body(content.data.into_owned()), + None => HttpResponse::NotFound().body("404 Not Found"), } } + +#[actix_web::get("/static/{_:.*}")] +async fn static_resources(path: web::Path) -> impl Responder { + handle_embedded_file(path.as_str()) +} + +// #[derive(Template)] +// #[template(path = "water.css", escape = "none")] +// struct WaterCSS<'a> { +// _marker: PhantomData<&'a ()>, +// } + +// // #[derive(Template)] +// // #[template(path = "logo.png", escape = "none")] +// struct LogoPNG<'a> { +// _marker: PhantomData<&'a ()>, +// } + +// #[derive(Template)] +// #[template(path = "favicon.svg", escape = "none")] +// struct Favicon<'a> { +// _marker: PhantomData<&'a ()>, +// } + +// #[get("/static/{resource}")] +// pub async fn static_resources(resource_id: web::Path) -> HttpResponse { +// match resource_id.into_inner().as_str() { +// "water.css" => HttpResponse::Ok().content_type("text/css").body( +// WaterCSS { +// _marker: Default::default(), +// } +// .render() +// .unwrap(), +// ), +// "logo.png" => HttpResponse::Ok() +// .content_type("image/png") +// .body(Ok(EmbedFile::open("templates/logo.png")?)), +// "favicon.svg" => HttpResponse::Ok().content_type("image/svg+xml").body( +// Favicon { +// _marker: Default::default(), +// } +// .render() +// .unwrap(), +// ), +// _ => HttpResponse::NotFound().content_type("text/html").finish(), +// } +// } diff --git a/templates/favicon.svg b/templates/assets/favicon.svg similarity index 100% rename from templates/favicon.svg rename to templates/assets/favicon.svg diff --git a/templates/assets/logo.png b/templates/assets/logo.png new file mode 100644 index 0000000..e4e2a30 Binary files /dev/null and b/templates/assets/logo.png differ diff --git a/templates/water.css b/templates/assets/water.css similarity index 100% rename from templates/water.css rename to templates/assets/water.css diff --git a/templates/footer.html b/templates/footer.html index d705401..ae7f14e 100644 --- a/templates/footer.html +++ b/templates/footer.html @@ -1,17 +1,17 @@ - {% if !args.hide_footer %}

{% if args.footer_text.as_ref().is_none() %} - MicroBin by Dániel Szabó. Fork me on GitHub! - Let's keep the Web compact, accessible and humane! + MicroBin by Dániel Szabó and the FOSS Community. + Let's keep the Web compact, accessible and humane! {%- else %} - {{ args.footer_text.as_ref().unwrap() }} + {{ args.footer_text.as_ref().unwrap() }} {%- endif %}

{%- endif %} + \ No newline at end of file diff --git a/templates/header.html b/templates/header.html index d282c93..8bd4a5b 100644 --- a/templates/header.html +++ b/templates/header.html @@ -10,7 +10,7 @@ - + {% if !args.pure_html %} {% if args.custom_css.as_ref().is_none() %} @@ -48,20 +48,17 @@ {% if !args.hide_logo %} - μ - {%- endif %} - - {% if args.title.as_ref().is_none() %} - MicroBin - {%- else %} - {{ args.title.as_ref().unwrap() }} - {%- endif %} + + + {%- endif %} {% if args.title.as_ref().is_none() %} + MicroBin {%- else %} {{ args.title.as_ref().unwrap() }} {%- endif %} New - Pasta + - Pasta List + List Info diff --git a/templates/index.html b/templates/index.html index 3c92851..1730b86 100644 --- a/templates/index.html +++ b/templates/index.html @@ -164,23 +164,27 @@ -
- - {% if !args.no_file_upload %} -
- -
- + +
+ {% if !args.no_file_upload %} +
+ +
+ +
+ {% endif %} + {% if args.readonly %} + + + {%- else %} + + + + {%- endif %}
- {% endif %} - {% if args.readonly %} - - {%- else %} - - {%- endif %} - -