Make Proxy Optional Feature

This commit is contained in:
spikecodes 2020-11-23 16:57:37 -08:00
parent 8a488af594
commit b218ec6065
4 changed files with 42 additions and 26 deletions

30
Cargo.lock generated
View File

@ -486,7 +486,7 @@ dependencies = [
"memchr",
"num_cpus",
"once_cell",
"pin-project-lite",
"pin-project-lite 0.1.11",
"pin-utils",
"slab",
"wasm-bindgen-futures",
@ -1006,7 +1006,7 @@ dependencies = [
"futures-io",
"memchr",
"parking",
"pin-project-lite",
"pin-project-lite 0.1.11",
"waker-fn",
]
@ -1236,7 +1236,7 @@ dependencies = [
"cookie",
"futures-lite",
"infer",
"pin-project-lite",
"pin-project-lite 0.1.11",
"rand",
"serde",
"serde_json",
@ -1724,6 +1724,12 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b"
[[package]]
name = "pin-project-lite"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b063f57ec186e6140e2b8b6921e5f1bd89c7356dda5b33acc5401203ca6131c"
[[package]]
name = "pin-utils"
version = "0.1.0"
@ -1908,9 +1914,9 @@ dependencies = [
[[package]]
name = "ring"
version = "0.16.16"
version = "0.16.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b72b84d47e8ec5a4f2872e8262b8f8256c5be1c938a7d6d3a867a3ba8f722f74"
checksum = "c5911690c9b773bab7e657471afc207f3827b249a657241327e3544d79bcabdd"
dependencies = [
"cc",
"libc",
@ -2234,7 +2240,7 @@ dependencies = [
"log",
"mime_guess",
"once_cell",
"pin-project-lite",
"pin-project-lite 0.1.11",
"serde",
"serde_json",
"web-sys",
@ -2378,7 +2384,7 @@ dependencies = [
"memchr",
"mio",
"mio-uds",
"pin-project-lite",
"pin-project-lite 0.1.11",
"signal-hook-registry",
"slab",
"winapi 0.3.9",
@ -2406,7 +2412,7 @@ dependencies = [
"futures-core",
"futures-sink",
"log",
"pin-project-lite",
"pin-project-lite 0.1.11",
"tokio",
]
@ -2421,13 +2427,13 @@ dependencies = [
[[package]]
name = "tracing"
version = "0.1.21"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0987850db3733619253fe60e17cb59b82d37c7e6c0236bb81e4d6b87c879f27"
checksum = "9f47026cdc4080c07e49b37087de021820269d996f581aac150ef9e5583eefe3"
dependencies = [
"cfg-if 0.1.10",
"cfg-if 1.0.0",
"log",
"pin-project-lite",
"pin-project-lite 0.2.0",
"tracing-attributes",
"tracing-core",
]

View File

@ -7,8 +7,12 @@ version = "0.1.6"
authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"]
edition = "2018"
[features]
default = ["proxy"]
proxy = ["actix-web/rustls"]
[dependencies]
actix-web = { version = "3.2.0", features = ["rustls"] }
actix-web = "3.2.0"
surf = "2.1.0"
askama = "0.8.0"
serde = "1.0.117"

View File

@ -74,18 +74,20 @@ 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#"<h4 class="post_body"><a href="{u}">{u}</a></h4>"#, u = data["data"]["url"].as_str().unwrap())
} else {
format!(r#"<img class="post_image" src="/imageproxy/{}.png"/>"#, data["data"]["url"].as_str().unwrap())
format!(r#"<img class="post_image" src="{}{}.png"/>"#, prefix, data["data"]["url"].as_str().unwrap())
};
match post_hint {
"hosted:video" => format!(
r#"<video class="post_image" src="/imageproxy/{}" controls/>"#,
data["data"]["media"]["reddit_video"]["fallback_url"].as_str().unwrap()
r#"<video class="post_image" src="{}{}" controls/>"#,
prefix, data["data"]["media"]["reddit_video"]["fallback_url"].as_str().unwrap()
),
"image" => format!(r#"<img class="post_image" src="/imageproxy/{}"/>"#, data["data"]["url"].as_str().unwrap()),
"image" => format!(r#"<img class="post_image" src="{}{}"/>"#, prefix, data["data"]["url"].as_str().unwrap()),
"self" => String::from(""),
_ => media,
}

View File

@ -2,6 +2,7 @@ use actix_web::{get, web, HttpResponse, Result, client::Client, Error};
#[get("/imageproxy/{url:.*}")]
async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
if cfg!(feature = "proxy") {
dbg!(&url);
let client = Client::default();
client.get(url)
@ -11,4 +12,7 @@ async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
.and_then(|res| {
Ok(HttpResponse::build(res.status()).streaming(res))
})
} else {
Ok(HttpResponse::Ok().body(""))
}
}