shrupl/src/sharry/client.rs

87 lines
2.6 KiB
Rust
Raw Normal View History

use std::sync::LazyLock;
2025-06-08 01:20:41 +00:00
use log::trace;
use regex::Regex;
2025-06-08 01:20:41 +00:00
use crate::{error, file};
use super::api::{NewShareRequest, Uri};
2025-06-08 01:20:41 +00:00
pub trait Client {
fn get_file_id(uri: &str) -> error::Result<&str> {
/// Pattern breakdown:
/// - `^([^:/?#]+)://` scheme (anything but `:/?#`) + `"://"`
/// - `([^/?#]+)` authority/host (anything but `/?#`)
/// - `/api/v2/alias/upload/` literal path segment
/// - `([^/]+)` capture SID (one or more non-slash chars)
/// - `/files/tus/` literal path segment
/// - `(?P<fid>[^/]+)` capture FID (one or more non-slash chars)
/// - `$` end of string
static UPLOAD_URL_RE: LazyLock<Regex> = LazyLock::new(|| {
trace!("compiling UPLOAD_URL_RE");
Regex::new(
r"^([^:/?#]+)://([^/?#]+)/api/v2/alias/upload/[^/]+/files/tus/(?P<fid>[^/]+)$",
)
.expect("Regex compilation failed")
});
if let Some(fid) = UPLOAD_URL_RE
.captures(uri)
.and_then(|caps| caps.name("fid").map(|m| m.as_str()))
{
Ok(fid)
} else {
Err(error::Error::unknown(format!(
2025-06-18 14:49:30 +00:00
"Could not extract File ID from {uri:?}"
)))
}
}
2025-06-08 01:20:41 +00:00
fn share_create(
&self,
uri: &Uri,
alias_id: &str,
data: NewShareRequest,
) -> error::Result<String>;
fn share_notify(&self, uri: &Uri, alias_id: &str, share_id: &str) -> error::Result<()>;
2025-06-08 01:20:41 +00:00
fn file_create(
2025-06-08 17:13:01 +00:00
&self,
uri: &Uri,
2025-06-08 17:13:01 +00:00
alias_id: &str,
share_id: &str,
file: &file::Checked,
) -> error::Result<String>;
2025-06-08 01:20:41 +00:00
fn file_patch(
&self,
uri: &Uri,
alias_id: &str,
share_id: &str,
chunk: &file::Chunk,
) -> error::Result<()>;
2025-06-08 01:20:41 +00:00
}
// TODO move into tests subdir
// #[cfg(test)]
// mod tests {
// use super::*;
2025-06-08 01:20:41 +00:00
// #[test]
// fn test_get_file_id() {
// let good = "https://example.com/api/v2/alias/upload/SID123/files/tus/FID456";
// let good = Client::get_file_id(good);
// assert!(good.is_ok());
// assert_eq!(good.unwrap(), "FID456");
// let bad = "https://example.com/api/v2/alias/upload//files/tus/FID456"; // missing SID
// assert!(Client::get_file_id(bad).is_err());
2025-06-08 17:13:01 +00:00
// let bad: &'static str = "https://example.com/api/v2/alias/upload/SID123/files/tus/"; // missing FID
// assert!(Client::get_file_id(bad).is_err());
// }
// }