From 8284130890b6d4b1c8cb01a67e819b4e20f8bdbc Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 7 Jun 2024 06:14:45 +0200 Subject: [PATCH] feat(uriproxy)!: renamed fns, added expand fn --- upub/core/src/context.rs | 4 ++-- utils/uriproxy/Cargo.toml | 2 +- utils/uriproxy/lib.rs | 19 +++++++++++++------ web/src/lib.rs | 4 ++-- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/upub/core/src/context.rs b/upub/core/src/context.rs index 5318d736..1e25b400 100644 --- a/upub/core/src/context.rs +++ b/upub/core/src/context.rs @@ -120,9 +120,9 @@ impl Context { /// get bare id, which is uuid for local stuff and +{uri|base64} for remote stuff pub fn id(&self, full_id: &str) -> String { if self.is_local(full_id) { - uriproxy::decompose_id(full_id) + uriproxy::decompose(full_id) } else { - uriproxy::compact_id(full_id) + uriproxy::compact(full_id) } } diff --git a/utils/uriproxy/Cargo.toml b/utils/uriproxy/Cargo.toml index af5fa254..bb2f105a 100644 --- a/utils/uriproxy/Cargo.toml +++ b/utils/uriproxy/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uriproxy" -version = "0.1.0" +version = "0.1.1" edition = "2021" authors = [ "alemi " ] description = "internal upub crate to handle remote uris" diff --git a/utils/uriproxy/lib.rs b/utils/uriproxy/lib.rs index 7b75ffc5..0eb65030 100644 --- a/utils/uriproxy/lib.rs +++ b/utils/uriproxy/lib.rs @@ -24,10 +24,8 @@ 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_URL_SAFE_NO_PAD.decode(id.replacen('+', "", 1)) { - if let Ok(uri) = std::str::from_utf8(&bytes) { - return uri.to_string(); - } + if let Some(expanded) = expand(id) { + return expanded; } } @@ -35,7 +33,7 @@ pub fn uri(base: &str, entity: UriClass, id: &str) -> String { } /// decompose local id constructed by uri() fn -pub fn decompose_id(full_id: &str) -> String { +pub fn decompose(full_id: &str) -> String { full_id // https://example.org/actors/test/followers/page?offset=42 .replace("https://", "") .replace("http://", "") @@ -45,8 +43,17 @@ pub fn decompose_id(full_id: &str) -> String { .to_string() } +pub fn expand(uri: &str) -> Option { + if let Ok(bytes) = base64::prelude::BASE64_URL_SAFE_NO_PAD.decode(uri.replacen('+', "", 1)) { + if let Ok(uri) = std::str::from_utf8(&bytes) { + return Some(uri.to_string()); + } + } + None +} + /// encode with base64 remote url and prefix it with + -pub fn compact_id(uri: &str) -> String { +pub fn compact(uri: &str) -> String { let encoded = base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(uri.as_bytes()); format!("+{encoded}") } diff --git a/web/src/lib.rs b/web/src/lib.rs index bfb4dbc7..2468061a 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -183,9 +183,9 @@ impl Uri { pub fn short(url: &str) -> String { if url.starts_with(URL_BASE) || url.starts_with('/') { - uriproxy::decompose_id(url) + uriproxy::decompose(url) } else if url.starts_with("https://") || url.starts_with("http://") { - uriproxy::compact_id(url) + uriproxy::compact(url) } else { url.to_string() }