Proxy Thumbnails

This commit is contained in:
spikecodes 2020-11-30 21:10:08 -08:00
parent 759c9fc66b
commit f33af75267
5 changed files with 22 additions and 20 deletions

View File

@ -3,7 +3,7 @@ name = "libreddit"
description = " Alternative private front-end to Reddit" description = " Alternative private front-end to Reddit"
license = "AGPL-3.0" license = "AGPL-3.0"
repository = "https://github.com/spikecodes/libreddit" repository = "https://github.com/spikecodes/libreddit"
version = "0.1.8" version = "0.1.9"
authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"] authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"]
edition = "2018" edition = "2018"

View File

@ -1,13 +1,10 @@
// CRATES // CRATES
use crate::utils::{request, val, Comment, ErrorTemplate, Flair, Params, Post}; use crate::utils::{format_url, request, val, Comment, ErrorTemplate, Flair, Params, Post};
use actix_web::{get, http::StatusCode, web, HttpResponse, Result}; use actix_web::{get, http::StatusCode, web, HttpResponse, Result};
use askama::Template; use askama::Template;
use chrono::{TimeZone, Utc}; use chrono::{TimeZone, Utc};
use pulldown_cmark::{html, Options, Parser}; use pulldown_cmark::{html, Options, Parser};
#[cfg(feature = "proxy")]
use base64::encode;
// STRUCTS // STRUCTS
#[derive(Template)] #[derive(Template)]
#[template(path = "post.html", escape = "none")] #[template(path = "post.html", escape = "none")]
@ -69,14 +66,6 @@ async fn page(web::Path((_sub, id)): web::Path<(String, String)>, params: web::Q
} }
} }
async fn format_url(url: &str) -> String {
#[cfg(feature = "proxy")]
return "/imageproxy/".to_string() + encode(url).as_str();
#[cfg(not(feature = "proxy"))]
return url.to_string();
}
// UTILITIES // UTILITIES
async fn media(data: &serde_json::Value) -> (String, String) { async fn media(data: &serde_json::Value) -> (String, String) {
let post_type: &str; let post_type: &str;

View File

@ -17,8 +17,6 @@ async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
Err(_e) => return Ok(HttpResponse::Ok().body("")), Err(_e) => return Ok(HttpResponse::Ok().body("")),
}; };
dbg!(&media);
let client = Client::default(); let client = Client::default();
client client
.get(media.replace("&amp;", "&")) .get(media.replace("&amp;", "&"))

View File

@ -1,5 +1,5 @@
// CRATES // CRATES
use crate::utils::{fetch_posts, request, val, ErrorTemplate, Params, Post, Subreddit}; use crate::utils::{fetch_posts, format_url, request, val, ErrorTemplate, Params, Post, Subreddit};
use actix_web::{get, http::StatusCode, web, HttpResponse, Result}; use actix_web::{get, http::StatusCode, web, HttpResponse, Result};
use askama::Template; use askama::Template;
@ -88,7 +88,7 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
name: val(&res, "display_name").await, name: val(&res, "display_name").await,
title: val(&res, "title").await, title: val(&res, "title").await,
description: val(&res, "public_description").await, description: val(&res, "public_description").await,
icon: val(&res, "icon_img").await, icon: format_url(val(&res, "icon_img").await.as_str()).await,
members: if members > 1000 { format!("{}k", members / 1000) } else { members.to_string() }, members: if members > 1000 { format!("{}k", members / 1000) } else { members.to_string() },
active: if active > 1000 { format!("{}k", active / 1000) } else { active.to_string() }, active: if active > 1000 { format!("{}k", active / 1000) } else { active.to_string() },
}; };

View File

@ -5,6 +5,9 @@ use chrono::{TimeZone, Utc};
use serde_json::{from_str, Value}; use serde_json::{from_str, Value};
// use surf::{client, get, middleware::Redirect}; // use surf::{client, get, middleware::Redirect};
#[cfg(feature = "proxy")]
use base64::encode;
// //
// STRUCTS // STRUCTS
// //
@ -72,6 +75,18 @@ pub struct ErrorTemplate {
pub message: String, pub message: String,
} }
//
// URL HANDLING
//
pub async fn format_url(url: &str) -> String {
#[cfg(feature = "proxy")]
return "/imageproxy/".to_string() + encode(url).as_str();
#[cfg(not(feature = "proxy"))]
return url.to_string();
}
// //
// JSON PARSING // JSON PARSING
// //
@ -108,7 +123,7 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Pos
for post in post_list.iter() { for post in post_list.iter() {
let img = if val(post, "thumbnail").await.starts_with("https:/") { let img = if val(post, "thumbnail").await.starts_with("https:/") {
val(post, "thumbnail").await format_url(val(post, "thumbnail").await.as_str()).await
} else { } else {
String::new() String::new()
}; };