Use base64 for encoding & Upgrade Media Handling

This commit is contained in:
spikecodes 2020-11-30 20:33:55 -08:00
parent 9a6430656d
commit 9d78266494
7 changed files with 90 additions and 76 deletions

78
Cargo.lock generated
View File

@ -93,7 +93,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a60f9ba7c4e6df97f3aacb14bb5c0cd7d98a49dcbaed0d7f292912ad9a6a3ed2" checksum = "a60f9ba7c4e6df97f3aacb14bb5c0cd7d98a49dcbaed0d7f292912ad9a6a3ed2"
dependencies = [ dependencies = [
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -267,7 +267,7 @@ checksum = "ad26f77093333e0e7c6ffe54ebe3582d908a104e448723eec6d43d08b07143fb"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -438,9 +438,9 @@ dependencies = [
[[package]] [[package]]
name = "async-io" name = "async-io"
version = "1.3.0" version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "458c8f66c246624e7cf87c01451f3392ab77d66a0f105a49d9353b30ea97ced8" checksum = "9315f8f07556761c3e48fec2e6b276004acf426e6dc068b2c2251854d65ee0fd"
dependencies = [ dependencies = [
"concurrent-queue", "concurrent-queue",
"fastrand", "fastrand",
@ -506,7 +506,7 @@ checksum = "8d3a45e77e34375a7923b1e8febb049bb011f064714a8e17a1a616fef01da13d"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -837,7 +837,7 @@ checksum = "41cb0e6161ad61ed084a36ba71fbba9e3ac5aee3606fb607fe08da6acbcf3d8c"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -879,7 +879,7 @@ dependencies = [
"heck", "heck",
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -1012,7 +1012,7 @@ dependencies = [
"proc-macro-hack", "proc-macro-hack",
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -1338,9 +1338,9 @@ checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.45" version = "0.3.46"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca059e81d9486668f12d455a4ea6daa600bd408134cd17e3d3fb5a32d1f016f8" checksum = "cf3d7383929f7c9c7c2d0fa596f325832df98c3704f2c60553080f7127a58175"
dependencies = [ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
@ -1398,8 +1398,8 @@ version = "0.1.8"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"askama", "askama",
"base64 0.13.0",
"chrono", "chrono",
"percent-encoding",
"pulldown-cmark", "pulldown-cmark",
"serde", "serde",
"serde_json", "serde_json",
@ -1698,7 +1698,7 @@ checksum = "65ad2ae56b6abe3a1ee25f15ee605bacadb9a764edaba9c2bf4103800d4a1895"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -1709,7 +1709,7 @@ checksum = "f8e8d2bf0b23038a4424865103a4df472855692821aab4e4f5c3312d461d9e5f"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -2013,7 +2013,7 @@ checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -2175,7 +2175,7 @@ dependencies = [
"quote 1.0.7", "quote 1.0.7",
"serde", "serde",
"serde_derive", "serde_derive",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -2191,7 +2191,7 @@ dependencies = [
"serde_derive", "serde_derive",
"serde_json", "serde_json",
"sha1", "sha1",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -2241,9 +2241,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.52" version = "1.0.53"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c1e438504729046a5cfae47f97c30d6d083c7d91d94603efdae3477fc070d4c" checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
@ -2267,7 +2267,7 @@ checksum = "9ba20f23e85b10754cd195504aebf6a27e2e6cbe28c17778a0c930724628dd56"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -2334,7 +2334,7 @@ dependencies = [
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"standback", "standback",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -2428,7 +2428,7 @@ checksum = "80e0ccfc3378da0cce270c946b676a376943f5cd16aeba64568e7939806f4ada"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
] ]
[[package]] [[package]]
@ -2620,36 +2620,36 @@ checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.68" version = "0.2.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42" checksum = "3cd364751395ca0f68cafb17666eee36b63077fb5ecd972bbcd74c90c4bf736e"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "cfg-if 1.0.0",
"wasm-bindgen-macro", "wasm-bindgen-macro",
] ]
[[package]] [[package]]
name = "wasm-bindgen-backend" name = "wasm-bindgen-backend"
version = "0.2.68" version = "0.2.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f22b422e2a757c35a73774860af8e112bff612ce6cb604224e8e47641a9e4f68" checksum = "1114f89ab1f4106e5b55e688b828c0ab0ea593a1ea7c094b141b14cbaaec2d62"
dependencies = [ dependencies = [
"bumpalo", "bumpalo",
"lazy_static", "lazy_static",
"log", "log",
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
[[package]] [[package]]
name = "wasm-bindgen-futures" name = "wasm-bindgen-futures"
version = "0.4.18" version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7866cab0aa01de1edf8b5d7936938a7e397ee50ce24119aef3e1eaa3b6171da" checksum = "1fe9756085a84584ee9457a002b7cdfe0bfff169f45d2591d8be1345a6780e35"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "cfg-if 1.0.0",
"js-sys", "js-sys",
"wasm-bindgen", "wasm-bindgen",
"web-sys", "web-sys",
@ -2657,9 +2657,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro" name = "wasm-bindgen-macro"
version = "0.2.68" version = "0.2.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b13312a745c08c469f0b292dd2fcd6411dba5f7160f593da6ef69b64e407038" checksum = "7a6ac8995ead1f084a8dea1e65f194d0973800c7f571f6edd70adf06ecf77084"
dependencies = [ dependencies = [
"quote 1.0.7", "quote 1.0.7",
"wasm-bindgen-macro-support", "wasm-bindgen-macro-support",
@ -2667,28 +2667,28 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro-support" name = "wasm-bindgen-macro-support"
version = "0.2.68" version = "0.2.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f249f06ef7ee334cc3b8ff031bfc11ec99d00f34d86da7498396dc1e3b1498fe" checksum = "b5a48c72f299d80557c7c62e37e7225369ecc0c963964059509fbafe917c7549"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
"quote 1.0.7", "quote 1.0.7",
"syn 1.0.52", "syn 1.0.53",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
[[package]] [[package]]
name = "wasm-bindgen-shared" name = "wasm-bindgen-shared"
version = "0.2.68" version = "0.2.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" checksum = "7e7811dd7f9398f14cc76efd356f98f03aa30419dea46aa810d71e819fc97158"
[[package]] [[package]]
name = "web-sys" name = "web-sys"
version = "0.3.45" version = "0.3.46"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bf6ef87ad7ae8008e15a355ce696bed26012b7caa21605188cfd8214ab51e2d" checksum = "222b1ef9334f92a21d3fb53dc3fd80f30836959a90f9274a626d7e06315ba3c3"
dependencies = [ dependencies = [
"js-sys", "js-sys",
"wasm-bindgen", "wasm-bindgen",

View File

@ -9,10 +9,10 @@ edition = "2018"
[features] [features]
default = ["proxy"] default = ["proxy"]
proxy = ["actix-web/rustls", "percent-encoding"] proxy = ["actix-web/rustls", "base64"]
[dependencies] [dependencies]
percent-encoding = { version = "2.1.0", optional = true } base64 = { version = "0.13.0", optional = true }
actix-web = "3.2.0" actix-web = "3.2.0"
surf = "2.1.0" surf = "2.1.0"
askama = "0.8.0" askama = "0.8.0"

View File

@ -6,7 +6,7 @@ use chrono::{TimeZone, Utc};
use pulldown_cmark::{html, Options, Parser}; use pulldown_cmark::{html, Options, Parser};
#[cfg(feature = "proxy")] #[cfg(feature = "proxy")]
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; use base64::encode;
// STRUCTS // STRUCTS
#[derive(Template)] #[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 { async fn format_url(url: &str) -> String {
#[cfg(feature = "proxy")] #[cfg(feature = "proxy")]
return utf8_percent_encode(url, NON_ALPHANUMERIC).to_string(); return "/imageproxy/".to_string() + encode(url).as_str();
#[cfg(not(feature = "proxy"))] #[cfg(not(feature = "proxy"))]
return url.to_string(); return url.to_string();
} }
// UTILITIES // UTILITIES
async fn media(data: &serde_json::Value) -> String { async fn media(data: &serde_json::Value) -> (String, String) {
let post_hint: &str = data["data"]["post_hint"].as_str().unwrap_or(""); let post_type: &str;
let has_media: bool = data["data"]["media"].is_object(); let url = if !data["preview"]["reddit_video_preview"]["fallback_url"].is_null() {
post_type = "video";
let prefix = if cfg!(feature = "proxy") { "/imageproxy/" } else { "" }; format_url(data["preview"]["reddit_video_preview"]["fallback_url"].as_str().unwrap()).await
} else if !data["secure_media"]["reddit_video"]["fallback_url"].is_null() {
let media: String = if !has_media { post_type = "video";
format!(r#"<h4 class="post_body"><a href="{u}">{u}</a></h4>"#, u = data["data"]["url"].as_str().unwrap()) 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 { } else {
format!( post_type = "link";
r#"<img class="post_image" src="{}{}.png"/>"#, data["url"].as_str().unwrap().to_string()
prefix,
format_url(data["data"]["url"].as_str().unwrap()).await
)
}; };
match post_hint { dbg!(post_type, url.to_string());
"hosted:video" => format!(
r#"<video class="post_image" src="{}{}" controls/>"#, (post_type.to_string(), url)
prefix,
format_url(data["data"]["media"]["reddit_video"]["fallback_url"].as_str().unwrap()).await
),
"image" => format!(r#"<img class="post_image" src="{}{}"/>"#, prefix, format_url(data["data"]["url"].as_str().unwrap()).await),
"self" => String::from(""),
_ => media,
}
} }
async fn markdown_to_html(md: &str) -> String { async fn markdown_to_html(md: &str) -> String {
@ -127,6 +120,8 @@ async fn parse_post(json: serde_json::Value) -> Result<Post, &'static str> {
let unix_time: i64 = post_data["data"]["created_utc"].as_f64().unwrap().round() as i64; let unix_time: i64 = post_data["data"]["created_utc"].as_f64().unwrap().round() as i64;
let score = post_data["data"]["score"].as_i64().unwrap(); let score = post_data["data"]["score"].as_i64().unwrap();
let media = media(&post_data["data"]).await;
let post = Post { let post = Post {
title: val(post_data, "title").await, title: val(post_data, "title").await,
community: val(post_data, "subreddit").await, community: val(post_data, "subreddit").await,
@ -134,7 +129,8 @@ async fn parse_post(json: serde_json::Value) -> Result<Post, &'static str> {
author: val(post_data, "author").await, author: val(post_data, "author").await,
url: val(post_data, "permalink").await, url: val(post_data, "permalink").await,
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() }, score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
media: media(post_data).await, post_type: media.0,
media: media.1,
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(), time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
flair: Flair( flair: Flair(
val(post_data, "link_flair_text").await, val(post_data, "link_flair_text").await,

View File

@ -1,22 +1,27 @@
use actix_web::{client::Client, get, web, Error, HttpResponse, Result}; use actix_web::{client::Client, get, web, Error, HttpResponse, Result};
#[cfg(feature = "proxy")] #[cfg(feature = "proxy")]
use percent_encoding::percent_decode_str; use base64::decode;
#[get("/imageproxy/{url:.*}")] #[get("/imageproxy/{url:.*}")]
async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> { async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
if cfg!(feature = "proxy") { if cfg!(feature = "proxy") {
#[cfg(feature = "proxy")] let media: String;
let media: String = percent_decode_str(url.as_str()).decode_utf8()?.to_string();
#[cfg(not(feature = "proxy"))] #[cfg(not(feature = "proxy"))]
let media: String = url; let media = url;
#[cfg(feature = "proxy")]
match decode(url) {
Ok(bytes) => media = String::from_utf8(bytes).unwrap(),
Err(_e) => return Ok(HttpResponse::Ok().body("")),
};
dbg!(&media); dbg!(&media);
let client = Client::default(); let client = Client::default();
client client
.get(media) .get(media.replace("&amp;", "&"))
.send() .send()
.await .await
.map_err(Error::from) .map_err(Error::from)

View File

@ -21,6 +21,7 @@ pub struct Post {
pub author: String, pub author: String,
pub url: String, pub url: String,
pub score: String, pub score: String,
pub post_type: String,
pub media: String, pub media: String,
pub time: String, pub time: String,
pub flair: Flair, pub flair: Flair,
@ -121,6 +122,7 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Pos
body: val(post, "body").await, body: val(post, "body").await,
author: val(post, "author").await, author: val(post, "author").await,
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() }, score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
post_type: "link".to_string(),
media: img, media: img,
url: val(post, "permalink").await, url: val(post, "permalink").await,
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(), time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),

View File

@ -207,16 +207,21 @@ span {
margin: 5px; margin: 5px;
} }
.post_right > a {
color: aqua;
}
.post_right > p { .post_right > p {
opacity: 0.75; opacity: 0.75;
} }
.post_image { .post_media {
max-width: 500px; max-width: 500px;
align-self: center; align-self: center;
} }
.post_image[src=""] { .post_media[src=""] {
display: none; display: none;
} }
@ -230,7 +235,7 @@ span {
margin-top: 1.5em; margin-top: 1.5em;
} }
.post_body a { .post_body > a {
text-decoration: underline; text-decoration: underline;
color: aqua; color: aqua;
} }

View File

@ -23,7 +23,13 @@
<small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small> <small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %} {% endif %}
</h3> </h3>
{{ post.media }} {% if post.post_type == "image" %}
<img class="post_media" src="{{ post.media }}"/>
{% else if post.post_type == "video" %}
<video class="post_media" src="{{ post.media }}" controls autoplay loop>
{% else if post.post_type == "link" %}
<a href="{{ post.media }}">{{ post.media }}</a>
{% endif %}
<h4 class="post_body">{{ post.body }}</h4> <h4 class="post_body">{{ post.body }}</h4>
</div> </div>
</div> </div>