Skip to content

Commit

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

assets.push((file_to_asset_path(asset), content, encoding, content_type));
let asspath = filepath_to_asset_paths("/".to_string() + asset.path().to_str().unwrap()).first().unwrap().to_string();
assets.push((asspath, content, encoding, content_type));
}
assets
}
Expand All @@ -292,32 +293,65 @@ 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: for all, also return variant with leading /
// TODO: check if &"..." == is ok
// TODO: filter out empty strings?
// TODO: split by "/" or 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.len() == 0 {
vec!["".to_string(), "index.html".to_string()]
} else if last == &"index.html" {
vec![
elements.join("/").to_string(),

] // + <elements>/index.html
} else if let Some(foo) = last.strip_suffix(".html") {
let prefix = elements.join("/").to_string();
vec![[prefix, foo.to_string()].join("/").to_string()] // + <elements>/<foo>.html
} else if let Some(foo) = last.strip_suffix(".gz") {
let prefix = elements.join("/").to_string();
// TODO: note about this being safe because we always reduce (.gz)
filepath_to_asset_paths([prefix, foo.to_string()].join("/").to_string())
} else {
let prefix = elements.join("/").to_string();
vec![[prefix, last.to_string()].join("/").to_string()]
}
},
}
}

#[test]
fn test_foo() {


fn foo(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);
}
file_path


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

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


}

0 comments on commit e5148ae

Please sign in to comment.