aboutsummaryrefslogtreecommitdiff
path: root/libs/nak/src/utils.rs
blob: 7d7886c6838d7d8a02ad99cdbd88d7a5fdb075a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//! Shared utility functions used across the application

use std::error::Error;
use std::fs;
use std::path::Path;

/// Download a file from URL to the specified path
pub fn download_file(url: &str, path: &Path) -> Result<(), Box<dyn Error>> {
    // Ensure parent directory exists
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }

    let resp = ureq::get(url).call()?;
    let mut reader = resp.into_reader();
    let mut file = fs::File::create(path)?;
    std::io::copy(&mut reader, &mut file)?;
    Ok(())
}