URL mime hosted completeness
Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
@@ -49,6 +49,7 @@ pub use theme::ColorTheme;
|
|||||||
pub use time::time_ago_since;
|
pub use time::time_ago_since;
|
||||||
pub use timecache::TimeCached;
|
pub use timecache::TimeCached;
|
||||||
pub use unknowns::{get_unknown_note_ids, NoteRefsUnkIdAction, SingleUnkIdAction, UnknownIds};
|
pub use unknowns::{get_unknown_note_ids, NoteRefsUnkIdAction, SingleUnkIdAction, UnknownIds};
|
||||||
|
pub use urls::{supported_mime_hosted_at_url, UrlMimes};
|
||||||
pub use user_account::UserAccount;
|
pub use user_account::UserAccount;
|
||||||
|
|
||||||
// export libs
|
// export libs
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use std::{
|
|||||||
|
|
||||||
use egui::TextBuffer;
|
use egui::TextBuffer;
|
||||||
use poll_promise::Promise;
|
use poll_promise::Promise;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
|
|
||||||
@@ -218,7 +219,6 @@ struct SupportedMimeType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SupportedMimeType {
|
impl SupportedMimeType {
|
||||||
#[allow(unused)]
|
|
||||||
pub fn from_extension(extension: &str) -> Result<Self, Error> {
|
pub fn from_extension(extension: &str) -> Result<Self, Error> {
|
||||||
if let Some(mime) = mime_guess::from_ext(extension)
|
if let Some(mime) = mime_guess::from_ext(extension)
|
||||||
.first()
|
.first()
|
||||||
@@ -239,3 +239,42 @@ impl SupportedMimeType {
|
|||||||
fn is_mime_supported(mime: &mime_guess::Mime) -> bool {
|
fn is_mime_supported(mime: &mime_guess::Mime) -> bool {
|
||||||
mime.type_() == mime_guess::mime::IMAGE
|
mime.type_() == mime_guess::mime::IMAGE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn url_has_supported_mime(url: &str) -> MimeHostedAtUrl {
|
||||||
|
if let Ok(url) = Url::parse(url) {
|
||||||
|
if let Some(path) = url.path_segments() {
|
||||||
|
if let Some(file_name) = path.last() {
|
||||||
|
if let Some(ext) = std::path::Path::new(file_name)
|
||||||
|
.extension()
|
||||||
|
.and_then(|ext| ext.to_str())
|
||||||
|
{
|
||||||
|
if SupportedMimeType::from_extension(ext).is_ok() {
|
||||||
|
return MimeHostedAtUrl::Yes;
|
||||||
|
} else {
|
||||||
|
return MimeHostedAtUrl::No;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MimeHostedAtUrl::Maybe
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn supported_mime_hosted_at_url(urls: &mut UrlMimes, url: &str) -> bool {
|
||||||
|
match url_has_supported_mime(url) {
|
||||||
|
MimeHostedAtUrl::Yes => true,
|
||||||
|
MimeHostedAtUrl::Maybe => urls
|
||||||
|
.get(url)
|
||||||
|
.and_then(|s| s.parse::<mime_guess::mime::Mime>().ok())
|
||||||
|
.map_or(false, |mime: mime_guess::mime::Mime| {
|
||||||
|
is_mime_supported(&mime)
|
||||||
|
}),
|
||||||
|
MimeHostedAtUrl::No => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum MimeHostedAtUrl {
|
||||||
|
Yes,
|
||||||
|
Maybe,
|
||||||
|
No,
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use egui::{Color32, Hyperlink, Image, RichText};
|
|||||||
use nostrdb::{BlockType, Mention, Ndb, Note, NoteKey, Transaction};
|
use nostrdb::{BlockType, Mention, Ndb, Note, NoteKey, Transaction};
|
||||||
use tracing::warn;
|
use tracing::warn;
|
||||||
|
|
||||||
use notedeck::{Images, NoteCache};
|
use notedeck::{supported_mime_hosted_at_url, Images, NoteCache};
|
||||||
|
|
||||||
pub struct NoteContents<'a> {
|
pub struct NoteContents<'a> {
|
||||||
ndb: &'a Ndb,
|
ndb: &'a Ndb,
|
||||||
@@ -127,10 +127,6 @@ pub fn render_note_preview(
|
|||||||
.inner
|
.inner
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_image_link(url: &str) -> bool {
|
|
||||||
url.ends_with("png") || url.ends_with("jpg") || url.ends_with("jpeg")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn render_note_contents(
|
fn render_note_contents(
|
||||||
ui: &mut egui::Ui,
|
ui: &mut egui::Ui,
|
||||||
@@ -212,9 +208,12 @@ fn render_note_contents(
|
|||||||
}
|
}
|
||||||
|
|
||||||
BlockType::Url => {
|
BlockType::Url => {
|
||||||
let lower_url = block.as_str().to_lowercase();
|
if !hide_media {
|
||||||
if !hide_media && is_image_link(&lower_url) {
|
let url = block.as_str().to_string();
|
||||||
images.push(block.as_str().to_string());
|
|
||||||
|
if supported_mime_hosted_at_url(&mut img_cache.urls, &url) {
|
||||||
|
images.push(url);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
#[cfg(feature = "profiling")]
|
#[cfg(feature = "profiling")]
|
||||||
puffin::profile_scope!("url contents");
|
puffin::profile_scope!("url contents");
|
||||||
|
|||||||
Reference in New Issue
Block a user