Individually proxy images and thumbnails

This commit is contained in:
spikecodes 2021-02-19 12:55:07 -08:00
parent e466be8946
commit c586de66ba
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB
4 changed files with 28 additions and 9 deletions

4
Cargo.lock generated
View File

@ -483,9 +483,9 @@ checksum = "3a4f925191b4367301851c6d99b09890311d74b0d43f274c0b34c86d308a3663"
[[package]]
name = "cc"
version = "1.0.66"
version = "1.0.67"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48"
checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd"
[[package]]
name = "cfg-if"

View File

@ -174,6 +174,7 @@ async fn main() -> tide::Result<()> {
app.at("/proxy/*url/").get(proxy::handler);
app.at("/vid/:id/:size/").get(proxy::video);
app.at("/img/:id/").get(proxy::image);
app.at("/thumb/:point/:id/").get(proxy::thumbnail);
// Browse user profile
app.at("/u/:name/").get(user::profile);

View File

@ -4,9 +4,6 @@ use tide::{Request, Response};
pub async fn handler(req: Request<()>) -> tide::Result {
let domains = vec![
// THUMBNAILS
"a.thumbs.redditmedia.com",
"b.thumbs.redditmedia.com",
// EMOJI
"emoji.redditmedia.com",
// ICONS
@ -15,8 +12,6 @@ pub async fn handler(req: Request<()>) -> tide::Result {
// PREVIEWS
"preview.redd.it",
"external-preview.redd.it",
// MEDIA
"i.redd.it",
];
let decoded = decode(req.param("url").unwrap_or_default()).map(|bytes| String::from_utf8(bytes).unwrap_or_default());
@ -49,6 +44,13 @@ pub async fn image(req: Request<()>) -> tide::Result {
request(url).await
}
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
}
async fn request(url: String) -> tide::Result {
let http = surf::get(url).await.unwrap();

View File

@ -188,15 +188,27 @@ pub fn format_url(url: &str) -> String {
} else {
let domain = Url::parse(url).map(|f| f.domain().unwrap_or_default().to_owned()).unwrap_or_default();
let capture = |regex: &str, format: &str| {
Regex::new(regex)
.map(|re| match re.captures(url) {
Some(caps) => [format, &caps[1], "/"].join(""),
None => String::new(),
})
.unwrap_or_default()
};
match domain.as_str() {
"v.redd.it" => {
let re = Regex::new(r"https://v\.redd\.it/(.*)/DASH_([0-9]{2,4}(\.mp4|$))").unwrap();
match re.captures(url) {
Some(caps) => format!("/vid/{}/{}", &caps[1], &caps[2]),
None => String::new()
None => String::new(),
}
}
"i.redd.it" => capture(r"https://i\.redd\.it/(.*)", "/img/"),
"a.thumbs.redditmedia.com" => capture(r"https://a\.thumbs\.redditmedia\.com/(.*)", "/thumb/a/"),
"b.thumbs.redditmedia.com" => capture(r"https://b\.thumbs\.redditmedia\.com/(.*)", "/thumb/b/"),
_ => format!("/proxy/{}/", encode(url).as_str()),
}
}
@ -244,7 +256,11 @@ pub async fn media(data: &Value) -> (String, Media, Vec<GalleryMedia>) {
} else {
// Return the picture if the media is an image
post_type = "image";
format_url(preview["source"]["url"].as_str().unwrap_or_default())
if data["domain"] == "i.redd.it" {
format_url(data["url"].as_str().unwrap_or_default())
} else {
format_url(preview["source"]["url"].as_str().unwrap_or_default())
}
}
} else if data["is_self"].as_bool().unwrap_or_default() {
// If type is self, return permalink