use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::error::Error;
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FileUploadInput {
pub name: Option<String>,
pub filename: Option<String>,
pub content_type: Option<String>,
pub data: Vec<u8>,
}
impl FileUploadInput {
pub fn consume(self) -> Result<(Option<String>, FileUpload, Vec<u8>), Error> {
let mime_type = infer::get(&self.data).map(|t| t.mime_type().to_string());
return Ok((
self.name,
FileUpload::new(
uuid::Uuid::new_v4(),
self.filename,
self.content_type,
mime_type,
),
self.data,
));
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct FileUpload {
id: String,
filename: Option<String>,
content_type: Option<String>,
mime_type: Option<String>,
}
impl FileUpload {
pub fn new(
id: Uuid,
filename: Option<String>,
content_type: Option<String>,
mime_type: Option<String>,
) -> Self {
Self {
id: id.to_string(),
filename,
content_type,
mime_type,
}
}
pub fn path(&self) -> &str {
&self.id
}
pub fn content_type(&self) -> Option<&str> {
self.content_type.as_deref()
}
pub fn original_filename(&self) -> Option<&str> {
self.filename.as_deref()
}
}
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct FileUploads(pub Vec<FileUpload>);