fix(uriproxy): oops didnt strip base

This commit is contained in:
əlemi 2024-05-20 06:42:22 +02:00
parent af3a3fbbb8
commit 96a0cf0df3
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -21,23 +21,28 @@ impl AsRef<str> for UriClass {
/// unpack uri in id if valid, otherwise compose full uri with "{base}/{entity}/{id}"
pub fn uri(base: &str, entity: UriClass, id: &str) -> String {
if id.starts_with('~') { // ready-to-use base64-encoded id
if let Ok(bytes) = base64::prelude::BASE64_STANDARD.decode(id) {
if let Ok(uri) = std::str::from_utf8(&bytes) {
return uri.to_string();
if let Some(bare_id) = get_nth_uri_element(id) {
if bare_id.starts_with('~') {
if let Ok(bytes) = base64::prelude::BASE64_STANDARD.decode(bare_id.replacen('~', "", 1)) {
if let Ok(uri) = std::str::from_utf8(&bytes) {
return uri.to_string();
}
}
}
}
format!("{}/{}/{}", base, entity.as_ref(), id)
}
fn get_nth_uri_element(uri: &str) -> Option<String> {
uri // https://example.org/users/test/followers/page?offset=42
.split('/') // ['https:', '', 'example.org', 'users', 'test', 'followers', 'page?offset=42' ]
.nth(4) // 'test'
.map(|x| x.to_string())
}
/// decompose local id constructed by uri() fn
pub fn decompose_id(full_id: &str) -> String {
full_id // https://example.org/users/test/followers/page?offset=42
.split('/') // ['https:', '', 'example.org', 'users', 'test', 'followers', 'page?offset=42' ]
.nth(4) // 'test'
.unwrap_or("")
.to_string()
get_nth_uri_element(full_id).unwrap_or_default()
}
/// encode with base64 remote url and prefix it with ~