forked from alemi/upub
feat(uriproxy)!: renamed fns, added expand fn
This commit is contained in:
parent
afa5bd45d0
commit
8284130890
4 changed files with 18 additions and 11 deletions
|
@ -120,9 +120,9 @@ impl Context {
|
||||||
/// get bare id, which is uuid for local stuff and +{uri|base64} for remote stuff
|
/// get bare id, which is uuid for local stuff and +{uri|base64} for remote stuff
|
||||||
pub fn id(&self, full_id: &str) -> String {
|
pub fn id(&self, full_id: &str) -> String {
|
||||||
if self.is_local(full_id) {
|
if self.is_local(full_id) {
|
||||||
uriproxy::decompose_id(full_id)
|
uriproxy::decompose(full_id)
|
||||||
} else {
|
} else {
|
||||||
uriproxy::compact_id(full_id)
|
uriproxy::compact(full_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "uriproxy"
|
name = "uriproxy"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = [ "alemi <me@alemi.dev>" ]
|
authors = [ "alemi <me@alemi.dev>" ]
|
||||||
description = "internal upub crate to handle remote uris"
|
description = "internal upub crate to handle remote uris"
|
||||||
|
|
|
@ -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 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 Some(expanded) = expand(id) {
|
||||||
if let Ok(uri) = std::str::from_utf8(&bytes) {
|
return expanded;
|
||||||
return uri.to_string();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +33,7 @@ pub fn uri(base: &str, entity: UriClass, id: &str) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// decompose local id constructed by uri() fn
|
/// 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
|
full_id // https://example.org/actors/test/followers/page?offset=42
|
||||||
.replace("https://", "")
|
.replace("https://", "")
|
||||||
.replace("http://", "")
|
.replace("http://", "")
|
||||||
|
@ -45,8 +43,17 @@ pub fn decompose_id(full_id: &str) -> String {
|
||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn expand(uri: &str) -> Option<String> {
|
||||||
|
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 +
|
/// 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());
|
let encoded = base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(uri.as_bytes());
|
||||||
format!("+{encoded}")
|
format!("+{encoded}")
|
||||||
}
|
}
|
||||||
|
|
|
@ -183,9 +183,9 @@ impl Uri {
|
||||||
|
|
||||||
pub fn short(url: &str) -> String {
|
pub fn short(url: &str) -> String {
|
||||||
if url.starts_with(URL_BASE) || url.starts_with('/') {
|
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://") {
|
} else if url.starts_with("https://") || url.starts_with("http://") {
|
||||||
uriproxy::compact_id(url)
|
uriproxy::compact(url)
|
||||||
} else {
|
} else {
|
||||||
url.to_string()
|
url.to_string()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue