|
1 | 1 | use std::path::{Path, PathBuf}; |
2 | 2 |
|
3 | | -use crate::{ |
4 | | - download::{download_url, save_b64}, |
5 | | - error::OpenAIError, |
6 | | - types::{ |
7 | | - audio::{AudioInput, CreateSpeechResponse}, |
8 | | - chat::{Prompt, StopConfiguration}, |
9 | | - embeddings::EmbeddingInput, |
10 | | - files::FileInput, |
11 | | - images::{Image, ImageInput, ImagesResponse}, |
12 | | - moderations::ModerationInput, |
13 | | - InputSource, |
14 | | - }, |
15 | | - util::create_all_dir, |
| 3 | +use crate::types::{ |
| 4 | + audio::AudioInput, |
| 5 | + chat::{Prompt, StopConfiguration}, |
| 6 | + embeddings::EmbeddingInput, |
| 7 | + files::FileInput, |
| 8 | + images::ImageInput, |
| 9 | + moderations::ModerationInput, |
| 10 | + InputSource, |
16 | 11 | }; |
17 | 12 |
|
18 | 13 | use bytes::Bytes; |
@@ -146,71 +141,6 @@ impl_input!(AudioInput); |
146 | 141 | impl_input!(FileInput); |
147 | 142 | impl_input!(ImageInput); |
148 | 143 |
|
149 | | -impl ImagesResponse { |
150 | | - /// Save each image in a dedicated Tokio task and return paths to saved files. |
151 | | - /// For [ResponseFormat::Url] each file is downloaded in dedicated Tokio task. |
152 | | - pub async fn save<P: AsRef<Path>>(&self, dir: P) -> Result<Vec<PathBuf>, OpenAIError> { |
153 | | - create_all_dir(dir.as_ref())?; |
154 | | - |
155 | | - let mut handles = vec![]; |
156 | | - for id in self.data.clone() { |
157 | | - let dir_buf = PathBuf::from(dir.as_ref()); |
158 | | - handles.push(tokio::spawn(async move { id.save(dir_buf).await })); |
159 | | - } |
160 | | - |
161 | | - let results = futures::future::join_all(handles).await; |
162 | | - let mut errors = vec![]; |
163 | | - let mut paths = vec![]; |
164 | | - |
165 | | - for result in results { |
166 | | - match result { |
167 | | - Ok(inner) => match inner { |
168 | | - Ok(path) => paths.push(path), |
169 | | - Err(e) => errors.push(e), |
170 | | - }, |
171 | | - Err(e) => errors.push(OpenAIError::FileSaveError(e.to_string())), |
172 | | - } |
173 | | - } |
174 | | - |
175 | | - if errors.is_empty() { |
176 | | - Ok(paths) |
177 | | - } else { |
178 | | - Err(OpenAIError::FileSaveError( |
179 | | - errors |
180 | | - .into_iter() |
181 | | - .map(|e| e.to_string()) |
182 | | - .collect::<Vec<String>>() |
183 | | - .join("; "), |
184 | | - )) |
185 | | - } |
186 | | - } |
187 | | -} |
188 | | - |
189 | | -impl CreateSpeechResponse { |
190 | | - pub async fn save<P: AsRef<Path>>(&self, file_path: P) -> Result<(), OpenAIError> { |
191 | | - let dir = file_path.as_ref().parent(); |
192 | | - |
193 | | - if let Some(dir) = dir { |
194 | | - create_all_dir(dir)?; |
195 | | - } |
196 | | - |
197 | | - tokio::fs::write(file_path, &self.bytes) |
198 | | - .await |
199 | | - .map_err(|e| OpenAIError::FileSaveError(e.to_string()))?; |
200 | | - |
201 | | - Ok(()) |
202 | | - } |
203 | | -} |
204 | | - |
205 | | -impl Image { |
206 | | - async fn save<P: AsRef<Path>>(&self, dir: P) -> Result<PathBuf, OpenAIError> { |
207 | | - match self { |
208 | | - Image::Url { url, .. } => download_url(url, dir).await, |
209 | | - Image::B64Json { b64_json, .. } => save_b64(b64_json, dir).await, |
210 | | - } |
211 | | - } |
212 | | -} |
213 | | - |
214 | 144 | macro_rules! impl_from_for_integer_array { |
215 | 145 | ($from_typ:ty, $to_typ:ty) => { |
216 | 146 | impl<const N: usize> From<[$from_typ; N]> for $to_typ { |
|
0 commit comments