redsunlib/src/proxy.rs

31 lines
747 B
Rust
Raw Normal View History

2020-12-15 13:35:04 +13:00
use actix_web::{client::Client, web, Error, HttpResponse, Result};
2020-11-30 15:50:29 +13:00
#[cfg(feature = "proxy")]
use base64::decode;
2020-11-23 17:22:51 +13:00
2020-12-15 13:35:04 +13:00
pub async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
2020-11-24 13:57:37 +13:00
if cfg!(feature = "proxy") {
2020-12-29 17:49:15 +13:00
#[cfg(feature = "proxy")]
let media: String;
2020-11-30 15:50:29 +13:00
#[cfg(not(feature = "proxy"))]
let media = url;
#[cfg(feature = "proxy")]
match decode(url) {
Ok(bytes) => media = String::from_utf8(bytes).unwrap(),
Err(_e) => return Ok(HttpResponse::Ok().body("")),
};
2020-11-30 15:50:29 +13:00
2020-11-24 13:57:37 +13:00
let client = Client::default();
2020-11-30 15:50:29 +13:00
client
.get(media.replace("&amp;", "&"))
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
}