upub/utils/uriproxy/lib.rs

53 lines
1.4 KiB
Rust
Raw Normal View History

use base64::Engine;
#[derive(Clone, Copy)]
pub enum UriClass {
2024-05-29 21:36:55 +02:00
Actor,
Object,
Activity,
}
impl AsRef<str> for UriClass {
fn as_ref(&self) -> &str {
match self {
2024-05-29 21:36:55 +02:00
Self::Actor => "actors",
Self::Object => "objects",
Self::Activity => "activities",
}
}
}
/// 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("https://") || id.starts_with("http://") {
return id.to_string();
}
if id.starts_with('+') { // ready-to-use base64-encoded id
2024-05-20 07:13:22 +02:00
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();
}
}
}
format!("{}/{}/{}", base, entity.as_ref(), id)
}
/// decompose local id constructed by uri() fn
pub fn decompose_id(full_id: &str) -> String {
full_id // https://example.org/actors/test/followers/page?offset=42
2024-05-20 08:45:59 +02:00
.replace("https://", "")
.replace("http://", "")
2024-05-29 21:36:55 +02:00
.split('/') // ['example.org', 'actors', 'test', 'followers', 'page?offset=42' ]
2024-05-20 08:45:59 +02:00
.nth(2) // 'test'
.unwrap_or("")
.to_string()
}
/// encode with base64 remote url and prefix it with +
pub fn compact_id(uri: &str) -> String {
2024-05-20 07:13:22 +02:00
let encoded = base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(uri.as_bytes());
format!("+{encoded}")
}