internet_mapper/src/filesystem.rs
2025-03-21 05:59:40 +00:00

25 lines
816 B
Rust

use std::path::PathBuf;
use tokio::fs;
use tracing::{error, instrument, trace};
use url::Url;
#[instrument(skip(data))]
pub async fn store(data: &str, url: &Url) {
let path = PathBuf::from("./downloaded".to_string() + url.path());
let basepath = path.ancestors().skip(1).take(1).collect::<PathBuf>();
trace!("Built path: {:?} and base path: {:?}", &path, &basepath);
if let Err(err) = fs::create_dir_all(&basepath).await {
let ex = path.ancestors().fold(String::new(), |mut s, item| {
s += ", ";
s += &item.to_string_lossy().to_string();
s
});
error!("Dir creation: {err} {:?} {ex}", basepath);
} else {
if let Err(err) = fs::write(&path, data).await {
error!("File creation: {err} {:?}", path);
}
}
}