From b08cfe37a28e7c84f754b4b75be9e2c1cec4ab22 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 3 Oct 2020 13:35:45 -0400 Subject: restored debug script, successfully getting image from URI into file --- dichroism/.gitignore | 4 + dichroism/Cargo.lock | 260 +++++++++++++++++++++++++++++++++++++++++++++++++- dichroism/Cargo.toml | 1 + dichroism/debug.sh | 2 + dichroism/src/main.rs | 33 ++++--- 5 files changed, 284 insertions(+), 16 deletions(-) create mode 100755 dichroism/debug.sh diff --git a/dichroism/.gitignore b/dichroism/.gitignore index 17e109a..82ce5a4 100644 --- a/dichroism/.gitignore +++ b/dichroism/.gitignore @@ -1,2 +1,6 @@ +/*.jpeg +/*.jpg +/*.png +/*.webp image-uri.txt target/ diff --git a/dichroism/Cargo.lock b/dichroism/Cargo.lock index 61f957d..14ed1f5 100644 --- a/dichroism/Cargo.lock +++ b/dichroism/Cargo.lock @@ -47,7 +47,7 @@ dependencies = [ "actix-service", "actix-threadpool", "actix-utils", - "base64", + "base64 0.12.3", "bitflags", "brotli2", "bytes", @@ -276,6 +276,12 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e" +[[package]] +name = "adler32" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" + [[package]] name = "aho-corasick" version = "0.7.13" @@ -329,7 +335,7 @@ dependencies = [ "actix-http", "actix-rt", "actix-service", - "base64", + "base64 0.12.3", "bytes", "derive_more", "futures-core", @@ -351,7 +357,7 @@ dependencies = [ "addr2line", "cfg-if", "libc", - "miniz_oxide", + "miniz_oxide 0.4.2", "object", "rustc-demangle", ] @@ -368,6 +374,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + [[package]] name = "bitflags" version = "1.2.1" @@ -409,6 +421,12 @@ version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" +[[package]] +name = "bytemuck" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41aa2ec95ca3b5c54cf73c91acf06d24f4495d5f1b1c12506ae3483d646177ac" + [[package]] name = "byteorder" version = "1.3.4" @@ -451,6 +469,12 @@ dependencies = [ "bitflags", ] +[[package]] +name = "color_quant" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dbbb57365263e881e805dc77d94697c9118fd94d8da011240555aa7b23445bd" + [[package]] name = "const_fn" version = "0.4.2" @@ -489,6 +513,63 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-channel" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b153fe7cbef478c567df0f972e02e6d736db11affe43dfc9c56a9374d1adfb87" +dependencies = [ + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-deque" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "lazy_static", + "maybe-uninit", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +dependencies = [ + "autocfg", + "cfg-if", + "lazy_static", +] + +[[package]] +name = "deflate" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174" +dependencies = [ + "adler32", + "byteorder", +] + [[package]] name = "derive_more" version = "0.99.11" @@ -505,8 +586,10 @@ name = "dichroism" version = "0.1.0" dependencies = [ "actix-web", + "base64 0.13.0", "diesel", "env_logger", + "image", "lazy_static", "listenfd", "log", @@ -605,7 +688,7 @@ dependencies = [ "cfg-if", "crc32fast", "libc", - "miniz_oxide", + "miniz_oxide 0.4.2", ] [[package]] @@ -743,6 +826,16 @@ dependencies = [ "wasi", ] +[[package]] +name = "gif" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02efba560f227847cb41463a7395c514d127d4f74fff12ef0137fff1b84b96c4" +dependencies = [ + "color_quant", + "weezl", +] + [[package]] name = "gimli" version = "0.22.0" @@ -840,6 +933,24 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "image" +version = "0.23.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985fc06b1304d19c28d5c562ed78ef5316183f2b0053b46763a0b94862373c34" +dependencies = [ + "bytemuck", + "byteorder", + "gif", + "jpeg-decoder", + "num-iter", + "num-rational", + "num-traits", + "png", + "scoped_threadpool", + "tiff", +] + [[package]] name = "indexmap" version = "1.6.0" @@ -886,6 +997,16 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" +[[package]] +name = "jpeg-decoder" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc797adac5f083b8ff0ca6f6294a999393d76e197c36488e2ef732c4715f6fa3" +dependencies = [ + "byteorder", + "rayon", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -968,6 +1089,12 @@ dependencies = [ "linked-hash-map", ] +[[package]] +name = "lzw" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084" + [[package]] name = "match_cfg" version = "0.1.0" @@ -980,18 +1107,42 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" +[[package]] +name = "maybe-uninit" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" + [[package]] name = "memchr" version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" +[[package]] +name = "memoffset" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043175f069eda7b85febe4a74abbaeff828d9f8b448515d3151a14a3542811aa" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "miniz_oxide" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435" +dependencies = [ + "adler32", +] + [[package]] name = "miniz_oxide" version = "0.4.2" @@ -1055,6 +1206,47 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "num-integer" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6e6b7c748f995c4c29c5f5ae0248536e04a5739927c74ec0fa564805094b9f" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5b4d7360f362cfb50dde8143501e6940b22f644be75a4cc90b2d81968908138" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.13.0" @@ -1153,6 +1345,18 @@ version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" +[[package]] +name = "png" +version = "0.16.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfe7f9f1c730833200b134370e1d5098964231af8450bce9b78ee3ab5278b970" +dependencies = [ + "bitflags", + "crc32fast", + "deflate", + "miniz_oxide 0.3.7", +] + [[package]] name = "ppv-lite86" version = "0.2.9" @@ -1236,6 +1440,31 @@ dependencies = [ "rand_core", ] +[[package]] +name = "rayon" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf6960dc9a5b4ee8d3e4c5787b4a112a8818e0290a42ff664ad60692fdf2032" +dependencies = [ + "autocfg", + "crossbeam-deque", + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8c4fec834fb6e6d2dd5eece3c7b432a52f0ba887cf40e595190c4107edc08bf" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "lazy_static", + "num_cpus", +] + [[package]] name = "redox_syscall" version = "0.1.57" @@ -1291,6 +1520,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +[[package]] +name = "scoped_threadpool" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" + [[package]] name = "scopeguard" version = "1.1.0" @@ -1524,6 +1759,17 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "tiff" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f3b8a87c4da944c3f27e5943289171ac71a6150a79ff6bacfff06d159dfff2f" +dependencies = [ + "byteorder", + "lzw", + "miniz_oxide 0.3.7", +] + [[package]] name = "time" version = "0.2.22" @@ -1806,6 +2052,12 @@ version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" +[[package]] +name = "weezl" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0e26e7a4d998e3d7949c69444b8b4916bac810da0d3a82ae612c89e952782f4" + [[package]] name = "widestring" version = "0.4.2" diff --git a/dichroism/Cargo.toml b/dichroism/Cargo.toml index 749d362..1a4679b 100644 --- a/dichroism/Cargo.toml +++ b/dichroism/Cargo.toml @@ -15,3 +15,4 @@ lazy_static = "1" listenfd = "0.3" log = "0.4" regex = "1" +image = "0.23" diff --git a/dichroism/debug.sh b/dichroism/debug.sh new file mode 100755 index 0000000..c5d1213 --- /dev/null +++ b/dichroism/debug.sh @@ -0,0 +1,2 @@ +#!/bin/sh +systemfd --no-pid -s http::8000 -- cargo watch -x run diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs index 77fd0a1..6072198 100644 --- a/dichroism/src/main.rs +++ b/dichroism/src/main.rs @@ -1,13 +1,14 @@ #[macro_use] extern crate lazy_static; use actix_web::{get, post, App, HttpResponse, HttpServer, Responder}; -use base64; +use base64::decode; use listenfd::ListenFd; use regex::Regex; lazy_static! { static ref DATA_URI_RE: Regex = - Regex::new("^data:image/(?Ppng|jpg|jpeg);base64,(?P.+)").unwrap(); + Regex::new("^data:image/(?Ppng|jpg|jpeg);base64,(?P.+)") + .expect("Couldn't parse Regex!"); } #[get("/")] @@ -20,21 +21,26 @@ async fn create_product(req_body: String) -> impl Responder { let caps = if let Some(c) = DATA_URI_RE.captures(&req_body) { c } else { - return HttpResponse::BadRequest().body("Invalid data URI"); + return HttpResponse::BadRequest().body("Invalid data URI or image format."); }; - let format = if let Some(f) = caps.name("format") { - f.as_str() - } else { - return HttpResponse::BadRequest().body("Invalid image format."); - }; + let format = caps + .name("format") + .expect("Data URI format extraction should never fail.") + .as_str(); + let data = caps + .name("data") + .expect("Data URI data extraction should never fail.") + .as_str(); - let data = if let Some(d) = caps.name("data") { - d.as_str() + let bytes = if let Ok(b) = decode(data) { + b } else { - return HttpResponse::BadRequest().body("No encoded data."); + return HttpResponse::BadRequest().body("Invalid image data in data URI."); }; + let img = image::load_from_memory(&bytes).unwrap(); + img.save("test.png").unwrap(); HttpResponse::Ok().body(format!("created\n{}\n{}", format, data)) } @@ -52,10 +58,13 @@ async fn main() -> std::io::Result<()> { .service(create_product) .service(get_products) }); + server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { + // "Debug mode" with cargo watch auto-reloading server.listen(l)? } else { - server.bind("127.0.0.1:8080")? + // "Release mode" + server.bind("127.0.0.1:8000")? }; server.run().await -- cgit v1.2.3