From 9d78266494d42dfff25ee7e997ba4ab1eb493a67 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Mon, 30 Nov 2020 20:33:55 -0800 Subject: [PATCH] Use base64 for encoding & Upgrade Media Handling --- Cargo.lock | 78 ++++++++++++++++++++++----------------------- Cargo.toml | 4 +-- src/post.rs | 48 +++++++++++++--------------- src/proxy.rs | 15 ++++++--- src/utils.rs | 2 ++ static/style.css | 11 +++++-- templates/post.html | 8 ++++- 7 files changed, 90 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 439f0ec..0957b93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,7 +93,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a60f9ba7c4e6df97f3aacb14bb5c0cd7d98a49dcbaed0d7f292912ad9a6a3ed2" dependencies = [ "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -267,7 +267,7 @@ checksum = "ad26f77093333e0e7c6ffe54ebe3582d908a104e448723eec6d43d08b07143fb" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -438,9 +438,9 @@ dependencies = [ [[package]] name = "async-io" -version = "1.3.0" +version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "458c8f66c246624e7cf87c01451f3392ab77d66a0f105a49d9353b30ea97ced8" +checksum = "9315f8f07556761c3e48fec2e6b276004acf426e6dc068b2c2251854d65ee0fd" dependencies = [ "concurrent-queue", "fastrand", @@ -506,7 +506,7 @@ checksum = "8d3a45e77e34375a7923b1e8febb049bb011f064714a8e17a1a616fef01da13d" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -837,7 +837,7 @@ checksum = "41cb0e6161ad61ed084a36ba71fbba9e3ac5aee3606fb607fe08da6acbcf3d8c" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -879,7 +879,7 @@ dependencies = [ "heck", "proc-macro2 1.0.24", "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -1012,7 +1012,7 @@ dependencies = [ "proc-macro-hack", "proc-macro2 1.0.24", "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -1338,9 +1338,9 @@ checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" [[package]] name = "js-sys" -version = "0.3.45" +version = "0.3.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca059e81d9486668f12d455a4ea6daa600bd408134cd17e3d3fb5a32d1f016f8" +checksum = "cf3d7383929f7c9c7c2d0fa596f325832df98c3704f2c60553080f7127a58175" dependencies = [ "wasm-bindgen", ] @@ -1398,8 +1398,8 @@ version = "0.1.8" dependencies = [ "actix-web", "askama", + "base64 0.13.0", "chrono", - "percent-encoding", "pulldown-cmark", "serde", "serde_json", @@ -1698,7 +1698,7 @@ checksum = "65ad2ae56b6abe3a1ee25f15ee605bacadb9a764edaba9c2bf4103800d4a1895" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -1709,7 +1709,7 @@ checksum = "f8e8d2bf0b23038a4424865103a4df472855692821aab4e4f5c3312d461d9e5f" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -2013,7 +2013,7 @@ checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -2175,7 +2175,7 @@ dependencies = [ "quote 1.0.7", "serde", "serde_derive", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -2191,7 +2191,7 @@ dependencies = [ "serde_derive", "serde_json", "sha1", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -2241,9 +2241,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.52" +version = "1.0.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c1e438504729046a5cfae47f97c30d6d083c7d91d94603efdae3477fc070d4c" +checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.7", @@ -2267,7 +2267,7 @@ checksum = "9ba20f23e85b10754cd195504aebf6a27e2e6cbe28c17778a0c930724628dd56" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -2334,7 +2334,7 @@ dependencies = [ "proc-macro2 1.0.24", "quote 1.0.7", "standback", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -2428,7 +2428,7 @@ checksum = "80e0ccfc3378da0cce270c946b676a376943f5cd16aeba64568e7939806f4ada" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", ] [[package]] @@ -2620,36 +2620,36 @@ checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" [[package]] name = "wasm-bindgen" -version = "0.2.68" +version = "0.2.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42" +checksum = "3cd364751395ca0f68cafb17666eee36b63077fb5ecd972bbcd74c90c4bf736e" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.68" +version = "0.2.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f22b422e2a757c35a73774860af8e112bff612ce6cb604224e8e47641a9e4f68" +checksum = "1114f89ab1f4106e5b55e688b828c0ab0ea593a1ea7c094b141b14cbaaec2d62" dependencies = [ "bumpalo", "lazy_static", "log", "proc-macro2 1.0.24", "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-futures" -version = "0.4.18" +version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7866cab0aa01de1edf8b5d7936938a7e397ee50ce24119aef3e1eaa3b6171da" +checksum = "1fe9756085a84584ee9457a002b7cdfe0bfff169f45d2591d8be1345a6780e35" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", "js-sys", "wasm-bindgen", "web-sys", @@ -2657,9 +2657,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.68" +version = "0.2.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b13312a745c08c469f0b292dd2fcd6411dba5f7160f593da6ef69b64e407038" +checksum = "7a6ac8995ead1f084a8dea1e65f194d0973800c7f571f6edd70adf06ecf77084" dependencies = [ "quote 1.0.7", "wasm-bindgen-macro-support", @@ -2667,28 +2667,28 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.68" +version = "0.2.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f249f06ef7ee334cc3b8ff031bfc11ec99d00f34d86da7498396dc1e3b1498fe" +checksum = "b5a48c72f299d80557c7c62e37e7225369ecc0c963964059509fbafe917c7549" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.7", - "syn 1.0.52", + "syn 1.0.53", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.68" +version = "0.2.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" +checksum = "7e7811dd7f9398f14cc76efd356f98f03aa30419dea46aa810d71e819fc97158" [[package]] name = "web-sys" -version = "0.3.45" +version = "0.3.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bf6ef87ad7ae8008e15a355ce696bed26012b7caa21605188cfd8214ab51e2d" +checksum = "222b1ef9334f92a21d3fb53dc3fd80f30836959a90f9274a626d7e06315ba3c3" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index bdec072..b267981 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,10 +9,10 @@ edition = "2018" [features] default = ["proxy"] -proxy = ["actix-web/rustls", "percent-encoding"] +proxy = ["actix-web/rustls", "base64"] [dependencies] -percent-encoding = { version = "2.1.0", optional = true } +base64 = { version = "0.13.0", optional = true } actix-web = "3.2.0" surf = "2.1.0" askama = "0.8.0" diff --git a/src/post.rs b/src/post.rs index 46281f9..7ce9ccb 100644 --- a/src/post.rs +++ b/src/post.rs @@ -6,7 +6,7 @@ use chrono::{TimeZone, Utc}; use pulldown_cmark::{html, Options, Parser}; #[cfg(feature = "proxy")] -use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; +use base64::encode; // STRUCTS #[derive(Template)] @@ -71,39 +71,32 @@ async fn page(web::Path((_sub, id)): web::Path<(String, String)>, params: web::Q async fn format_url(url: &str) -> String { #[cfg(feature = "proxy")] - return utf8_percent_encode(url, NON_ALPHANUMERIC).to_string(); + return "/imageproxy/".to_string() + encode(url).as_str(); #[cfg(not(feature = "proxy"))] return url.to_string(); } // UTILITIES -async fn media(data: &serde_json::Value) -> String { - let post_hint: &str = data["data"]["post_hint"].as_str().unwrap_or(""); - let has_media: bool = data["data"]["media"].is_object(); - - let prefix = if cfg!(feature = "proxy") { "/imageproxy/" } else { "" }; - - let media: String = if !has_media { - format!(r#"

{u}

"#, u = data["data"]["url"].as_str().unwrap()) +async fn media(data: &serde_json::Value) -> (String, String) { + let post_type: &str; + let url = if !data["preview"]["reddit_video_preview"]["fallback_url"].is_null() { + post_type = "video"; + format_url(data["preview"]["reddit_video_preview"]["fallback_url"].as_str().unwrap()).await + } else if !data["secure_media"]["reddit_video"]["fallback_url"].is_null() { + post_type = "video"; + format_url(data["secure_media"]["reddit_video"]["fallback_url"].as_str().unwrap()).await + } else if data["post_hint"].as_str().unwrap_or("") == "image" { + post_type = "image"; + format_url(data["preview"]["images"][0]["source"]["url"].as_str().unwrap()).await } else { - format!( - r#""#, - prefix, - format_url(data["data"]["url"].as_str().unwrap()).await - ) + post_type = "link"; + data["url"].as_str().unwrap().to_string() }; - match post_hint { - "hosted:video" => format!( - r#"