2025-06-25 23:42:00 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
|
|
use log::trace;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2025-07-01 15:24:26 +00:00
|
|
|
/// ID of a file in a Sharry share
|
|
|
|
|
///
|
|
|
|
|
/// - impl `Clone` as this is just a String
|
|
|
|
|
/// - impl `serde` for cachefile handling
|
|
|
|
|
/// - impl `Display` for formatting compatibility
|
|
|
|
|
/// - impl `AsRef<[u8]>` for hashing with `blake2b_simd`
|
2025-07-01 15:20:07 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2025-06-25 23:42:00 +00:00
|
|
|
pub struct Uri(String);
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for Uri {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.write_str(&self.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AsRef<[u8]> for Uri {
|
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
|
self.0.as_bytes()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Uri {
|
|
|
|
|
pub fn new(protocol: impl fmt::Display, base_url: impl fmt::Display) -> Self {
|
|
|
|
|
Self(format!("{protocol}://{base_url}"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn endpoint(&self, path: fmt::Arguments) -> String {
|
|
|
|
|
let uri = format!("{}/api/v2/{path}", self.0);
|
|
|
|
|
trace!("endpoint: {uri:?}");
|
|
|
|
|
uri
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn share_create(&self) -> String {
|
|
|
|
|
self.endpoint(format_args!("alias/upload/new"))
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-27 01:47:38 +00:00
|
|
|
pub fn share_notify(&self, share_id: &super::ShareID) -> String {
|
2025-06-25 23:42:00 +00:00
|
|
|
self.endpoint(format_args!("alias/mail/notify/{share_id}"))
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-27 01:47:38 +00:00
|
|
|
pub fn file_create(&self, share_id: &super::ShareID) -> String {
|
2025-06-25 23:42:00 +00:00
|
|
|
self.endpoint(format_args!("alias/upload/{share_id}/files/tus"))
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-27 01:47:38 +00:00
|
|
|
pub fn file_patch(&self, share_id: &super::ShareID, file_id: &super::FileID) -> String {
|
2025-06-25 23:42:00 +00:00
|
|
|
self.endpoint(format_args!("alias/upload/{share_id}/files/tus/{file_id}"))
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-27 16:59:31 +00:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn basic_tests() {
|
|
|
|
|
let cases = vec![
|
|
|
|
|
("http", "example.com", "http://example.com"),
|
|
|
|
|
("https", "my-host:8080", "https://my-host:8080"),
|
|
|
|
|
("custom+scheme", "host", "custom+scheme://host"),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (protocol, base_url, display) in cases {
|
|
|
|
|
let uri = Uri::new(protocol, base_url);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
uri.to_string(),
|
|
|
|
|
display,
|
|
|
|
|
"`impl Display for Uri` expected: {:?}, got {:?}",
|
|
|
|
|
display,
|
|
|
|
|
uri.to_string(),
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
uri.as_ref(),
|
|
|
|
|
display.as_bytes(),
|
|
|
|
|
"`impl AsRef<[u8]> for Uri` expected: {:?}, got {:?}",
|
|
|
|
|
display.as_bytes(),
|
|
|
|
|
uri.as_ref(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|