2025-06-08 21:31:50 +00:00
|
|
|
use std::{error::Error, fmt::Display, io};
|
2025-06-08 01:20:41 +00:00
|
|
|
|
|
|
|
|
use log::debug;
|
2025-06-08 21:31:50 +00:00
|
|
|
use thiserror::Error;
|
2025-06-08 01:20:41 +00:00
|
|
|
|
2025-06-08 17:13:01 +00:00
|
|
|
use super::{
|
|
|
|
|
api::{NewShareRequest, NewShareResponse, NotifyShareResponse, Uri},
|
|
|
|
|
file::{FileChecked, FileUploading, SharryFile},
|
|
|
|
|
};
|
2025-06-08 01:20:41 +00:00
|
|
|
|
|
|
|
|
pub trait Client {
|
|
|
|
|
fn sharry_share_create(
|
|
|
|
|
&self,
|
|
|
|
|
uri: &Uri,
|
|
|
|
|
alias_id: &str,
|
|
|
|
|
data: NewShareRequest,
|
|
|
|
|
) -> Result<String, ClientError>;
|
|
|
|
|
|
|
|
|
|
fn sharry_share_notify(
|
|
|
|
|
&self,
|
|
|
|
|
uri: &Uri,
|
|
|
|
|
alias_id: &str,
|
|
|
|
|
share_id: &str,
|
|
|
|
|
) -> Result<(), ClientError>;
|
|
|
|
|
|
2025-06-08 17:13:01 +00:00
|
|
|
fn sharry_file_create(
|
|
|
|
|
&self,
|
|
|
|
|
uri: &Uri,
|
|
|
|
|
alias_id: &str,
|
|
|
|
|
share_id: &str,
|
2025-06-10 01:17:17 +00:00
|
|
|
file_name: &str,
|
|
|
|
|
file_size: u64,
|
|
|
|
|
) -> Result<String, ClientError>;
|
2025-06-08 01:20:41 +00:00
|
|
|
|
2025-06-10 01:17:17 +00:00
|
|
|
fn sharry_file_patch(
|
|
|
|
|
&self,
|
|
|
|
|
patch_uri: &str,
|
|
|
|
|
alias_id: &str,
|
|
|
|
|
offset: u64,
|
|
|
|
|
chunk: &[u8],
|
|
|
|
|
) -> Result<u64, ClientError>;
|
2025-06-08 01:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-08 21:31:50 +00:00
|
|
|
#[derive(Debug, Error)]
|
2025-06-08 01:20:41 +00:00
|
|
|
pub enum ClientError {
|
|
|
|
|
#[error("file I/O error: {0}")]
|
|
|
|
|
FileIO(#[from] io::Error),
|
|
|
|
|
|
|
|
|
|
#[error("network request failed: {0}")]
|
|
|
|
|
Request(String),
|
|
|
|
|
|
|
|
|
|
#[error("response parsing failed: {0}")]
|
|
|
|
|
ResponseParsing(String),
|
2025-06-08 17:13:01 +00:00
|
|
|
|
|
|
|
|
#[error("unexpected response status: {actual} (expected {expected})")]
|
|
|
|
|
ResponseStatus { actual: u16, expected: u16 },
|
|
|
|
|
|
2025-06-08 01:20:41 +00:00
|
|
|
#[error("unexpected response content: {0}")]
|
|
|
|
|
ResponseContent(String),
|
2025-06-08 21:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ClientError {
|
|
|
|
|
fn req_err(msg: impl Display) -> Self {
|
2025-06-10 01:17:17 +00:00
|
|
|
Self::Request(msg.to_string())
|
2025-06-08 21:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn res_parse_err(msg: impl Display) -> Self {
|
2025-06-10 01:17:17 +00:00
|
|
|
Self::ResponseParsing(msg.to_string())
|
2025-06-08 21:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn res_content_err(msg: impl Display) -> Self {
|
2025-06-10 01:17:17 +00:00
|
|
|
Self::ResponseContent(msg.to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn res_check_status<T>(actual: T, expected: T) -> Result<(), Self>
|
|
|
|
|
where
|
|
|
|
|
T: Into<u16> + Eq,
|
|
|
|
|
{
|
|
|
|
|
if actual == expected {
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err(Self::ResponseStatus {
|
|
|
|
|
actual: actual.into(),
|
|
|
|
|
expected: expected.into(),
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-06-08 21:31:50 +00:00
|
|
|
}
|
2025-06-08 01:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Client for ureq::Agent {
|
|
|
|
|
fn sharry_share_create(
|
|
|
|
|
&self,
|
|
|
|
|
uri: &Uri,
|
|
|
|
|
alias_id: &str,
|
|
|
|
|
data: NewShareRequest,
|
|
|
|
|
) -> Result<String, ClientError> {
|
|
|
|
|
let res = {
|
|
|
|
|
let endpoint = uri.get_endpoint("alias/upload/new");
|
|
|
|
|
|
|
|
|
|
self.post(endpoint)
|
|
|
|
|
.header("Sharry-Alias", alias_id)
|
|
|
|
|
.send_json(data)
|
2025-06-08 21:31:50 +00:00
|
|
|
.map_err(|e| ClientError::req_err(e))?
|
2025-06-08 01:20:41 +00:00
|
|
|
.body_mut()
|
|
|
|
|
.read_json::<NewShareResponse>()
|
2025-06-08 21:31:50 +00:00
|
|
|
.map_err(|e| ClientError::res_parse_err(e))?
|
2025-06-08 01:20:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
debug!("response: {res:?}");
|
|
|
|
|
|
|
|
|
|
if res.success && (res.message == "Share created.") {
|
|
|
|
|
Ok(res.id)
|
|
|
|
|
} else {
|
2025-06-08 21:31:50 +00:00
|
|
|
Err(ClientError::res_content_err(format!("{res:?}")))
|
2025-06-08 01:20:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sharry_share_notify(
|
|
|
|
|
&self,
|
|
|
|
|
uri: &Uri,
|
|
|
|
|
alias_id: &str,
|
|
|
|
|
share_id: &str,
|
|
|
|
|
) -> Result<(), ClientError> {
|
|
|
|
|
let res = {
|
|
|
|
|
let endpoint = uri.get_endpoint(format!("alias/mail/notify/{}", share_id));
|
|
|
|
|
|
|
|
|
|
self.post(endpoint)
|
|
|
|
|
.header("Sharry-Alias", alias_id)
|
|
|
|
|
.send_empty()
|
2025-06-08 21:31:50 +00:00
|
|
|
.map_err(|e| ClientError::req_err(e))?
|
2025-06-08 01:20:41 +00:00
|
|
|
.body_mut()
|
|
|
|
|
.read_json::<NotifyShareResponse>()
|
2025-06-08 21:31:50 +00:00
|
|
|
.map_err(|e| ClientError::res_parse_err(e))?
|
2025-06-08 01:20:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
debug!("response: {res:?}");
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-06-08 17:13:01 +00:00
|
|
|
|
|
|
|
|
fn sharry_file_create(
|
|
|
|
|
&self,
|
|
|
|
|
uri: &Uri,
|
|
|
|
|
alias_id: &str,
|
|
|
|
|
share_id: &str,
|
2025-06-10 01:17:17 +00:00
|
|
|
file_name: &str,
|
|
|
|
|
file_size: u64,
|
|
|
|
|
) -> Result<String, ClientError> {
|
2025-06-08 17:13:01 +00:00
|
|
|
let res = {
|
|
|
|
|
let endpoint = uri.get_endpoint(format!("alias/upload/{}/files/tus", share_id));
|
|
|
|
|
|
|
|
|
|
self.post(endpoint)
|
|
|
|
|
.header("Sharry-Alias", alias_id)
|
2025-06-10 01:17:17 +00:00
|
|
|
.header("Sharry-File-Name", file_name)
|
|
|
|
|
.header("Upload-Length", file_size)
|
2025-06-08 17:13:01 +00:00
|
|
|
.send_empty()
|
2025-06-10 01:17:17 +00:00
|
|
|
.map_err(ClientError::req_err)?
|
2025-06-08 17:13:01 +00:00
|
|
|
};
|
|
|
|
|
|
2025-06-10 01:17:17 +00:00
|
|
|
ClientError::res_check_status(res.status(), ureq::http::StatusCode::CREATED)?;
|
2025-06-08 17:13:01 +00:00
|
|
|
|
|
|
|
|
let location = (res.headers().get("Location"))
|
2025-06-08 21:31:50 +00:00
|
|
|
.ok_or_else(|| ClientError::res_parse_err("Location header not found"))?
|
2025-06-08 17:13:01 +00:00
|
|
|
.to_str()
|
2025-06-10 01:17:17 +00:00
|
|
|
.map_err(ClientError::res_parse_err)?
|
2025-06-08 17:13:01 +00:00
|
|
|
.to_string();
|
|
|
|
|
|
|
|
|
|
debug!("patch uri: {location}");
|
|
|
|
|
|
2025-06-10 01:17:17 +00:00
|
|
|
Ok(location)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sharry_file_patch(
|
|
|
|
|
&self,
|
|
|
|
|
patch_uri: &str,
|
|
|
|
|
alias_id: &str,
|
|
|
|
|
offset: u64,
|
|
|
|
|
chunk: &[u8],
|
|
|
|
|
) -> Result<u64, ClientError> {
|
|
|
|
|
let res = self
|
|
|
|
|
.patch(patch_uri)
|
|
|
|
|
.header("Sharry-Alias", alias_id)
|
|
|
|
|
.header("Upload-Offset", offset)
|
|
|
|
|
.send(chunk)
|
|
|
|
|
.map_err(ClientError::req_err)?;
|
|
|
|
|
|
|
|
|
|
ClientError::res_check_status(res.status(), ureq::http::StatusCode::NO_CONTENT)?;
|
|
|
|
|
|
|
|
|
|
let res_offset = (res.headers().get("Upload-Offset"))
|
|
|
|
|
.ok_or_else(|| ClientError::res_parse_err("Upload-Offset header not found"))?
|
|
|
|
|
.to_str()
|
|
|
|
|
.map_err(ClientError::res_parse_err)?
|
|
|
|
|
.parse::<u64>()
|
|
|
|
|
.map_err(ClientError::res_parse_err)?;
|
|
|
|
|
|
|
|
|
|
// get chunk length as `u64` (we have checked while reading the chunk!)
|
|
|
|
|
let chunk_len = u64::try_from(chunk.len()).expect("something's VERY wrong");
|
|
|
|
|
|
|
|
|
|
if offset + chunk_len == res_offset {
|
|
|
|
|
Ok(res_offset)
|
|
|
|
|
} else {
|
|
|
|
|
Err(ClientError::ResponseContent(format!(
|
|
|
|
|
"Unexpected Upload-Offset: {} (expected {})",
|
|
|
|
|
res_offset,
|
|
|
|
|
offset + chunk_len
|
|
|
|
|
)))
|
|
|
|
|
}
|
2025-06-08 17:13:01 +00:00
|
|
|
}
|
2025-06-08 01:20:41 +00:00
|
|
|
}
|