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 timecache::TimeCached;
|
||||
pub use unknowns::{get_unknown_note_ids, NoteRefsUnkIdAction, SingleUnkIdAction, UnknownIds};
|
||||
pub use urls::{supported_mime_hosted_at_url, UrlMimes};
|
||||
pub use user_account::UserAccount;
|
||||
|
||||
// export libs
|
||||
|
||||
@@ -9,6 +9,7 @@ use std::{
|
||||
|
||||
use egui::TextBuffer;
|
||||
use poll_promise::Promise;
|
||||
use url::Url;
|
||||
|
||||
use crate::Error;
|
||||
|
||||
@@ -218,7 +219,6 @@ struct SupportedMimeType {
|
||||
}
|
||||
|
||||
impl SupportedMimeType {
|
||||
#[allow(unused)]
|
||||
pub fn from_extension(extension: &str) -> Result<Self, Error> {
|
||||
if let Some(mime) = mime_guess::from_ext(extension)
|
||||
.first()
|
||||
@@ -239,3 +239,42 @@ impl SupportedMimeType {
|
||||
fn is_mime_supported(mime: &mime_guess::Mime) -> bool {
|
||||
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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user