Remove html merge
This commit is contained in:
parent
f481105fd9
commit
3f366c4e31
9 changed files with 50 additions and 154 deletions
48
src/main.rs
48
src/main.rs
|
@ -1,6 +1,6 @@
|
|||
#![windows_subsystem = "windows"]
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use clap::{Arg, Command};
|
||||
use fast_image_resize as fr;
|
||||
use image::{io::Reader as ImageReader, DynamicImage};
|
||||
|
@ -68,9 +68,19 @@ fn main() -> Result<()> {
|
|||
.ok_or_else(|| anyhow!("No file"))?
|
||||
};
|
||||
|
||||
if !img_path.exists() {
|
||||
MessageDialog::new()
|
||||
.set_title("Image error")
|
||||
.set_text(&format!("Image `{}` does not exist", img_path.display()))
|
||||
.set_type(native_dialog::MessageType::Error)
|
||||
.show_alert()?;
|
||||
bail!("File `{}` does not exist !", img_path.display());
|
||||
}
|
||||
|
||||
info!("Open `{}`", img_path.display());
|
||||
let (width, height) = image::image_dimensions(&img_path)?;
|
||||
const MAX_IMG_SIZE: u32 = 20_000_000;
|
||||
info!("Image size {} * {}", width, height);
|
||||
let img_data_path = if height * width > MAX_IMG_SIZE {
|
||||
// @todo utilisation de rfd au lieu de message_dialog
|
||||
// @todo Option pas de resize / taille en ligne de commande
|
||||
|
@ -111,9 +121,8 @@ fn main() -> Result<()> {
|
|||
img_path
|
||||
};
|
||||
|
||||
info!("Generate HTML");
|
||||
let html = std::str::from_utf8(Embed::get("index.html").unwrap().data.as_ref())?.to_string();
|
||||
run_server(html, img_data_path);
|
||||
info!("Run server");
|
||||
run_server(img_data_path);
|
||||
|
||||
info!("Create webview");
|
||||
let event_loop = EventLoop::new();
|
||||
|
@ -143,7 +152,7 @@ fn main() -> Result<()> {
|
|||
});
|
||||
}
|
||||
|
||||
fn run_server(html: String, img_path: PathBuf) -> JoinHandle<()> {
|
||||
fn run_server(img_path: PathBuf) -> JoinHandle<()> {
|
||||
std::thread::spawn(|| {
|
||||
info!("Create runtime");
|
||||
let async_runtime = tokio::runtime::Builder::new_current_thread()
|
||||
|
@ -151,13 +160,38 @@ fn run_server(html: String, img_path: PathBuf) -> JoinHandle<()> {
|
|||
.build()
|
||||
.unwrap();
|
||||
info!("Create response");
|
||||
let hello = warp::path!("index.html").map(move || warp::reply::html(html.clone()));
|
||||
let index = warp::path("index.html").map(|| {
|
||||
info!("Request `index.html`");
|
||||
let datas = get_file_data("index.html");
|
||||
warp::reply::html(datas)
|
||||
});
|
||||
let css = warp::path!("css" / String).map(move |val: String| {
|
||||
info!("Request css `{}`", &val);
|
||||
let datas = get_file_data(&format!("css/{}", val));
|
||||
warp::http::Response::builder().body(datas)
|
||||
});
|
||||
let js = warp::path!("js" / String).map(move |val: String| {
|
||||
info!("Request js `{}`", &val);
|
||||
let datas = get_file_data(&format!("js/{}", val));
|
||||
warp::http::Response::builder().body(datas)
|
||||
});
|
||||
let img = warp::path!("img").map(move || std::fs::read(&img_path).unwrap());
|
||||
info!("Launch webserver");
|
||||
async_runtime.block_on(warp::serve(hello.or(img)).run(([127, 0, 0, 1], 62371)));
|
||||
async_runtime
|
||||
.block_on(warp::serve(img.or(index).or(css).or(js)).run(([127, 0, 0, 1], 62371)));
|
||||
})
|
||||
}
|
||||
|
||||
fn get_file_data(filename: &str) -> String {
|
||||
if let Some(file) = Embed::get(filename) {
|
||||
std::str::from_utf8(file.data.as_ref())
|
||||
.unwrap_or("")
|
||||
.to_string()
|
||||
} else {
|
||||
"".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// Fast resize
|
||||
pub fn fast_image_resize(
|
||||
img: &DynamicImage,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue