From 8a917fcde3f933107ad2899186d4026de6dd8114 Mon Sep 17 00:00:00 2001 From: Pim Date: Fri, 5 Jul 2024 03:32:12 +0200 Subject: [PATCH] feat: add download button on image/gif/video posts (#173) * feat: add download button on image/gif/video posts * chore: fix formatting * chore: dont create reference --- src/utils.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ static/style.css | 9 +++++---- templates/utils.html | 27 ++++++++++++++++++++------- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 5e8d83c..b11096f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -169,6 +169,7 @@ pub struct Media { pub width: i64, pub height: i64, pub poster: String, + pub download_name: String, } impl Media { @@ -235,6 +236,15 @@ impl Media { let alt_url = alt_url_val.map_or(String::new(), |val| format_url(val.as_str().unwrap_or_default())); + let download_name = if post_type == "image" || post_type == "gif" || post_type == "video" { + let permalink_base = url_path_basename(data["permalink"].as_str().unwrap_or_default()); + let media_url_base = url_path_basename(url_val.as_str().unwrap_or_default()); + + format!("redlib_{permalink_base}_{media_url_base}") + } else { + String::new() + }; + ( post_type.to_string(), Self { @@ -245,6 +255,7 @@ impl Media { width: source["width"].as_i64().unwrap_or_default(), height: source["height"].as_i64().unwrap_or_default(), poster: format_url(source["url"].as_str().unwrap_or_default()), + download_name, }, gallery, ) @@ -389,6 +400,7 @@ impl Post { width: data["thumbnail_width"].as_i64().unwrap_or_default(), height: data["thumbnail_height"].as_i64().unwrap_or_default(), poster: String::new(), + download_name: String::new(), }, media, domain: val(post, "domain"), @@ -727,6 +739,7 @@ pub async fn parse_post(post: &Value) -> Post { width: post["data"]["thumbnail_width"].as_i64().unwrap_or_default(), height: post["data"]["thumbnail_height"].as_i64().unwrap_or_default(), poster: String::new(), + download_name: String::new(), }, flair: Flair { flair_parts: FlairPart::parse( @@ -1110,6 +1123,20 @@ pub async fn nsfw_landing(req: Request, req_url: String) -> Result String { + let url_result = Url::parse(format!("https://libredd.it/{path}").as_str()); + + if url_result.is_err() { + path.to_string() + } else { + let mut url = url_result.unwrap(); + url.path_segments_mut().unwrap().pop_if_empty(); + + url.path_segments().unwrap().last().unwrap().to_string() + } +} + #[cfg(test)] mod tests { use super::{format_num, format_url, rewrite_urls}; @@ -1218,3 +1245,19 @@ fn test_rewriting_image_links() { let output = r#"

caption 1
li.desktop_item { +.desktop_item { display: auto; } @media screen and (min-width: 481px) { - #post_links > li.mobile_item { + .mobile_item { display: none; } } @@ -1770,10 +1770,11 @@ td, th { } #post_links > li { margin-right: 10px } - #post_links > li.desktop_item { display: none } - #post_links > li.mobile_item { display: auto } .post_footer > p > span#upvoted { display: none } + .desktop_item { display: none } + .mobile_item { display: auto } + .popup { width: auto; } diff --git a/templates/utils.html b/templates/utils.html index 8edb55b..e1d317a 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -164,13 +164,28 @@ Upvotes @@ -178,8 +193,7 @@ {%- endmacro %} {% macro external_reddit_link(permalink) %} -{% for dev_type in ["desktop", "mobile"] %} -
  • +
  • -{% endfor %} {% endmacro %} {% macro post_in_list(post) -%}