redsunlib/src/proxy.rs

28 lines
697 B
Rust
Raw Normal View History

2020-11-30 15:50:29 +13:00
use actix_web::{client::Client, get, web, Error, HttpResponse, Result};
#[cfg(feature = "proxy")]
use percent_encoding::percent_decode_str;
2020-11-23 17:22:51 +13:00
#[get("/imageproxy/{url:.*}")]
async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
2020-11-24 13:57:37 +13:00
if cfg!(feature = "proxy") {
2020-11-30 15:50:29 +13:00
#[cfg(feature = "proxy")]
let media: String = percent_decode_str(url.as_str()).decode_utf8()?.to_string();
#[cfg(not(feature = "proxy"))]
let media: String = url;
dbg!(&media);
2020-11-24 13:57:37 +13:00
let client = Client::default();
2020-11-30 15:50:29 +13:00
client
.get(media)
2020-11-24 13:57:37 +13:00
.send()
.await
.map_err(Error::from)
2020-11-30 15:50:29 +13:00
.and_then(|res| Ok(HttpResponse::build(res.status()).streaming(res)))
2020-11-24 13:57:37 +13:00
} else {
Ok(HttpResponse::Ok().body(""))
}
2020-11-30 15:50:29 +13:00
}