use std::path::Path;
use reqwest::Body;
use tokio_util::codec::{BytesCodec, FramedRead};
use crate::error::OpenAIError;
pub(crate) async fn file_stream_body<P: AsRef<Path>>(path: P) -> Result<Body, OpenAIError> {
let file = tokio::fs::File::open(path.as_ref())
.await
.map_err(|e| OpenAIError::FileReadError(e.to_string()))?;
let stream = FramedRead::new(file, BytesCodec::new());
let body = Body::wrap_stream(stream);
Ok(body)
}
pub(crate) async fn create_file_part<P: AsRef<Path>>(
path: P,
) -> Result<reqwest::multipart::Part, OpenAIError> {
let file_name = path
.as_ref()
.file_name()
.ok_or_else(|| {
OpenAIError::FileReadError(format!(
"cannot extract file name from {}",
path.as_ref().display()
))
})?
.to_str()
.unwrap()
.to_string();
let file_part = reqwest::multipart::Part::stream(file_stream_body(path.as_ref()).await?)
.file_name(file_name)
.mime_str("application/octet-stream")
.unwrap();
Ok(file_part)
}
pub(crate) fn create_all_dir<P: AsRef<Path>>(dir: P) -> Result<(), OpenAIError> {
let exists = match Path::try_exists(dir.as_ref()) {
Ok(exists) => exists,
Err(e) => return Err(OpenAIError::FileSaveError(e.to_string())),
};
if !exists {
std::fs::create_dir_all(dir.as_ref())
.map_err(|e| OpenAIError::FileSaveError(e.to_string()))?;
}
Ok(())
}