mirror of
https://github.com/redlib-org/redlib.git
synced 2025-06-08 07:37:45 +00:00
Use base64 for encoding & Upgrade Media Handling
This commit is contained in:
parent
9a6430656d
commit
9d78266494
7 changed files with 90 additions and 76 deletions
48
src/post.rs
48
src/post.rs
|
@ -6,7 +6,7 @@ use chrono::{TimeZone, Utc};
|
|||
use pulldown_cmark::{html, Options, Parser};
|
||||
|
||||
#[cfg(feature = "proxy")]
|
||||
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
|
||||
use base64::encode;
|
||||
|
||||
// STRUCTS
|
||||
#[derive(Template)]
|
||||
|
@ -71,39 +71,32 @@ 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 utf8_percent_encode(url, NON_ALPHANUMERIC).to_string();
|
||||
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 {
|
||||
let post_hint: &str = data["data"]["post_hint"].as_str().unwrap_or("");
|
||||
let has_media: bool = data["data"]["media"].is_object();
|
||||
|
||||
let prefix = if cfg!(feature = "proxy") { "/imageproxy/" } else { "" };
|
||||
|
||||
let media: String = if !has_media {
|
||||
format!(r#"<h4 class="post_body"><a href="{u}">{u}</a></h4>"#, u = data["data"]["url"].as_str().unwrap())
|
||||
async fn media(data: &serde_json::Value) -> (String, String) {
|
||||
let post_type: &str;
|
||||
let url = if !data["preview"]["reddit_video_preview"]["fallback_url"].is_null() {
|
||||
post_type = "video";
|
||||
format_url(data["preview"]["reddit_video_preview"]["fallback_url"].as_str().unwrap()).await
|
||||
} else if !data["secure_media"]["reddit_video"]["fallback_url"].is_null() {
|
||||
post_type = "video";
|
||||
format_url(data["secure_media"]["reddit_video"]["fallback_url"].as_str().unwrap()).await
|
||||
} else if data["post_hint"].as_str().unwrap_or("") == "image" {
|
||||
post_type = "image";
|
||||
format_url(data["preview"]["images"][0]["source"]["url"].as_str().unwrap()).await
|
||||
} else {
|
||||
format!(
|
||||
r#"<img class="post_image" src="{}{}.png"/>"#,
|
||||
prefix,
|
||||
format_url(data["data"]["url"].as_str().unwrap()).await
|
||||
)
|
||||
post_type = "link";
|
||||
data["url"].as_str().unwrap().to_string()
|
||||
};
|
||||
|
||||
match post_hint {
|
||||
"hosted:video" => format!(
|
||||
r#"<video class="post_image" src="{}{}" controls/>"#,
|
||||
prefix,
|
||||
format_url(data["data"]["media"]["reddit_video"]["fallback_url"].as_str().unwrap()).await
|
||||
),
|
||||
"image" => format!(r#"<img class="post_image" src="{}{}"/>"#, prefix, format_url(data["data"]["url"].as_str().unwrap()).await),
|
||||
"self" => String::from(""),
|
||||
_ => media,
|
||||
}
|
||||
dbg!(post_type, url.to_string());
|
||||
|
||||
(post_type.to_string(), url)
|
||||
}
|
||||
|
||||
async fn markdown_to_html(md: &str) -> String {
|
||||
|
@ -127,6 +120,8 @@ async fn parse_post(json: serde_json::Value) -> Result<Post, &'static str> {
|
|||
let unix_time: i64 = post_data["data"]["created_utc"].as_f64().unwrap().round() as i64;
|
||||
let score = post_data["data"]["score"].as_i64().unwrap();
|
||||
|
||||
let media = media(&post_data["data"]).await;
|
||||
|
||||
let post = Post {
|
||||
title: val(post_data, "title").await,
|
||||
community: val(post_data, "subreddit").await,
|
||||
|
@ -134,7 +129,8 @@ async fn parse_post(json: serde_json::Value) -> Result<Post, &'static str> {
|
|||
author: val(post_data, "author").await,
|
||||
url: val(post_data, "permalink").await,
|
||||
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
|
||||
media: media(post_data).await,
|
||||
post_type: media.0,
|
||||
media: media.1,
|
||||
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
|
||||
flair: Flair(
|
||||
val(post_data, "link_flair_text").await,
|
||||
|
|
15
src/proxy.rs
15
src/proxy.rs
|
@ -1,22 +1,27 @@
|
|||
use actix_web::{client::Client, get, web, Error, HttpResponse, Result};
|
||||
|
||||
#[cfg(feature = "proxy")]
|
||||
use percent_encoding::percent_decode_str;
|
||||
use base64::decode;
|
||||
|
||||
#[get("/imageproxy/{url:.*}")]
|
||||
async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
|
||||
if cfg!(feature = "proxy") {
|
||||
#[cfg(feature = "proxy")]
|
||||
let media: String = percent_decode_str(url.as_str()).decode_utf8()?.to_string();
|
||||
let media: String;
|
||||
|
||||
#[cfg(not(feature = "proxy"))]
|
||||
let media: String = url;
|
||||
let media = url;
|
||||
|
||||
#[cfg(feature = "proxy")]
|
||||
match decode(url) {
|
||||
Ok(bytes) => media = String::from_utf8(bytes).unwrap(),
|
||||
Err(_e) => return Ok(HttpResponse::Ok().body("")),
|
||||
};
|
||||
|
||||
dbg!(&media);
|
||||
|
||||
let client = Client::default();
|
||||
client
|
||||
.get(media)
|
||||
.get(media.replace("&", "&"))
|
||||
.send()
|
||||
.await
|
||||
.map_err(Error::from)
|
||||
|
|
|
@ -21,6 +21,7 @@ pub struct Post {
|
|||
pub author: String,
|
||||
pub url: String,
|
||||
pub score: String,
|
||||
pub post_type: String,
|
||||
pub media: String,
|
||||
pub time: String,
|
||||
pub flair: Flair,
|
||||
|
@ -121,6 +122,7 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Pos
|
|||
body: val(post, "body").await,
|
||||
author: val(post, "author").await,
|
||||
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
|
||||
post_type: "link".to_string(),
|
||||
media: img,
|
||||
url: val(post, "permalink").await,
|
||||
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue