use crate::errors::ReqwestBuilderError;
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct FileUpload {
pub filename: String,
#[serde(skip)] pub content: Vec<u8>,
#[serde(skip)] pub mime_type: Option<String>,
}
impl FileUpload {
pub fn from_path<P: AsRef<Path>>(path: P) -> std::result::Result<Self, ReqwestBuilderError> {
let path = path.as_ref();
let content = std::fs::read(path)?;
let filename = path
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("file")
.to_string();
let mime_type = mime_guess::from_path(path)
.first()
.map(|mime| mime.to_string());
Ok(Self {
filename,
content,
mime_type,
})
}
pub fn from_bytes(filename: String, content: Vec<u8>, mime_type: Option<String>) -> Self {
Self {
filename,
content,
mime_type,
}
}
}