2020-12-01 05:33:55 +01:00
|
|
|
use base64::decode;
|
2021-02-09 18:38:52 +01:00
|
|
|
use surf::{Body, Url};
|
|
|
|
use tide::{Request, Response};
|
2020-11-23 05:22:51 +01:00
|
|
|
|
2021-02-09 18:38:52 +01:00
|
|
|
pub async fn handler(req: Request<()>) -> tide::Result {
|
2021-01-03 05:50:23 +01:00
|
|
|
let domains = vec![
|
2021-01-03 19:22:41 +01:00
|
|
|
// ICONS
|
|
|
|
"styles.redditmedia.com",
|
2021-01-04 03:23:57 +01:00
|
|
|
"www.redditstatic.com",
|
2021-01-03 05:50:23 +01:00
|
|
|
];
|
2020-11-30 03:50:29 +01:00
|
|
|
|
2021-02-09 18:38:52 +01:00
|
|
|
let decoded = decode(req.param("url").unwrap_or_default()).map(|bytes| String::from_utf8(bytes).unwrap_or_default());
|
2020-12-01 05:33:55 +01:00
|
|
|
|
2021-01-26 02:01:02 +01:00
|
|
|
match decoded {
|
2021-01-31 05:18:57 +01:00
|
|
|
Ok(media) => match Url::parse(media.as_str()) {
|
|
|
|
Ok(url) => {
|
2021-02-09 18:38:52 +01:00
|
|
|
if domains.contains(&url.domain().unwrap_or_default()) {
|
2021-02-18 19:04:59 +01:00
|
|
|
request(url.to_string()).await
|
2021-01-31 05:18:57 +01:00
|
|
|
} else {
|
2021-02-09 18:38:52 +01:00
|
|
|
Err(tide::Error::from_str(403, "Resource must be from Reddit"))
|
2021-01-03 05:50:23 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 18:38:52 +01:00
|
|
|
Err(_) => Err(tide::Error::from_str(400, "Can't parse base64 into URL")),
|
2021-01-31 05:18:57 +01:00
|
|
|
},
|
2021-02-09 18:38:52 +01:00
|
|
|
Err(_) => Err(tide::Error::from_str(400, "Can't decode base64")),
|
2020-11-24 01:57:37 +01:00
|
|
|
}
|
2020-11-30 03:50:29 +01:00
|
|
|
}
|
2021-02-18 19:04:59 +01:00
|
|
|
|
|
|
|
pub async fn video(req: Request<()>) -> tide::Result {
|
|
|
|
let id = req.param("id").unwrap_or_default();
|
|
|
|
let size = req.param("size").unwrap_or("720.mp4");
|
|
|
|
let url = format!("https://v.redd.it/{}/DASH_{}", id, size);
|
|
|
|
request(url).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn image(req: Request<()>) -> tide::Result {
|
|
|
|
let id = req.param("id").unwrap_or_default();
|
|
|
|
let url = format!("https://i.redd.it/{}", id);
|
|
|
|
request(url).await
|
|
|
|
}
|
|
|
|
|
2021-02-19 21:55:07 +01:00
|
|
|
pub async fn thumbnail(req: Request<()>) -> tide::Result {
|
|
|
|
let id = req.param("id").unwrap_or_default();
|
|
|
|
let point = req.param("point").unwrap_or_default();
|
|
|
|
let url = format!("https://{}.thumbs.redditmedia.com/{}", point, id);
|
|
|
|
request(url).await
|
|
|
|
}
|
|
|
|
|
2021-02-20 03:18:09 +01:00
|
|
|
pub async fn emoji(req: Request<()>) -> tide::Result {
|
|
|
|
let id = req.param("id").unwrap_or_default();
|
|
|
|
let name = req.param("name").unwrap_or_default();
|
|
|
|
let url = format!("https://emoji.redditmedia.com/{}/{}", id, name);
|
|
|
|
request(url).await
|
|
|
|
}
|
|
|
|
|
2021-02-20 05:50:55 +01:00
|
|
|
pub async fn preview(req: Request<()>) -> tide::Result {
|
|
|
|
let id = req.param("id").unwrap_or_default();
|
|
|
|
let query = req.param("query").unwrap_or_default();
|
|
|
|
let prefix = match req.param("location").unwrap_or_default() {
|
|
|
|
"ext" => "external-",
|
|
|
|
_ => ""
|
|
|
|
};
|
|
|
|
let url = format!("https://{}preview.redd.it/{}?{}", prefix, id, query);
|
|
|
|
request(url).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn style(req: Request<()>) -> tide::Result {
|
|
|
|
let path = req.param("path").unwrap_or_default();
|
|
|
|
let url = format!("https://styles.redditmedia.com/{}", path);
|
|
|
|
request(url).await
|
|
|
|
}
|
|
|
|
|
2021-02-18 19:04:59 +01:00
|
|
|
async fn request(url: String) -> tide::Result {
|
|
|
|
let http = surf::get(url).await.unwrap();
|
|
|
|
|
|
|
|
let content_length = http.header("Content-Length").map(|v| v.to_string()).unwrap_or_default();
|
|
|
|
let content_type = http.content_type().map(|m| m.to_string()).unwrap_or_default();
|
|
|
|
|
|
|
|
Ok(
|
|
|
|
Response::builder(http.status())
|
|
|
|
.body(Body::from_reader(http, None))
|
|
|
|
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
|
|
|
|
.header("Content-Length", content_length)
|
|
|
|
.header("Content-Type", content_type)
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
}
|