Downloads a file from a URI and saves it to a temporary location. # Arguments `uri` - A string slice that holds the URI of the file. # Returns `PathBuf` - The path to the downloaded file.
(uri: &str)
| 19 | /// |
| 20 | /// * `PathBuf` - The path to the downloaded file. |
| 21 | pub async fn download_file(uri: &str) -> std::io::Result<PathBuf> { |
| 22 | let response = reqwest::get(uri).await.expect("Failed to download file"); |
| 23 | let temp_file = tempfile::NamedTempFile::new()?; |
| 24 | let temp_path = temp_file.path().to_owned(); |
| 25 | let mut file = File::create(&temp_path)?; |
| 26 | let bytes = response.bytes().await.expect("Failed to read response"); |
| 27 | std::io::copy(&mut bytes.as_ref(), &mut file)?; |
| 28 | |
| 29 | // Get a random filename in the temp directory using timestamp |
| 30 | let temp_dir = std::env::temp_dir(); |
| 31 | let filename = temp_path |
| 32 | .file_name() |
| 33 | .and_then(|s| s.to_str()) |
| 34 | .unwrap_or("stack"); |
| 35 | let final_path = temp_dir.join(filename); |
| 36 | |
| 37 | // Persist the file to the new location |
| 38 | temp_file.persist(&final_path)?; |
| 39 | Ok(final_path) |
| 40 | } |
| 41 | |
| 42 | // /// Inflates a stack bundle file into a temporary directory. |
| 43 | // pub fn inflate_bundle(stack_file: &Path) -> Result<PathBuf> { |
no outgoing calls
no test coverage detected