Skip to content

Commit

Permalink
Improve error
Browse files Browse the repository at this point in the history
  • Loading branch information
achoum committed Aug 14, 2024
1 parent d0c1ebc commit 25348f1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ path = "src/bin/ui.rs"
[dependencies]
aes = "0.8.4"
aes-gcm = "0.10.3"
anyhow = "1.0.86"
base64 = "0.22.1"
clap = { version = "4.5.13", features = ["derive"] }
handlebars = "6.0.0"
Expand Down
24 changes: 18 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use aes_gcm::{
aead::{Aead, AeadCore, KeyInit},
Aes256Gcm, Key,
};
use anyhow::Context;
use base64::{self, Engine};
use handlebars::Handlebars;
use hmac::Hmac;
Expand All @@ -26,11 +27,13 @@ fn embede_html_content(src: &str, workdir: &Path) -> Result<String, Box<dyn Erro
for element in document.select(&selector) {
if let Some(src) = element.value().attr("src") {
if Url::parse(src).is_ok() {
eprintln!("Don't embbed remote image: {src}");
eprintln!("Skip remote image: {src}");
continue;
}
eprintln!("Embbed local image: {src}");
let element_data = std::fs::read(workdir.join(src))?;
let path = workdir.join(src);
let element_data =
std::fs::read(&path).context(format!("Failed to open {:?}", &path))?;
let mime_type =
infer::get(&element_data).map_or("image/png", |info| info.mime_type());
let base64_img = base64::engine::general_purpose::STANDARD.encode(&element_data);
Expand All @@ -42,8 +45,15 @@ fn embede_html_content(src: &str, workdir: &Path) -> Result<String, Box<dyn Erro

{
let selector = Selector::parse(r#"script[src]"#)?;
if document.select(&selector).next().is_some() {
eprintln!("Script found. Note that scripts are not embedded.");
//if document.select(&selector).next().is_some() {
for element in document.select(&selector) {
if let Some(src) = element.value().attr("src") {
if Url::parse(src).is_ok() {
eprintln!("Don't embbed remote image: {src}");
continue;
}
eprintln!("Local script found: {src}. Note that scripts are not embedded.");
}
}
}

Expand All @@ -52,11 +62,13 @@ fn embede_html_content(src: &str, workdir: &Path) -> Result<String, Box<dyn Erro
for element in document.select(&selector) {
if let Some(href) = element.value().attr("href") {
if Url::parse(href).is_ok() {
eprintln!("Don't embbed remote stylesheet: {href}");
eprintln!("Skip remote stylesheet: {href}");
continue;
}
eprintln!("Embbed local stylesheet: {href}");
let element_data = std::fs::read_to_string(workdir.join(href))?;
let path = workdir.join(href);
let element_data = std::fs::read_to_string(&path)
.context(format!("Failed to open {:?}", &path))?;
let new_tag = format!("<style>{}</style>", element_data);
src_content = src_content.replacen(&element.html(), &new_tag, 1);
}
Expand Down

0 comments on commit 25348f1

Please sign in to comment.