Compare commits
No commits in common. "bc4c15d17c6f49b243a1e24b5fd93e2546db3b32" and "2315c9cd2ed29bbfe1e17507d8f7f2f7a67c8b62" have entirely different histories.
bc4c15d17c
...
2315c9cd2e
14 changed files with 200 additions and 305 deletions
42
.vscode/tasks.json
vendored
42
.vscode/tasks.json
vendored
|
|
@ -43,16 +43,16 @@
|
|||
"problemMatcher": "$rustc",
|
||||
"group": "build"
|
||||
},
|
||||
{
|
||||
"label": "Run Unit Tests",
|
||||
"type": "cargo",
|
||||
"command": "test",
|
||||
"args": [
|
||||
"--lib"
|
||||
],
|
||||
"problemMatcher": "$rustc",
|
||||
"group": "test"
|
||||
},
|
||||
// {
|
||||
// "label": "Run Unit Tests",
|
||||
// "type": "cargo",
|
||||
// "command": "test",
|
||||
// "args": [
|
||||
// "--lib"
|
||||
// ],
|
||||
// "problemMatcher": "$rustc",
|
||||
// "group": "test"
|
||||
// },
|
||||
// {
|
||||
// "label": "Run Integration Tests",
|
||||
// "type": "cargo",
|
||||
|
|
@ -64,16 +64,16 @@
|
|||
// "problemMatcher": "$rustc",
|
||||
// "group": "test"
|
||||
// },
|
||||
{
|
||||
"label": "Run All Tests",
|
||||
"type": "shell",
|
||||
"command": "echo All Tests successful!",
|
||||
"dependsOn": [
|
||||
"Run Unit Tests",
|
||||
"Run Integration Tests"
|
||||
],
|
||||
"dependsOrder": "sequence",
|
||||
"group": "test"
|
||||
}
|
||||
// {
|
||||
// "label": "Run All Tests",
|
||||
// "type": "shell",
|
||||
// "command": "echo All Tests successful!",
|
||||
// "dependsOn": [
|
||||
// "Run Unit Tests",
|
||||
// "Run Integration Tests"
|
||||
// ],
|
||||
// "dependsOrder": "sequence",
|
||||
// "group": "test"
|
||||
// }
|
||||
],
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ use crate::{
|
|||
cli::Cli,
|
||||
file::{Chunk, FileTrait},
|
||||
output::new_progressbar,
|
||||
sharry::{Client, ShareID},
|
||||
sharry::Client,
|
||||
};
|
||||
|
||||
pub struct AppState {
|
||||
|
|
@ -32,7 +32,7 @@ fn new_http(args: &Cli) -> ureq::Agent {
|
|||
.into()
|
||||
}
|
||||
|
||||
fn new_share(args: &Cli) -> crate::Result<ShareID> {
|
||||
fn new_share(args: &Cli) -> crate::Result<String> {
|
||||
new_http(args).share_create(&args.get_uri(), &args.alias, args.get_share_request())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ use crate::{
|
|||
cli::Cli,
|
||||
file::{self, Chunk, FileTrait},
|
||||
output::new_progressbar,
|
||||
sharry::{AliasID, Client, ShareID, Uri},
|
||||
sharry::{Client, Uri},
|
||||
};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
|
|
@ -23,8 +23,8 @@ pub struct CacheFile {
|
|||
file_name: PathBuf,
|
||||
|
||||
uri: Uri,
|
||||
alias_id: AliasID,
|
||||
share_id: ShareID,
|
||||
alias_id: String,
|
||||
share_id: String,
|
||||
|
||||
uploading: Option<file::Uploading>,
|
||||
files: VecDeque<file::Checked>,
|
||||
|
|
@ -97,7 +97,7 @@ impl CacheFile {
|
|||
|
||||
pub fn from_args(
|
||||
args: &Cli,
|
||||
new_share: impl FnOnce(&Cli) -> crate::Result<ShareID>,
|
||||
new_share: impl FnOnce(&Cli) -> crate::Result<String>,
|
||||
) -> crate::Result<Self> {
|
||||
let mut files = args.files.clone();
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use log::LevelFilter;
|
|||
|
||||
use crate::{
|
||||
file::{Checked, FileTrait},
|
||||
sharry::{AliasID, Uri, json::NewShareRequest},
|
||||
sharry::{NewShareRequest, Uri},
|
||||
};
|
||||
|
||||
#[derive(Parser)]
|
||||
|
|
@ -69,7 +69,7 @@ pub struct Cli {
|
|||
url: String,
|
||||
|
||||
/// ID of a public alias to use
|
||||
pub alias: AliasID,
|
||||
pub alias: String,
|
||||
|
||||
/// Files to upload to the new share
|
||||
#[arg(value_name = "FILE", required = true, value_parser = parse_sharry_file)]
|
||||
|
|
@ -159,7 +159,7 @@ impl Cli {
|
|||
let mut hasher = Blake2b::new().hash_length(16).to_state();
|
||||
|
||||
hasher.update(self.get_uri().as_ref());
|
||||
hasher.update(self.alias.as_ref());
|
||||
hasher.update(self.alias.as_bytes());
|
||||
|
||||
for chk in sorted(&self.files) {
|
||||
hasher.update(chk.as_ref());
|
||||
|
|
|
|||
10
src/error.rs
10
src/error.rs
|
|
@ -1,20 +1,18 @@
|
|||
use std::fmt;
|
||||
|
||||
use crate::sharry;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Parameter {
|
||||
#[error("given URI {0:?}")]
|
||||
Uri(String),
|
||||
|
||||
#[error("given Alias ID {0:?}")]
|
||||
AliasID(sharry::AliasID),
|
||||
AliasID(String),
|
||||
|
||||
#[error("stored Share ID {0:?}")]
|
||||
ShareID(sharry::ShareID),
|
||||
ShareID(String),
|
||||
|
||||
#[error("stored {0:?}")]
|
||||
FileID(sharry::FileID),
|
||||
#[error("stored File ID {0:?}")]
|
||||
FileID(String),
|
||||
}
|
||||
|
||||
impl Parameter {
|
||||
|
|
|
|||
|
|
@ -76,8 +76,8 @@ impl Checked {
|
|||
self,
|
||||
client: &impl sharry::Client,
|
||||
uri: &sharry::Uri,
|
||||
alias_id: &sharry::AliasID,
|
||||
share_id: &sharry::ShareID,
|
||||
alias_id: &str,
|
||||
share_id: &str,
|
||||
) -> crate::Result<Uploading> {
|
||||
let file_id = client.file_create(uri, alias_id, share_id, &self)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
use std::fmt;
|
||||
|
||||
use crate::sharry;
|
||||
|
||||
pub struct Chunk<'t> {
|
||||
file_id: sharry::FileID,
|
||||
file_id: String,
|
||||
offset: u64,
|
||||
data: &'t [u8],
|
||||
}
|
||||
|
|
@ -19,7 +17,7 @@ impl fmt::Debug for Chunk<'_> {
|
|||
}
|
||||
|
||||
impl<'t> Chunk<'t> {
|
||||
pub fn new(file_id: sharry::FileID, offset: u64, data: &'t [u8]) -> Self {
|
||||
pub fn new(file_id: String, offset: u64, data: &'t [u8]) -> Self {
|
||||
Self {
|
||||
file_id,
|
||||
offset,
|
||||
|
|
@ -27,7 +25,7 @@ impl<'t> Chunk<'t> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_file_id(&self) -> &sharry::FileID {
|
||||
pub fn get_file_id(&self) -> &str {
|
||||
&self.file_id
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ use std::{
|
|||
use log::warn;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::sharry;
|
||||
|
||||
use super::{Checked, Chunk, FileTrait};
|
||||
|
||||
|
|
@ -19,19 +18,14 @@ pub struct Uploading {
|
|||
size: u64,
|
||||
/// hash of that file
|
||||
hash: Option<String>,
|
||||
file_id: sharry::FileID,
|
||||
file_id: String,
|
||||
#[serde(skip)]
|
||||
last_offset: Option<u64>,
|
||||
offset: u64,
|
||||
}
|
||||
|
||||
impl Uploading {
|
||||
pub(super) fn new(
|
||||
path: PathBuf,
|
||||
size: u64,
|
||||
hash: Option<String>,
|
||||
file_id: sharry::FileID,
|
||||
) -> Self {
|
||||
pub(super) fn new(path: PathBuf, size: u64, hash: Option<String>, file_id: String) -> Self {
|
||||
Self {
|
||||
path,
|
||||
size,
|
||||
|
|
|
|||
|
|
@ -2,14 +2,14 @@ use log::{debug, trace};
|
|||
|
||||
use crate::{
|
||||
file::{self, FileTrait},
|
||||
sharry::{self, AliasID, FileID, ShareID, Uri},
|
||||
sharry::{self, Uri},
|
||||
};
|
||||
|
||||
fn find_cause(
|
||||
uri: &Uri,
|
||||
alias_id: &AliasID,
|
||||
share_id: Option<&ShareID>,
|
||||
file_id: Option<&FileID>,
|
||||
alias_id: &str,
|
||||
share_id: Option<&str>,
|
||||
file_id: Option<&str>,
|
||||
) -> impl FnOnce(ureq::Error) -> crate::Error {
|
||||
move |error| match error {
|
||||
ureq::Error::StatusCode(403) => {
|
||||
|
|
@ -49,15 +49,15 @@ impl sharry::Client for ureq::Agent {
|
|||
fn share_create(
|
||||
&self,
|
||||
uri: &Uri,
|
||||
alias_id: &AliasID,
|
||||
data: sharry::json::NewShareRequest,
|
||||
) -> crate::Result<ShareID> {
|
||||
alias_id: &str,
|
||||
data: sharry::NewShareRequest,
|
||||
) -> crate::Result<String> {
|
||||
let res = {
|
||||
let endpoint = uri.share_create();
|
||||
|
||||
let mut res = self
|
||||
.post(&endpoint)
|
||||
.header("Sharry-Alias", alias_id.as_ref())
|
||||
.header("Sharry-Alias", alias_id)
|
||||
.send_json(data)
|
||||
.map_err(find_cause(uri, alias_id, None, None))?;
|
||||
|
||||
|
|
@ -65,7 +65,7 @@ impl sharry::Client for ureq::Agent {
|
|||
crate::Error::res_status_check(res.status(), ureq::http::StatusCode::OK)?;
|
||||
|
||||
res.body_mut()
|
||||
.read_json::<sharry::json::NewShareResponse>()
|
||||
.read_json::<sharry::NewShareResponse>()
|
||||
.map_err(crate::Error::response)?
|
||||
};
|
||||
|
||||
|
|
@ -74,19 +74,19 @@ impl sharry::Client for ureq::Agent {
|
|||
if res.success && (res.message == "Share created.") {
|
||||
trace!("new share id: {:?}", res.id);
|
||||
|
||||
Ok(res.id.into())
|
||||
Ok(res.id)
|
||||
} else {
|
||||
Err(crate::Error::response(format!("{res:?}")))
|
||||
}
|
||||
}
|
||||
|
||||
fn share_notify(&self, uri: &Uri, alias_id: &AliasID, share_id: &ShareID) -> crate::Result<()> {
|
||||
fn share_notify(&self, uri: &Uri, alias_id: &str, share_id: &str) -> crate::Result<()> {
|
||||
let res = {
|
||||
let endpoint = uri.share_notify(share_id);
|
||||
|
||||
let mut res = self
|
||||
.post(&endpoint)
|
||||
.header("Sharry-Alias", alias_id.as_ref())
|
||||
.header("Sharry-Alias", alias_id)
|
||||
.send_empty()
|
||||
.map_err(find_cause(uri, alias_id, Some(share_id), None))?;
|
||||
|
||||
|
|
@ -94,7 +94,7 @@ impl sharry::Client for ureq::Agent {
|
|||
crate::Error::res_status_check(res.status(), ureq::http::StatusCode::OK)?;
|
||||
|
||||
res.body_mut()
|
||||
.read_json::<sharry::json::NotifyShareResponse>()
|
||||
.read_json::<sharry::NotifyShareResponse>()
|
||||
.map_err(crate::Error::response)?
|
||||
};
|
||||
|
||||
|
|
@ -106,16 +106,16 @@ impl sharry::Client for ureq::Agent {
|
|||
fn file_create(
|
||||
&self,
|
||||
uri: &Uri,
|
||||
alias_id: &AliasID,
|
||||
share_id: &ShareID,
|
||||
alias_id: &str,
|
||||
share_id: &str,
|
||||
file: &file::Checked,
|
||||
) -> crate::Result<FileID> {
|
||||
) -> crate::Result<String> {
|
||||
let res = {
|
||||
let endpoint = uri.file_create(share_id);
|
||||
|
||||
let res = self
|
||||
.post(&endpoint)
|
||||
.header("Sharry-Alias", alias_id.as_ref())
|
||||
.header("Sharry-Alias", alias_id)
|
||||
.header("Sharry-File-Name", file.get_name())
|
||||
.header("Upload-Length", file.get_size())
|
||||
.send_empty()
|
||||
|
|
@ -132,14 +132,18 @@ impl sharry::Client for ureq::Agent {
|
|||
.map_err(crate::Error::response)?
|
||||
.to_string();
|
||||
|
||||
FileID::try_from(location)
|
||||
let file_id = Self::get_file_id(&location)?;
|
||||
|
||||
debug!("location: {location:?}, file_id: {file_id:?}");
|
||||
|
||||
Ok(file_id.to_owned())
|
||||
}
|
||||
|
||||
fn file_patch(
|
||||
&self,
|
||||
uri: &Uri,
|
||||
alias_id: &AliasID,
|
||||
share_id: &ShareID,
|
||||
alias_id: &str,
|
||||
share_id: &str,
|
||||
chunk: &file::Chunk,
|
||||
) -> crate::Result<()> {
|
||||
let res = {
|
||||
|
|
@ -147,7 +151,7 @@ impl sharry::Client for ureq::Agent {
|
|||
|
||||
let res = self
|
||||
.patch(&endpoint)
|
||||
.header("Sharry-Alias", alias_id.as_ref())
|
||||
.header("Sharry-Alias", alias_id)
|
||||
.header("Upload-Offset", chunk.get_offset())
|
||||
.send(chunk.get_data())
|
||||
.map_err(find_cause(
|
||||
|
|
|
|||
|
|
@ -33,15 +33,55 @@ impl Uri {
|
|||
self.endpoint(format_args!("alias/upload/new"))
|
||||
}
|
||||
|
||||
pub fn share_notify(&self, share_id: &super::ShareID) -> String {
|
||||
pub fn share_notify(&self, share_id: &str) -> String {
|
||||
self.endpoint(format_args!("alias/mail/notify/{share_id}"))
|
||||
}
|
||||
|
||||
pub fn file_create(&self, share_id: &super::ShareID) -> String {
|
||||
pub fn file_create(&self, share_id: &str) -> String {
|
||||
self.endpoint(format_args!("alias/upload/{share_id}/files/tus"))
|
||||
}
|
||||
|
||||
pub fn file_patch(&self, share_id: &super::ShareID, file_id: &super::FileID) -> String {
|
||||
pub fn file_patch(&self, share_id: &str, file_id: &str) -> String {
|
||||
self.endpoint(format_args!("alias/upload/{share_id}/files/tus/{file_id}"))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
#[allow(non_snake_case)]
|
||||
pub struct NewShareRequest {
|
||||
name: String,
|
||||
validity: u32,
|
||||
description: Option<String>,
|
||||
maxViews: u32,
|
||||
password: Option<String>,
|
||||
}
|
||||
|
||||
impl NewShareRequest {
|
||||
pub fn new(
|
||||
name: impl Into<String>,
|
||||
description: Option<impl Into<String>>,
|
||||
max_views: u32,
|
||||
) -> Self {
|
||||
Self {
|
||||
name: name.into(),
|
||||
validity: 0,
|
||||
description: description.map(Into::into),
|
||||
maxViews: max_views,
|
||||
password: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct NewShareResponse {
|
||||
pub success: bool,
|
||||
pub message: String,
|
||||
pub id: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[allow(dead_code)]
|
||||
pub struct NotifyShareResponse {
|
||||
pub success: bool,
|
||||
pub message: String,
|
||||
}
|
||||
87
src/sharry/client.rs
Normal file
87
src/sharry/client.rs
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
use std::sync::LazyLock;
|
||||
|
||||
use log::trace;
|
||||
use regex::Regex;
|
||||
|
||||
use crate::file;
|
||||
|
||||
use super::api::{NewShareRequest, Uri};
|
||||
|
||||
pub trait Client {
|
||||
fn get_file_id(uri: &str) -> crate::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(crate::Error::mismatch(
|
||||
"<proto>://<base>/api/v2/alias/upload/<share>/files/tus/<file>",
|
||||
uri,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn share_create(
|
||||
&self,
|
||||
uri: &Uri,
|
||||
alias_id: &str,
|
||||
data: NewShareRequest,
|
||||
) -> crate::Result<String>;
|
||||
|
||||
fn share_notify(&self, uri: &Uri, alias_id: &str, share_id: &str) -> crate::Result<()>;
|
||||
|
||||
fn file_create(
|
||||
&self,
|
||||
uri: &Uri,
|
||||
alias_id: &str,
|
||||
share_id: &str,
|
||||
file: &file::Checked,
|
||||
) -> crate::Result<String>;
|
||||
|
||||
fn file_patch(
|
||||
&self,
|
||||
uri: &Uri,
|
||||
alias_id: &str,
|
||||
share_id: &str,
|
||||
chunk: &file::Chunk,
|
||||
) -> crate::Result<()>;
|
||||
}
|
||||
|
||||
// TODO move into tests subdir
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::*;
|
||||
|
||||
// #[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());
|
||||
|
||||
// let bad: &'static str = "https://example.com/api/v2/alias/upload/SID123/files/tus/"; // missing FID
|
||||
// assert!(Client::get_file_id(bad).is_err());
|
||||
// }
|
||||
// }
|
||||
|
|
@ -1,155 +0,0 @@
|
|||
use std::{fmt, sync::LazyLock};
|
||||
|
||||
use log::{debug, trace};
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct AliasID(String);
|
||||
|
||||
impl fmt::Display for AliasID {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for AliasID {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
self.0.as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for AliasID {
|
||||
fn from(value: String) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct ShareID(String);
|
||||
|
||||
impl fmt::Display for ShareID {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for ShareID {
|
||||
fn from(value: String) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct FileID(String);
|
||||
|
||||
impl fmt::Display for FileID {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<String> for FileID {
|
||||
type Error = crate::Error;
|
||||
|
||||
fn try_from(value: String) -> crate::Result<Self> {
|
||||
/// 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")
|
||||
});
|
||||
|
||||
trace!("TryFrom {value:?}");
|
||||
|
||||
if let Some(fid) = UPLOAD_URL_RE
|
||||
.captures(&value)
|
||||
.and_then(|caps| caps.name("fid").map(|m| m.as_str()))
|
||||
{
|
||||
let result = Self(fid.to_owned());
|
||||
debug!("{result:?}");
|
||||
|
||||
Ok(result)
|
||||
} else {
|
||||
Err(crate::Error::mismatch(
|
||||
"<proto>://<base>/api/v2/alias/upload/<share>/files/tus/<file>",
|
||||
value,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn valid_urls_produce_expected_file_id() {
|
||||
// a handful of valid‐looking URLs
|
||||
let cases = vec![
|
||||
(
|
||||
"http://example.com/api/v2/alias/upload/SID123/files/tus/FID456",
|
||||
"FID456",
|
||||
),
|
||||
(
|
||||
"https://my-host:8080/api/v2/alias/upload/another-SID/files/tus/some-file-id",
|
||||
"some-file-id",
|
||||
),
|
||||
(
|
||||
"custom+scheme://host/api/v2/alias/upload/x/files/tus/y",
|
||||
"y",
|
||||
),
|
||||
];
|
||||
|
||||
for (good, expected_fid) in cases {
|
||||
let s = good.to_string();
|
||||
let file_id = FileID::try_from(s.clone()).expect("URL should parse successfully");
|
||||
assert_eq!(
|
||||
file_id.0, expected_fid,
|
||||
"Expected `{}` → FileID({}), got {:?}",
|
||||
good, expected_fid, file_id
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_urls_return_error() {
|
||||
let bad_inputs = vec![
|
||||
// missing /api/v2/alias/upload
|
||||
"http://example.com/files/tus/FID",
|
||||
// missing /files/tus
|
||||
"http://example.com/api/v2/alias/upload/SID123/FID456",
|
||||
// trailing slash (doesn't match `$`)
|
||||
"http://example.com/api/v2/alias/upload/SID/files/tus/FID/",
|
||||
// empty fid
|
||||
"http://example.com/api/v2/alias/upload/SID/files/tus/",
|
||||
// random string
|
||||
"just-a-random-string",
|
||||
];
|
||||
|
||||
for bad in bad_inputs {
|
||||
let err = FileID::try_from(bad.to_string()).expect_err("URL should not parse");
|
||||
// make sure it's the Mismatch variant, and that it contains the original input
|
||||
match err {
|
||||
crate::Error::Mismatch { expected, actual } => {
|
||||
assert_eq!(
|
||||
expected, "<proto>://<base>/api/v2/alias/upload/<share>/files/tus/<file>",
|
||||
"Error should output expected format"
|
||||
);
|
||||
assert_eq!(actual, bad.to_string(), "Error should echo back the input");
|
||||
}
|
||||
_ => panic!("Expected Error::Mismatch for input `{bad}` but got {err:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
#[allow(non_snake_case)]
|
||||
pub struct NewShareRequest {
|
||||
name: String,
|
||||
validity: u32,
|
||||
description: Option<String>,
|
||||
maxViews: u32,
|
||||
password: Option<String>,
|
||||
}
|
||||
|
||||
impl NewShareRequest {
|
||||
pub fn new(
|
||||
name: impl Into<String>,
|
||||
description: Option<impl Into<String>>,
|
||||
max_views: u32,
|
||||
) -> Self {
|
||||
Self {
|
||||
name: name.into(),
|
||||
validity: 0,
|
||||
description: description.map(Into::into),
|
||||
maxViews: max_views,
|
||||
password: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct NewShareResponse {
|
||||
pub success: bool,
|
||||
pub message: String,
|
||||
pub id: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[allow(dead_code)]
|
||||
pub struct NotifyShareResponse {
|
||||
pub success: bool,
|
||||
pub message: String,
|
||||
}
|
||||
|
|
@ -1,35 +1,5 @@
|
|||
mod ids;
|
||||
pub mod json;
|
||||
mod uri;
|
||||
mod api;
|
||||
mod client;
|
||||
|
||||
pub use ids::{AliasID, FileID, ShareID};
|
||||
pub use uri::Uri;
|
||||
|
||||
use crate::file;
|
||||
|
||||
pub trait Client {
|
||||
fn share_create(
|
||||
&self,
|
||||
uri: &Uri,
|
||||
alias_id: &AliasID,
|
||||
data: json::NewShareRequest,
|
||||
) -> crate::Result<ShareID>;
|
||||
|
||||
fn share_notify(&self, uri: &Uri, alias_id: &AliasID, share_id: &ShareID) -> crate::Result<()>;
|
||||
|
||||
fn file_create(
|
||||
&self,
|
||||
uri: &Uri,
|
||||
alias_id: &AliasID,
|
||||
share_id: &ShareID,
|
||||
file: &file::Checked,
|
||||
) -> crate::Result<FileID>;
|
||||
|
||||
fn file_patch(
|
||||
&self,
|
||||
uri: &Uri,
|
||||
alias_id: &AliasID,
|
||||
share_id: &ShareID,
|
||||
chunk: &file::Chunk,
|
||||
) -> crate::Result<()>;
|
||||
}
|
||||
pub use api::{NewShareRequest, NewShareResponse, NotifyShareResponse, Uri};
|
||||
pub use client::Client;
|
||||
|
|
|
|||
Loading…
Reference in a new issue