2021-01-03 17:50:23 +13:00
|
|
|
use actix_web::{client::Client, error, web, Error, HttpResponse, Result};
|
|
|
|
use url::Url;
|
2020-11-30 15:50:29 +13:00
|
|
|
|
2020-12-01 17:33:55 +13:00
|
|
|
use base64::decode;
|
2020-11-23 17:22:51 +13:00
|
|
|
|
2021-01-03 17:50:23 +13:00
|
|
|
pub async fn handler(web::Path(b64): web::Path<String>) -> Result<HttpResponse> {
|
|
|
|
let domains = vec![
|
2021-01-04 07:22:41 +13:00
|
|
|
// THUMBNAILS
|
2021-01-03 17:50:23 +13:00
|
|
|
"a.thumbs.redditmedia.com",
|
|
|
|
"b.thumbs.redditmedia.com",
|
2021-01-04 07:22:41 +13:00
|
|
|
// ICONS
|
|
|
|
"styles.redditmedia.com",
|
2021-01-04 15:23:57 +13:00
|
|
|
"www.redditstatic.com",
|
2021-01-04 07:22:41 +13:00
|
|
|
// PREVIEWS
|
2021-01-03 17:50:23 +13:00
|
|
|
"preview.redd.it",
|
|
|
|
"external-preview.redd.it",
|
2021-01-04 07:22:41 +13:00
|
|
|
// MEDIA
|
2021-01-03 17:50:23 +13:00
|
|
|
"i.redd.it",
|
|
|
|
"v.redd.it",
|
|
|
|
];
|
2020-11-30 15:50:29 +13:00
|
|
|
|
2021-01-03 17:50:23 +13:00
|
|
|
match decode(b64) {
|
|
|
|
Ok(bytes) => {
|
2021-01-04 18:31:21 +13:00
|
|
|
let media = String::from_utf8(bytes).unwrap_or_default();
|
2020-12-01 17:33:55 +13:00
|
|
|
|
2021-01-03 17:50:23 +13:00
|
|
|
match Url::parse(media.as_str()) {
|
|
|
|
Ok(url) => {
|
|
|
|
let domain = url.domain().unwrap_or_default();
|
2020-11-30 15:50:29 +13:00
|
|
|
|
2021-01-03 17:50:23 +13:00
|
|
|
if domains.contains(&domain) {
|
2021-01-07 11:19:10 +13:00
|
|
|
Client::default().get(media.replace("&", "&")).send().await.map_err(Error::from).map(|res| {
|
|
|
|
HttpResponse::build(res.status())
|
|
|
|
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
|
|
|
|
.header("Content-Length", res.headers().get("Content-Length").unwrap().to_owned())
|
|
|
|
.header("Content-Type", res.headers().get("Content-Type").unwrap().to_owned())
|
|
|
|
.streaming(res)
|
|
|
|
})
|
2021-01-03 17:50:23 +13:00
|
|
|
} else {
|
|
|
|
Err(error::ErrorForbidden("Resource must be from Reddit"))
|
|
|
|
}
|
|
|
|
}
|
2021-01-07 08:11:04 +13:00
|
|
|
Err(_) => Err(error::ErrorBadRequest("Can't parse base64 into URL")),
|
2021-01-03 17:50:23 +13:00
|
|
|
}
|
|
|
|
}
|
2021-01-07 08:11:04 +13:00
|
|
|
Err(_) => Err(error::ErrorBadRequest("Can't decode base64")),
|
2020-11-24 13:57:37 +13:00
|
|
|
}
|
2020-11-30 15:50:29 +13:00
|
|
|
}
|