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-13 10:43:03 +13:00
|
|
|
// EMOJI
|
|
|
|
"emoji.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-26 14:01:02 +13:00
|
|
|
let decoded = decode(b64).map(|bytes| String::from_utf8(bytes).unwrap_or_default());
|
2020-12-01 17:33:55 +13:00
|
|
|
|
2021-01-26 14:01:02 +13:00
|
|
|
match decoded {
|
|
|
|
Ok(media) => {
|
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-14 16:53:52 +13:00
|
|
|
_ => Err(error::ErrorBadRequest("Can't parse base64 into URL")),
|
2021-01-03 17:50:23 +13:00
|
|
|
}
|
|
|
|
}
|
2021-01-14 16:53:52 +13:00
|
|
|
_ => Err(error::ErrorBadRequest("Can't decode base64")),
|
2020-11-24 13:57:37 +13:00
|
|
|
}
|
2020-11-30 15:50:29 +13:00
|
|
|
}
|