use reqwest::Body;
use std::{
borrow::Cow,
path::Path,
};
use tokio_util::codec::{
BytesCodec,
FramedRead,
};
pub enum Image {
Url(String),
File { name: String, body: Body },
}
impl Image {
pub async fn from_path(path: &Path) -> std::io::Result<Self> {
let name = path
.file_name()
.map(|name| name.to_string_lossy())
.unwrap_or(Cow::Borrowed("file.png"))
.into();
let file = tokio::fs::File::open(path).await?;
Self::from_file(name, file)
}
pub fn from_file(name: String, file: tokio::fs::File) -> std::io::Result<Self> {
let stream = FramedRead::new(file, BytesCodec::new());
let body = reqwest::Body::wrap_stream(stream);
Ok(Self::File { name, body })
}
}
impl From<String> for Image {
fn from(url: String) -> Self {
Image::Url(url)
}
}
impl From<&str> for Image {
fn from(url: &str) -> Self {
Image::Url(url.into())
}
}