|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +#[cfg(target_os = "android")] |
| 4 | +use dioxus::mobile::use_asset_handler; |
| 5 | + |
| 6 | +#[cfg(not(target_os = "android"))] |
| 7 | +use dioxus::desktop::use_asset_handler; |
| 8 | + |
| 9 | +#[doc(hidden)] |
| 10 | +macro_rules! response { |
| 11 | + ($status:expr) => {{ |
| 12 | + #[cfg(target_os = "android")] |
| 13 | + use dioxus::mobile::wry::http::Response; |
| 14 | + |
| 15 | + #[cfg(not(target_os = "android"))] |
| 16 | + use dioxus::desktop::wry::http::Response; |
| 17 | + |
| 18 | + Response::builder() |
| 19 | + .status($status) |
| 20 | + .body(Vec::new()) |
| 21 | + .unwrap() |
| 22 | + }}; |
| 23 | + ($image_src:expr, $image_bytes:expr) => {{ |
| 24 | + #[cfg(target_os = "android")] |
| 25 | + use dioxus::mobile::wry::http::Response; |
| 26 | + |
| 27 | + #[cfg(not(target_os = "android"))] |
| 28 | + use dioxus::desktop::wry::http::Response; |
| 29 | + |
| 30 | + Response::builder() |
| 31 | + .header("Content-Type", $image_src.format().to_mime_type()) |
| 32 | + .body($image_bytes) |
| 33 | + .unwrap() |
| 34 | + }}; |
| 35 | +} |
| 36 | + |
| 37 | +fn get_value<'a>(query: &'a str, key: &str) -> Option<&'a str> { |
| 38 | + let start_pos = match query.find(&format!("{}=", key)) { |
| 39 | + Some(pos) => pos + key.len() + 1, |
| 40 | + None => return None, |
| 41 | + }; |
| 42 | + let end_pos = match query[start_pos..].find("&") { |
| 43 | + Some(pos) => start_pos + pos, |
| 44 | + None => start_pos + query[start_pos..].len(), |
| 45 | + }; |
| 46 | + Some(&query[start_pos..end_pos]) |
| 47 | +} |
| 48 | + |
| 49 | +fn get_decoded(query: &str, key: &str) -> Option<String> { |
| 50 | + get_value(query, key) |
| 51 | + .and_then(|value| urlencoding::decode(value).ok()) |
| 52 | + .map(|value| value.to_string()) |
| 53 | +} |
| 54 | + |
| 55 | +fn get_int<T: FromStr>(query: &str, key: &str) -> Option<T> { |
| 56 | + get_value(query, key).and_then(|value| value.parse::<T>().ok()) |
| 57 | +} |
| 58 | + |
| 59 | +pub fn use_gallery_handler() { |
| 60 | + use_asset_handler("gallery", move |request, responder| { |
| 61 | + if request.method() != "GET" { |
| 62 | + return responder.respond(response!(404)); |
| 63 | + } |
| 64 | + |
| 65 | + let uri = request.uri(); |
| 66 | + let query = uri.query().unwrap_or_default(); |
| 67 | + |
| 68 | + let image_url: String = match get_decoded(query, "url") { |
| 69 | + Some(url) => url.to_string(), |
| 70 | + None => return responder.respond(response!(400)), |
| 71 | + }; |
| 72 | + |
| 73 | + let width: u32 = match get_int(query, "width") { |
| 74 | + Some(value) => value, |
| 75 | + None => return responder.respond(response!(400)), |
| 76 | + }; |
| 77 | + |
| 78 | + let height: u32 = match get_int(query, "height") { |
| 79 | + Some(value) => value, |
| 80 | + None => return responder.respond(response!(400)), |
| 81 | + }; |
| 82 | + |
| 83 | + crate::util::thread::spawn!(async move { |
| 84 | + let image_bytes = crate::util::http::download_bytes(image_url).await; |
| 85 | + let Ok(image_bytes) = image_bytes else { |
| 86 | + return responder.respond(response!(404)); |
| 87 | + }; |
| 88 | + |
| 89 | + let image_src = crate::util::image::Image::try_from(image_bytes); |
| 90 | + let image_src = image_src.and_then(|src| src.resize(width, height)); |
| 91 | + let Ok(image_src) = image_src else { |
| 92 | + return responder.respond(response!(500)); |
| 93 | + }; |
| 94 | + |
| 95 | + let Ok(image_bytes) = TryInto::<Vec<u8>>::try_into(&image_src) else { |
| 96 | + return responder.respond(response!(500)); |
| 97 | + }; |
| 98 | + |
| 99 | + responder.respond(response!(image_src, image_bytes)); |
| 100 | + }); |
| 101 | + }); |
| 102 | +} |
0 commit comments