Individually proxy videos

This commit is contained in:
spikecodes 2021-02-18 10:04:59 -08:00
parent 2f4deb221a
commit 58127b17d8
No known key found for this signature in database
GPG key ID: 004CECFF9B463BCB
5 changed files with 67 additions and 54 deletions

View file

@ -17,7 +17,6 @@ pub async fn handler(req: Request<()>) -> tide::Result {
"external-preview.redd.it",
// MEDIA
"i.redd.it",
"v.redd.it",
];
let decoded = decode(req.param("url").unwrap_or_default()).map(|bytes| String::from_utf8(bytes).unwrap_or_default());
@ -26,19 +25,7 @@ pub async fn handler(req: Request<()>) -> tide::Result {
Ok(media) => match Url::parse(media.as_str()) {
Ok(url) => {
if domains.contains(&url.domain().unwrap_or_default()) {
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(),
)
request(url.to_string()).await
} else {
Err(tide::Error::from_str(403, "Resource must be from Reddit"))
}
@ -48,3 +35,32 @@ pub async fn handler(req: Request<()>) -> tide::Result {
Err(_) => Err(tide::Error::from_str(400, "Can't decode base64")),
}
}
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
}
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(),
)
}