Parse Matrix media URL for clients

This commit is contained in:
2025-02-26 21:15:07 +01:00
parent fa4665280d
commit 6adc0c1fbb
8 changed files with 29 additions and 7 deletions

20
src/utils/base_utils.rs Normal file
View File

@ -0,0 +1,20 @@
use rand::distr::{Alphanumeric, SampleString};
use std::time::{SystemTime, UNIX_EPOCH};
/// Generate a random string of a given size
pub fn rand_str(len: usize) -> String {
Alphanumeric.sample_string(&mut rand::rng(), len)
}
/// Get current time
pub fn curr_time() -> anyhow::Result<u64> {
Ok(SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|t| t.as_secs())?)
}
/// Format time
pub fn format_time(time: u64) -> Option<String> {
let time = chrono::DateTime::from_timestamp(time as i64, 0)?;
Some(time.naive_local().to_string())
}

10
src/utils/matrix_utils.rs Normal file
View File

@ -0,0 +1,10 @@
use ruma::OwnedServerName;
use std::str::FromStr;
/// Parse Matrix media URL returning media id and server name
pub fn parse_mxc_url(url: &str) -> Option<(&str, OwnedServerName)> {
let strip = url.strip_prefix("mxc://")?;
let parts = strip.split_once('/')?;
Some((parts.0, OwnedServerName::from_str(parts.1).ok()?))
}

2
src/utils/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod base_utils;
pub mod matrix_utils;