2025-06-25 16:48:00 +00:00
|
|
|
|
use std::sync::LazyLock;
|
2025-06-08 01:20:41 +00:00
|
|
|
|
|
2025-06-18 13:09:34 +00:00
|
|
|
|
use log::trace;
|
|
|
|
|
|
use regex::Regex;
|
2025-06-08 01:20:41 +00:00
|
|
|
|
|
2025-06-25 16:48:00 +00:00
|
|
|
|
use crate::{error, file};
|
2025-06-10 18:20:52 +00:00
|
|
|
|
|
2025-06-18 13:09:34 +00:00
|
|
|
|
use super::api::{NewShareRequest, Uri};
|
2025-06-08 01:20:41 +00:00
|
|
|
|
|
|
|
|
|
|
pub trait Client {
|
2025-06-25 16:48:00 +00:00
|
|
|
|
fn get_file_id(uri: &str) -> error::Result<&str> {
|
2025-06-18 13:09:34 +00:00
|
|
|
|
/// 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 {
|
2025-06-25 16:48:00 +00:00
|
|
|
|
Err(error::Error::unknown(format!(
|
2025-06-18 14:49:30 +00:00
|
|
|
|
"Could not extract File ID from {uri:?}"
|
2025-06-18 13:09:34 +00:00
|
|
|
|
)))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-08 01:20:41 +00:00
|
|
|
|
|
2025-06-18 13:09:34 +00:00
|
|
|
|
fn share_create(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
uri: &Uri,
|
|
|
|
|
|
alias_id: &str,
|
|
|
|
|
|
data: NewShareRequest,
|
2025-06-25 16:48:00 +00:00
|
|
|
|
) -> error::Result<String>;
|
2025-06-18 13:09:34 +00:00
|
|
|
|
|
2025-06-25 16:48:00 +00:00
|
|
|
|
fn share_notify(&self, uri: &Uri, alias_id: &str, share_id: &str) -> error::Result<()>;
|
2025-06-08 01:20:41 +00:00
|
|
|
|
|
2025-06-10 18:20:52 +00:00
|
|
|
|
fn file_create(
|
2025-06-08 17:13:01 +00:00
|
|
|
|
&self,
|
2025-06-18 13:09:34 +00:00
|
|
|
|
uri: &Uri,
|
2025-06-08 17:13:01 +00:00
|
|
|
|
alias_id: &str,
|
2025-06-18 13:09:34 +00:00
|
|
|
|
share_id: &str,
|
|
|
|
|
|
file: &file::Checked,
|
2025-06-25 16:48:00 +00:00
|
|
|
|
) -> error::Result<String>;
|
2025-06-08 01:20:41 +00:00
|
|
|
|
|
2025-06-18 13:09:34 +00:00
|
|
|
|
fn file_patch(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
uri: &Uri,
|
|
|
|
|
|
alias_id: &str,
|
|
|
|
|
|
share_id: &str,
|
|
|
|
|
|
chunk: &file::Chunk,
|
2025-06-25 16:48:00 +00:00
|
|
|
|
) -> error::Result<()>;
|
2025-06-08 01:20:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-18 13:09:34 +00:00
|
|
|
|
// TODO move into tests subdir
|
2025-06-12 00:54:26 +00:00
|
|
|
|
|
2025-06-18 13:09:34 +00:00
|
|
|
|
// #[cfg(test)]
|
|
|
|
|
|
// mod tests {
|
|
|
|
|
|
// use super::*;
|
2025-06-08 01:20:41 +00:00
|
|
|
|
|
2025-06-18 13:09:34 +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");
|
2025-06-12 00:54:26 +00:00
|
|
|
|
|
2025-06-18 13:09:34 +00:00
|
|
|
|
// 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
|
|
|
|
|
2025-06-18 13:09:34 +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());
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|