use url::Url;
pub fn sanitize_object_store_url_for_dirname(url: &Url) -> String {
let mut parts = vec![url.scheme()];
if let Some(host) = url.host_str() {
parts.push(host);
}
let dirname = parts.join("_");
dirname.replace(['/', ':', '?', '&', '=', '\\'], "_")
}
pub fn sanitize_path_for_dirname(path: &str) -> String {
path.replace(['/', ':', '?', '&', '=', '\\'], "_")
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
use url::Url;
#[test]
fn test_can_create_directories_with_sanitized_names() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let test_urls = [
"http://example.com/path/to/resource",
"https://example.com?param1=value1¶m2=value2",
"s3://bucket-name/object/key",
"https://user:password@example.com:8080/path?query=value#fragment",
"file:///C:/Windows/System32/",
"https://example.com/path/with/special?chars=%20%26%3F",
"http://192.168.1.1:8080/admin?debug=true",
"ftp://files.example.com/pub/file.txt",
"https://例子.测试",
&format!("https://example.com/{}", "a".repeat(200)),
];
for url_str in test_urls {
let url = Url::parse(url_str).expect("Failed to parse URL");
let dirname = sanitize_object_store_url_for_dirname(&url);
let dir_path = temp_dir.path().join(dirname);
fs::create_dir(&dir_path).expect("Failed to create directory");
assert!(dir_path.exists());
assert!(dir_path.is_dir());
fs::remove_dir(&dir_path).expect("Failed to remove test directory");
}
}
}