/// Make a GET HTTP request to `url`, stream the response body to
/// `file_path` and verify that the resulting file has hash
/// `expected_sha256_hex`.
///
/// Returns immediately if the file already exists with the given hash.
///
/// Deletes an existing file with an incorrect file hash or no
/// hash is given to this function.
///
/// Since existing files get deleted if they have an incorrect hash,
/// this code will also work if a crash happens throughout execution
/// leading to inconsistent data.


Returns immediately if the file already exists with the given hash.

Deletes an existing file with an incorrect file hash or no
hash is given to this function.

Since existing files get deleted if they have an incorrect hash,
this code will also work if a crash happens throughout execution
leading to inconsistent data.
Returns immediately if the file already exists with the given hash . Deletes an existing file with an incorrect file hash or no hash is given to this function . Since existing files get deleted if they have an incorrect hash this code will also work if a crash happens throughout execution leading to inconsistent data .
pub async fn download_file(
        &self,
        url: &str,
        file_path: &Path,
        expected_sha256_hex: Option<String>,
    ) -> FileDownloadResult<()> {
        // In case the file already exists, check the file hash.
        if file_path.exists() {
            if let Some(expected_hash) = expected_sha256_hex.as_ref() {
                match check_file_hash(file_path, expected_hash) {
                    Ok(()) => {
                        if let Some(logger) = &self.logger {
                            info!(logger, "File already exists: {}", url);
                        }
                        return Ok(());
                    }
                    Err(e) => {
                        if let Some(logger) = &self.logger {
                            warn!(
                                logger,
                                "File already exists, but hash check failed: {:?} - deleting file",
                                e
                            );
                        }
                        fs::remove_file(&file_path)
                            .map_err(|e| FileDownloadError::file_remove_error(file_path, e))?;
                    }
                }
            } else {
                fs::remove_file(&file_path)
                    .map_err(|e| FileDownloadError::file_remove_error(file_path, e))?;
            }
        }
        if let Some(logger) = &self.logger {
            info!(logger, "Downloading file from: {}", url);
        }
        let response = self.http_get(url).await?;
        self.stream_response_body_to_file(response, file_path)
            .await?;
        if let Some(expected_hash) = expected_sha256_hex.as_ref() {
            check_file_hash(file_path, expected_hash)
        } else {
            Ok(())
        }
    }
pub async fn download_file ( & self , url : & str , file_path : & Path , expected_sha256_hex : Option < String > , ) -> FileDownloadResult < ( ) > { // In case the file already exists, check the file hash. if file_path . exists ( ) { if let Some ( expected_hash ) = expected_sha256_hex . as_ref ( ) { match check_file_hash ( file_path , expected_hash ) { Ok ( ( ) ) => { if let Some ( logger ) = & self . logger { info ! ( logger " " url ) ; } return Ok ( ( ) ) ; } Err ( e ) => { if let Some ( logger ) = & self . logger { warn ! ( logger " " e ) ; } fs :: remove_file ( & file_path ) . map_err ( | e | FileDownloadError :: file_remove_error ( file_path , e ) ) ? ; } } } else { fs :: remove_file ( & file_path ) . map_err ( | e | FileDownloadError :: file_remove_error ( file_path , e ) ) ? ; } } if let Some ( logger ) = & self . logger { info ! ( logger " " url ) ; } let response = self . http_get ( url ) . await ? ; self . stream_response_body_to_file ( response , file_path ) . await ? ; if let Some ( expected_hash ) = expected_sha256_hex . as_ref ( ) { check_file_hash ( file_path , expected_hash ) } else { Ok ( ( ) ) } }
Make a GET HTTP request to `url`, stream the response body to
`file_path` and verify that the resulting file has hash
`expected_sha256_hex`.
Make a GET HTTP request to ` url ` stream the response body to ` file_path ` and verify that the resulting file has hash ` expected_sha256_hex ` .
&self
url
&str
file_path
&Path
expected_sha256_hex
Option<String>