Build Media Proxy

This commit is contained in:
spikecodes
2020-11-22 20:22:51 -08:00
parent 16bf825e14
commit a1e57d874f
6 changed files with 142 additions and 34 deletions

View File

@ -39,7 +39,7 @@ async fn main() -> std::io::Result<()> {
}
// start http server
println!("Running Libreddit on {}!", address);
println!("Running Libreddit on {}!", address.clone());
HttpServer::new(|| {
App::new()
@ -47,6 +47,8 @@ async fn main() -> std::io::Result<()> {
.service(style)
.service(favicon)
.service(robots)
// PROXY SERVICE
.service(proxy::handler)
// POST SERVICES
.service(post::short)
.service(post::page)
@ -57,7 +59,8 @@ async fn main() -> std::io::Result<()> {
// USER SERVICES
.service(user::page)
})
.bind(address)?
.bind(address.clone())
.expect(format!("Cannot bind to the address: {}", address).as_str())
.run()
.await
}

View File

@ -77,15 +77,15 @@ async fn media(data: &serde_json::Value) -> String {
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="{}.png"/>"#, data["data"]["url"].as_str().unwrap())
format!(r#"<img class="post_image" src="/imageproxy/{}.png"/>"#, data["data"]["url"].as_str().unwrap())
};
match post_hint {
"hosted:video" => format!(
r#"<video class="post_image" src="{}" controls/>"#,
r#"<video class="post_image" src="/imageproxy/{}" controls/>"#,
data["data"]["media"]["reddit_video"]["fallback_url"].as_str().unwrap()
),
"image" => format!(r#"<img class="post_image" src="{}"/>"#, data["data"]["url"].as_str().unwrap()),
"image" => format!(r#"<img class="post_image" src="/imageproxy/{}"/>"#, data["data"]["url"].as_str().unwrap()),
"self" => String::from(""),
_ => media,
}

View File

@ -0,0 +1,14 @@
use actix_web::{get, web, HttpResponse, Result, client::Client, Error};
#[get("/imageproxy/{url:.*}")]
async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
dbg!("Proxy: ", &url);
let client = Client::default();
client.get(url)
.send()
.await
.map_err(Error::from)
.and_then(|res| {
Ok(HttpResponse::build(res.status()).streaming(res))
})
}