Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nmattia committed Oct 27, 2023
1 parent 19d4706 commit 9f3d964
Showing 1 changed file with 116 additions and 24 deletions.
140 changes: 116 additions & 24 deletions src/internet_identity/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,20 @@ fn collect_assets_from_dir(dir: &Dir) -> Vec<(String, Vec<u8>, ContentEncoding,
),
};

assets.push((file_to_asset_path(asset), content, encoding, content_type));
let asspaths = filepath_to_asset_paths(asset.path().to_str().unwrap().to_string());

// TODO: add canister/lightweight test to ensure assets are loaded up
// & certified successfully
for asspath in asspaths {
println!("/{}", asspath);
// TODO: don't clone content
assets.push((
format!("/{}", asspath),
content.clone(),
encoding,
content_type,
));
}
}
assets
}
Expand All @@ -292,32 +305,111 @@ fn file_extension<'a>(asset: &'a File) -> &'a str {
}

/// Returns the asset path for a given file:
/// * TODO:
/// * make relative path absolute
/// * map **/index.html to **/
/// * map **/<foo>.html to **/foo
/// * map **/<foo>.js.gz to **/<foo>.js
fn file_to_asset_path(asset: &File) -> String {
// make path absolute
let mut file_path = "/".to_string() + asset.path().to_str().unwrap();

if file_path.ends_with("index.html") {
// drop index.html filename (i.e. maps **/index.html to **/)
file_path = file_path
.chars()
.take(file_path.len() - "index.html".len())
.collect()
} else if file_path.ends_with(".html") {
// drop .html file endings (i.e. maps **/<foo>.html to **/foo)
file_path = file_path
.chars()
.take(file_path.len() - ".html".len())
.collect()
} else if file_path.ends_with(".gz") {
// drop .gz for .foo.gz files (i.e. maps **/<foo>.js.gz to **/<foo>.js)
file_path = file_path
.chars()
.take(file_path.len() - ".gz".len())
.collect()
fn filepath_to_asset_paths(file_path: String) -> Vec<String> {
// TODO: error out if starting with /?
// TODO: explain why not splitting by system path sep
let parts: Vec<&str> = file_path.split('/').collect();

match parts.split_last() {
None => {
vec!["Error, TODO".to_string()]
}
Some((last, elements)) => {
if last == &"index.html" && elements.is_empty() {
vec!["".to_string(), "index.html".to_string()]
} else if last == &"index.html" {
let prefix = elements.join("/").to_string();
vec![
format!("{prefix}"),
format!("{prefix}/"),
format!("{prefix}/index.html"),
]
} else if let Some(path) = last.strip_suffix(".html") {
let mut elements = elements.to_vec();
elements.push(path);
let prefix = elements.join("/").to_string();
vec![
format!("{prefix}"),
format!("{prefix}/"),
format!("{prefix}/index.html"),
]
} else if let Some(file) = last.strip_suffix(".gz") {
let mut elements = elements.to_vec();
elements.push(file);
// TODO: note about this being safe because we always reduce (.gz)
filepath_to_asset_paths(elements.join("/").to_string())
} else {
let mut elements = elements.to_vec();
elements.push(last);
let prefix = elements.join("/").to_string();
vec![prefix]
}
}
}
file_path
}

#[test]
fn test_filepath_asset_paths() {
fn assert_gen_paths(inp: String, exp: Vec<String>) {
let mut exp = exp.clone();
exp.sort();

let mut actual = filepath_to_asset_paths(inp);
actual.sort();
assert_eq!(exp, actual);
}

assert_gen_paths(
"index.html".to_string(),
vec!["".to_string(), "index.html".to_string()],
);

assert_gen_paths(
"foo.html".to_string(),
vec![
"foo".to_string(),
"foo/".to_string(),
"foo/index.html".to_string(),
],
);

assert_gen_paths(
"foo/index.html".to_string(),
vec![
"foo".to_string(),
"foo/".to_string(),
"foo/index.html".to_string(),
],
);

assert_gen_paths("index.css".to_string(), vec!["index.css".to_string()]);
assert_gen_paths("foo.bar.gz".to_string(), vec!["foo.bar".to_string()]);

assert_gen_paths(
"sub/foo.bar.gz".to_string(),
vec!["sub/foo.bar".to_string()],
);

assert_gen_paths(
"foo.html.gz".to_string(),
vec![
"foo".to_string(),
"foo/".to_string(),
"foo/index.html".to_string(),
],
);

assert_gen_paths(
"sub/foo.html.gz".to_string(),
vec![
"sub/foo".to_string(),
"sub/foo/".to_string(),
"sub/foo/index.html".to_string(),
],
);
}

0 comments on commit 9f3d964

Please sign in to comment.