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
This commit is contained in:
parent
67a890cab3
commit
8a917fcde3
43
src/utils.rs
43
src/utils.rs
@ -169,6 +169,7 @@ pub struct Media {
|
|||||||
pub width: i64,
|
pub width: i64,
|
||||||
pub height: i64,
|
pub height: i64,
|
||||||
pub poster: String,
|
pub poster: String,
|
||||||
|
pub download_name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Media {
|
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 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(),
|
post_type.to_string(),
|
||||||
Self {
|
Self {
|
||||||
@ -245,6 +255,7 @@ impl Media {
|
|||||||
width: source["width"].as_i64().unwrap_or_default(),
|
width: source["width"].as_i64().unwrap_or_default(),
|
||||||
height: source["height"].as_i64().unwrap_or_default(),
|
height: source["height"].as_i64().unwrap_or_default(),
|
||||||
poster: format_url(source["url"].as_str().unwrap_or_default()),
|
poster: format_url(source["url"].as_str().unwrap_or_default()),
|
||||||
|
download_name,
|
||||||
},
|
},
|
||||||
gallery,
|
gallery,
|
||||||
)
|
)
|
||||||
@ -389,6 +400,7 @@ impl Post {
|
|||||||
width: data["thumbnail_width"].as_i64().unwrap_or_default(),
|
width: data["thumbnail_width"].as_i64().unwrap_or_default(),
|
||||||
height: data["thumbnail_height"].as_i64().unwrap_or_default(),
|
height: data["thumbnail_height"].as_i64().unwrap_or_default(),
|
||||||
poster: String::new(),
|
poster: String::new(),
|
||||||
|
download_name: String::new(),
|
||||||
},
|
},
|
||||||
media,
|
media,
|
||||||
domain: val(post, "domain"),
|
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(),
|
width: post["data"]["thumbnail_width"].as_i64().unwrap_or_default(),
|
||||||
height: post["data"]["thumbnail_height"].as_i64().unwrap_or_default(),
|
height: post["data"]["thumbnail_height"].as_i64().unwrap_or_default(),
|
||||||
poster: String::new(),
|
poster: String::new(),
|
||||||
|
download_name: String::new(),
|
||||||
},
|
},
|
||||||
flair: Flair {
|
flair: Flair {
|
||||||
flair_parts: FlairPart::parse(
|
flair_parts: FlairPart::parse(
|
||||||
@ -1110,6 +1123,20 @@ pub async fn nsfw_landing(req: Request<Body>, req_url: String) -> Result<Respons
|
|||||||
Ok(Response::builder().status(403).header("content-type", "text/html").body(body.into()).unwrap_or_default())
|
Ok(Response::builder().status(403).header("content-type", "text/html").body(body.into()).unwrap_or_default())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the last (non-empty) segment of a path string
|
||||||
|
pub fn url_path_basename(path: &str) -> 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{format_num, format_url, rewrite_urls};
|
use super::{format_num, format_url, rewrite_urls};
|
||||||
@ -1218,3 +1245,19 @@ fn test_rewriting_image_links() {
|
|||||||
let output = r#"<p><figure><a href="/preview/pre/6awags382xo31.png?width=2560&format=png&auto=webp&s=9c563aed4f07a91bdd249b5a3cea43a79710dcfc"><img loading="lazy" src="/preview/pre/6awags382xo31.png?width=2560&format=png&auto=webp&s=9c563aed4f07a91bdd249b5a3cea43a79710dcfc"></a><figcaption>caption 1</figcaption></figure></p"#;
|
let output = r#"<p><figure><a href="/preview/pre/6awags382xo31.png?width=2560&format=png&auto=webp&s=9c563aed4f07a91bdd249b5a3cea43a79710dcfc"><img loading="lazy" src="/preview/pre/6awags382xo31.png?width=2560&format=png&auto=webp&s=9c563aed4f07a91bdd249b5a3cea43a79710dcfc"></a><figcaption>caption 1</figcaption></figure></p"#;
|
||||||
assert_eq!(rewrite_urls(input), output);
|
assert_eq!(rewrite_urls(input), output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_url_path_basename() {
|
||||||
|
// without trailing slash
|
||||||
|
assert_eq!(url_path_basename("/first/last"), "last");
|
||||||
|
// with trailing slash
|
||||||
|
assert_eq!(url_path_basename("/first/last/"), "last");
|
||||||
|
// with query parameters
|
||||||
|
assert_eq!(url_path_basename("/first/last/?some=query"), "last");
|
||||||
|
// file path
|
||||||
|
assert_eq!(url_path_basename("/cdn/image.jpg"), "image.jpg");
|
||||||
|
// when a full url is passed instead of just a path
|
||||||
|
assert_eq!(url_path_basename("https://doma.in/first/last"), "last");
|
||||||
|
// empty path
|
||||||
|
assert_eq!(url_path_basename("/"), "");
|
||||||
|
}
|
||||||
|
@ -1110,12 +1110,12 @@ a.search_subreddit:hover {
|
|||||||
margin-right: 15px;
|
margin-right: 15px;
|
||||||
}
|
}
|
||||||
|
|
||||||
#post_links > li.desktop_item {
|
.desktop_item {
|
||||||
display: auto;
|
display: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media screen and (min-width: 481px) {
|
@media screen and (min-width: 481px) {
|
||||||
#post_links > li.mobile_item {
|
.mobile_item {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1770,10 +1770,11 @@ td, th {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#post_links > li { margin-right: 10px }
|
#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 }
|
.post_footer > p > span#upvoted { display: none }
|
||||||
|
|
||||||
|
.desktop_item { display: none }
|
||||||
|
.mobile_item { display: auto }
|
||||||
|
|
||||||
.popup {
|
.popup {
|
||||||
width: auto;
|
width: auto;
|
||||||
}
|
}
|
||||||
|
@ -164,13 +164,28 @@
|
|||||||
<span class="label"> Upvotes</span></div>
|
<span class="label"> Upvotes</span></div>
|
||||||
<div class="post_footer">
|
<div class="post_footer">
|
||||||
<ul id="post_links">
|
<ul id="post_links">
|
||||||
<li class="desktop_item"><a href="{{ post.permalink }}">permalink</a></li>
|
<li>
|
||||||
<li class="mobile_item"><a href="{{ post.permalink }}">link</a></li>
|
<a href="{{ post.permalink }}">
|
||||||
|
<span class="desktop_item">perma</span>link
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
{% if post.num_duplicates > 0 %}
|
{% if post.num_duplicates > 0 %}
|
||||||
<li class="desktop_item"><a href="/r/{{ post.community }}/duplicates/{{ post.id }}">duplicates</a></li>
|
<li>
|
||||||
<li class="mobile_item"><a href="/r/{{ post.community }}/duplicates/{{ post.id }}">dupes</a></li>
|
<a href="/r/{{ post.community }}/duplicates/{{ post.id }}">
|
||||||
|
dup<span class="desktop_item">licat</span>es
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% call external_reddit_link(post.permalink) %}
|
{% call external_reddit_link(post.permalink) %}
|
||||||
|
|
||||||
|
{% if post.media.download_name != "" %}
|
||||||
|
<li>
|
||||||
|
<a href="{{ post.media.url }}" download="{{ post.media.download_name }}">
|
||||||
|
<span class="mobile_item">dl</span>
|
||||||
|
<span class="desktop_item">download</span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
{% endif %}
|
||||||
</ul>
|
</ul>
|
||||||
<p>{{ post.upvote_ratio }}%<span id="upvoted"> Upvoted</span></p>
|
<p>{{ post.upvote_ratio }}%<span id="upvoted"> Upvoted</span></p>
|
||||||
</div>
|
</div>
|
||||||
@ -178,8 +193,7 @@
|
|||||||
{%- endmacro %}
|
{%- endmacro %}
|
||||||
|
|
||||||
{% macro external_reddit_link(permalink) %}
|
{% macro external_reddit_link(permalink) %}
|
||||||
{% for dev_type in ["desktop", "mobile"] %}
|
<li>
|
||||||
<li class="{{ dev_type }}_item">
|
|
||||||
<a
|
<a
|
||||||
{% if prefs.disable_visit_reddit_confirmation != "on" %}
|
{% if prefs.disable_visit_reddit_confirmation != "on" %}
|
||||||
href="#popup"
|
href="#popup"
|
||||||
@ -193,7 +207,6 @@
|
|||||||
{% call visit_reddit_confirmation(permalink) %}
|
{% call visit_reddit_confirmation(permalink) %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</li>
|
</li>
|
||||||
{% endfor %}
|
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
{% macro post_in_list(post) -%}
|
{% macro post_in_list(post) -%}
|
||||||
|
Loading…
Reference in New Issue
Block a user