reqwest_builder/
file_upload.rs

1use crate::errors::ReqwestBuilderError;
2use serde::{Deserialize, Serialize};
3use std::path::Path;
4
5/// File data for upload
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
7pub struct FileUpload {
8    pub filename: String,
9    #[serde(skip)] // Don't serialize file content
10    pub content: Vec<u8>,
11    #[serde(skip)] // Don't serialize mime type
12    pub mime_type: Option<String>,
13}
14
15impl FileUpload {
16    /// Create a new file upload from file path
17    pub fn from_path<P: AsRef<Path>>(path: P) -> std::result::Result<Self, ReqwestBuilderError> {
18        let path = path.as_ref();
19        let content = std::fs::read(path)?;
20        let filename = path
21            .file_name()
22            .and_then(|name| name.to_str())
23            .unwrap_or("file")
24            .to_string();
25
26        let mime_type = mime_guess::from_path(path)
27            .first()
28            .map(|mime| mime.to_string());
29
30        Ok(Self {
31            filename,
32            content,
33            mime_type,
34        })
35    }
36
37    /// Create a new file upload from bytes
38    pub fn from_bytes(filename: String, content: Vec<u8>, mime_type: Option<String>) -> Self {
39        Self {
40            filename,
41            content,
42            mime_type,
43        }
44    }
45}