use SupportedMimeType for media_upload

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-02-20 18:09:52 -05:00
parent 490dedfaf1
commit e5fc461a79
3 changed files with 6 additions and 46 deletions

View File

@@ -50,7 +50,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 urls::{supported_mime_hosted_at_url, SupportedMimeType, UrlMimes};
pub use user_account::UserAccount;
// export libs

View File

@@ -214,7 +214,8 @@ impl UrlMimes {
}
}
struct SupportedMimeType {
#[derive(Debug)]
pub struct SupportedMimeType {
mime: mime_guess::Mime,
}
@@ -238,7 +239,6 @@ impl SupportedMimeType {
}
}
#[allow(unused)]
pub fn to_mime(&self) -> &str {
self.mime.essence_str()
}

View File

@@ -3,6 +3,7 @@ use std::path::PathBuf;
use base64::{prelude::BASE64_URL_SAFE, Engine};
use ehttp::Request;
use nostrdb::{Note, NoteBuilder};
use notedeck::SupportedMimeType;
use poll_promise::Promise;
use sha2::{Digest, Sha256};
use url::Url;
@@ -232,13 +233,13 @@ fn find_nip94_ev_in_json(json: String) -> Result<Nip94Event, Error> {
pub struct MediaPath {
full_path: PathBuf,
file_name: String,
media_type: SupportedMediaType,
media_type: SupportedMimeType,
}
impl MediaPath {
pub fn new(path: PathBuf) -> Result<Self, Error> {
if let Some(ex) = path.extension().and_then(|f| f.to_str()) {
let media_type = SupportedMediaType::from_extension(ex)?;
let media_type = SupportedMimeType::from_extension(ex)?;
let file_name = path
.file_name()
.and_then(|name| name.to_str())
@@ -259,47 +260,6 @@ impl MediaPath {
}
}
#[derive(Debug)]
pub enum SupportedMediaType {
Png,
Jpeg,
Webp,
}
impl SupportedMediaType {
pub fn mime_extension(&self) -> &str {
match &self {
SupportedMediaType::Png => "png",
SupportedMediaType::Jpeg => "jpeg",
SupportedMediaType::Webp => "webp",
}
}
pub fn to_mime(&self) -> String {
format!("{}/{}", self.mime_type(), self.mime_extension())
}
fn mime_type(&self) -> String {
match &self {
SupportedMediaType::Png | SupportedMediaType::Jpeg | SupportedMediaType::Webp => {
"image"
}
}
.to_string()
}
fn from_extension(ext: &str) -> Result<Self, Error> {
match ext.to_lowercase().as_str() {
"jpeg" | "jpg" => Ok(SupportedMediaType::Jpeg),
"png" => Ok(SupportedMediaType::Png),
"webp" => Ok(SupportedMediaType::Webp),
unsupported_type => Err(Error::Generic(format!(
"{unsupported_type} is not a valid file type to upload."
))),
}
}
}
#[derive(Clone, Debug, serde::Deserialize)]
pub struct Nip94Event {
pub url: String,