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"
license = "AGPL-3.0"
repository = "https://github.com/spikecodes/libreddit"
version = "0.1.8"
version = "0.1.9"
authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"]
edition = "2018"

View File

@ -1,13 +1,10 @@
// 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 askama::Template;
use chrono::{TimeZone, Utc};
use pulldown_cmark::{html, Options, Parser};
#[cfg(feature = "proxy")]
use base64::encode;
// STRUCTS
#[derive(Template)]
#[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
async fn media(data: &serde_json::Value) -> (String, String) {
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("")),
};
dbg!(&media);
let client = Client::default();
client
.get(media.replace("&amp;", "&"))

View File

@ -1,5 +1,5 @@
// 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 askama::Template;
@ -88,7 +88,7 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
name: val(&res, "display_name").await,
title: val(&res, "title").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() },
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 surf::{client, get, middleware::Redirect};
#[cfg(feature = "proxy")]
use base64::encode;
//
// STRUCTS
//
@ -72,6 +75,18 @@ pub struct ErrorTemplate {
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
//
@ -108,7 +123,7 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Pos
for post in post_list.iter() {
let img = if val(post, "thumbnail").await.starts_with("https:/") {
val(post, "thumbnail").await
format_url(val(post, "thumbnail").await.as_str()).await
} else {
String::new()
};
@ -166,14 +181,14 @@ pub async fn request(url: String) -> Result<serde_json::Value, &'static str> {
// let mut res = client.send(req).await.unwrap();
// let success = res.status().is_success();
// let body = res.body_string().await.unwrap();
// --- reqwest ---
let res = reqwest::get(&url).await.unwrap();
// Read the status from the response
let success = res.status().is_success();
// Read the body of the response
let body = res.text().await.unwrap();
dbg!(url.clone());
// Parse the response from Reddit as JSON