libreddit/src/proxy.rs

31 lines
738 B
Rust
Raw Normal View History

2020-12-15 01:35:04 +01:00
use actix_web::{client::Client, web, Error, HttpResponse, Result};
2020-11-30 03:50:29 +01:00
#[cfg(feature = "proxy")]
use base64::decode;
2020-11-23 05:22:51 +01:00
2020-12-15 01:35:04 +01:00
pub async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
2020-11-24 01:57:37 +01:00
if cfg!(feature = "proxy") {
2020-12-29 05:49:15 +01:00
#[cfg(feature = "proxy")]
let media: String;
2020-11-30 03:50:29 +01: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 03:50:29 +01:00
2020-11-24 01:57:37 +01:00
let client = Client::default();
2020-11-30 03:50:29 +01:00
client
.get(media.replace("&amp;", "&"))
2020-11-24 01:57:37 +01:00
.send()
.await
.map_err(Error::from)
2021-01-01 21:33:57 +01:00
.map(|res| HttpResponse::build(res.status()).streaming(res))
2020-11-24 01:57:37 +01:00
} else {
Ok(HttpResponse::Ok().body(""))
}
2020-11-30 03:50:29 +01:00
}