add id::{AliasID, ShareID}

This commit is contained in:
Jörn-Michael Miehe 2025-06-27 01:47:38 +00:00
parent d8c48b74ca
commit c7b24b1250
11 changed files with 82 additions and 44 deletions

View file

@ -9,7 +9,7 @@ use crate::{
error, error,
file::{Chunk, FileTrait}, file::{Chunk, FileTrait},
output::new_progressbar, output::new_progressbar,
sharry::Client, sharry::{Client, ShareID},
}; };
pub struct AppState { pub struct AppState {
@ -33,7 +33,7 @@ fn new_http(args: &Cli) -> ureq::Agent {
.into() .into()
} }
fn new_share(args: &Cli) -> error::Result<String> { fn new_share(args: &Cli) -> error::Result<ShareID> {
new_http(args).share_create(&args.get_uri(), &args.alias, args.get_share_request()) new_http(args).share_create(&args.get_uri(), &args.alias, args.get_share_request())
} }

View file

@ -15,7 +15,7 @@ use crate::{
error, error,
file::{self, Chunk, FileTrait}, file::{self, Chunk, FileTrait},
output::new_progressbar, output::new_progressbar,
sharry::{Client, Uri}, sharry::{AliasID, Client, ShareID, Uri},
}; };
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -24,8 +24,8 @@ pub struct CacheFile {
file_name: PathBuf, file_name: PathBuf,
uri: Uri, uri: Uri,
alias_id: String, alias_id: AliasID,
share_id: String, share_id: ShareID,
uploading: Option<file::Uploading>, uploading: Option<file::Uploading>,
files: VecDeque<file::Checked>, files: VecDeque<file::Checked>,
@ -98,7 +98,7 @@ impl CacheFile {
pub fn from_args( pub fn from_args(
args: &Cli, args: &Cli,
new_share: impl FnOnce(&Cli) -> error::Result<String>, new_share: impl FnOnce(&Cli) -> error::Result<ShareID>,
) -> error::Result<Self> { ) -> error::Result<Self> {
let mut files = args.files.clone(); let mut files = args.files.clone();

View file

@ -11,7 +11,7 @@ use log::LevelFilter;
use crate::{ use crate::{
file::{Checked, FileTrait}, file::{Checked, FileTrait},
sharry::{NewShareRequest, Uri}, sharry::{AliasID, NewShareRequest, Uri},
}; };
#[derive(Parser)] #[derive(Parser)]
@ -69,7 +69,7 @@ pub struct Cli {
url: String, url: String,
/// ID of a public alias to use /// ID of a public alias to use
pub alias: String, pub alias: AliasID,
/// Files to upload to the new share /// Files to upload to the new share
#[arg(value_name = "FILE", required = true, value_parser = parse_sharry_file)] #[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(); let mut hasher = Blake2b::new().hash_length(16).to_state();
hasher.update(self.get_uri().as_ref()); hasher.update(self.get_uri().as_ref());
hasher.update(self.alias.as_bytes()); hasher.update(self.alias.as_ref());
for chk in sorted(&self.files) { for chk in sorted(&self.files) {
hasher.update(chk.as_ref()); hasher.update(chk.as_ref());

View file

@ -8,10 +8,10 @@ pub enum Parameter {
Uri(String), Uri(String),
#[error("given Alias ID {0:?}")] #[error("given Alias ID {0:?}")]
AliasID(String), AliasID(sharry::AliasID),
#[error("stored Share ID {0:?}")] #[error("stored Share ID {0:?}")]
ShareID(String), ShareID(sharry::ShareID),
#[error("stored {0:?}")] #[error("stored {0:?}")]
FileID(sharry::FileID), FileID(sharry::FileID),

View file

@ -76,8 +76,8 @@ impl Checked {
self, self,
client: &impl sharry::Client, client: &impl sharry::Client,
uri: &sharry::Uri, uri: &sharry::Uri,
alias_id: &str, alias_id: &sharry::AliasID,
share_id: &str, share_id: &sharry::ShareID,
) -> error::Result<Uploading> { ) -> error::Result<Uploading> {
let file_id = client.file_create(uri, alias_id, share_id, &self)?; let file_id = client.file_create(uri, alias_id, share_id, &self)?;

View file

@ -3,13 +3,13 @@ use log::{debug, trace};
use crate::{ use crate::{
error, error,
file::{self, FileTrait}, file::{self, FileTrait},
sharry::{self, FileID, Uri}, sharry::{self, AliasID, FileID, ShareID, Uri},
}; };
fn find_cause( fn find_cause(
uri: &Uri, uri: &Uri,
alias_id: &str, alias_id: &AliasID,
share_id: Option<&str>, share_id: Option<&ShareID>,
file_id: Option<&FileID>, file_id: Option<&FileID>,
) -> impl FnOnce(ureq::Error) -> error::Error { ) -> impl FnOnce(ureq::Error) -> error::Error {
move |error| match error { move |error| match error {
@ -22,7 +22,7 @@ fn find_cause(
trace!("HTTP Error 404: Share and/or file may have been deleted!"); trace!("HTTP Error 404: Share and/or file may have been deleted!");
if let Some(file_id) = file_id { if let Some(file_id) = file_id {
error::Error::InvalidParameter(error::Parameter::FileID(file_id.clone())) error::Error::InvalidParameter(error::Parameter::FileID(file_id.to_owned()))
} else if let Some(share_id) = share_id { } else if let Some(share_id) = share_id {
error::Error::InvalidParameter(error::Parameter::ShareID(share_id.to_owned())) error::Error::InvalidParameter(error::Parameter::ShareID(share_id.to_owned()))
} else { } else {
@ -50,15 +50,15 @@ impl sharry::Client for ureq::Agent {
fn share_create( fn share_create(
&self, &self,
uri: &Uri, uri: &Uri,
alias_id: &str, alias_id: &AliasID,
data: sharry::NewShareRequest, data: sharry::NewShareRequest,
) -> error::Result<String> { ) -> error::Result<ShareID> {
let res = { let res = {
let endpoint = uri.share_create(); let endpoint = uri.share_create();
let mut res = self let mut res = self
.post(&endpoint) .post(&endpoint)
.header("Sharry-Alias", alias_id) .header("Sharry-Alias", alias_id.as_ref())
.send_json(data) .send_json(data)
.map_err(find_cause(uri, alias_id, None, None))?; .map_err(find_cause(uri, alias_id, None, None))?;
@ -75,19 +75,19 @@ impl sharry::Client for ureq::Agent {
if res.success && (res.message == "Share created.") { if res.success && (res.message == "Share created.") {
trace!("new share id: {:?}", res.id); trace!("new share id: {:?}", res.id);
Ok(res.id) Ok(res.id.into())
} else { } else {
Err(error::Error::response(format!("{res:?}"))) Err(error::Error::response(format!("{res:?}")))
} }
} }
fn share_notify(&self, uri: &Uri, alias_id: &str, share_id: &str) -> error::Result<()> { fn share_notify(&self, uri: &Uri, alias_id: &AliasID, share_id: &ShareID) -> error::Result<()> {
let res = { let res = {
let endpoint = uri.share_notify(share_id); let endpoint = uri.share_notify(share_id);
let mut res = self let mut res = self
.post(&endpoint) .post(&endpoint)
.header("Sharry-Alias", alias_id) .header("Sharry-Alias", alias_id.as_ref())
.send_empty() .send_empty()
.map_err(find_cause(uri, alias_id, Some(share_id), None))?; .map_err(find_cause(uri, alias_id, Some(share_id), None))?;
@ -107,8 +107,8 @@ impl sharry::Client for ureq::Agent {
fn file_create( fn file_create(
&self, &self,
uri: &Uri, uri: &Uri,
alias_id: &str, alias_id: &AliasID,
share_id: &str, share_id: &ShareID,
file: &file::Checked, file: &file::Checked,
) -> error::Result<FileID> { ) -> error::Result<FileID> {
let res = { let res = {
@ -116,7 +116,7 @@ impl sharry::Client for ureq::Agent {
let res = self let res = self
.post(&endpoint) .post(&endpoint)
.header("Sharry-Alias", alias_id) .header("Sharry-Alias", alias_id.as_ref())
.header("Sharry-File-Name", file.get_name()) .header("Sharry-File-Name", file.get_name())
.header("Upload-Length", file.get_size()) .header("Upload-Length", file.get_size())
.send_empty() .send_empty()
@ -139,8 +139,8 @@ impl sharry::Client for ureq::Agent {
fn file_patch( fn file_patch(
&self, &self,
uri: &Uri, uri: &Uri,
alias_id: &str, alias_id: &AliasID,
share_id: &str, share_id: &ShareID,
chunk: &file::Chunk, chunk: &file::Chunk,
) -> error::Result<()> { ) -> error::Result<()> {
let res = { let res = {
@ -148,7 +148,7 @@ impl sharry::Client for ureq::Agent {
let res = self let res = self
.patch(&endpoint) .patch(&endpoint)
.header("Sharry-Alias", alias_id) .header("Sharry-Alias", alias_id.as_ref())
.header("Upload-Offset", chunk.get_offset()) .header("Upload-Offset", chunk.get_offset())
.send(chunk.get_data()) .send(chunk.get_data())
.map_err(find_cause( .map_err(find_cause(

View file

@ -6,8 +6,41 @@ use serde::{Deserialize, Serialize};
use crate::error; use crate::error;
// pub struct AliasID(String); #[derive(Serialize, Deserialize, Debug, Clone)]
// pub struct ShareID(String); 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)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FileID(String); pub struct FileID(String);

View file

@ -2,6 +2,6 @@ mod id;
mod json; mod json;
mod uri; mod uri;
pub use id::FileID; pub use id::{AliasID, FileID, ShareID};
pub use json::{NewShareRequest, NewShareResponse, NotifyShareResponse}; pub use json::{NewShareRequest, NewShareResponse, NotifyShareResponse};
pub use uri::Uri; pub use uri::Uri;

View file

@ -33,15 +33,15 @@ impl Uri {
self.endpoint(format_args!("alias/upload/new")) self.endpoint(format_args!("alias/upload/new"))
} }
pub fn share_notify(&self, share_id: &str) -> String { pub fn share_notify(&self, share_id: &super::ShareID) -> String {
self.endpoint(format_args!("alias/mail/notify/{share_id}")) self.endpoint(format_args!("alias/mail/notify/{share_id}"))
} }
pub fn file_create(&self, share_id: &str) -> String { pub fn file_create(&self, share_id: &super::ShareID) -> String {
self.endpoint(format_args!("alias/upload/{share_id}/files/tus")) self.endpoint(format_args!("alias/upload/{share_id}/files/tus"))
} }
pub fn file_patch(&self, share_id: &str, file_id: &super::FileID) -> String { pub fn file_patch(&self, share_id: &super::ShareID, file_id: &super::FileID) -> String {
self.endpoint(format_args!("alias/upload/{share_id}/files/tus/{file_id}")) self.endpoint(format_args!("alias/upload/{share_id}/files/tus/{file_id}"))
} }
} }

View file

@ -1,30 +1,33 @@
use crate::{error, file}; use crate::{error, file};
use super::api::{FileID, NewShareRequest, Uri}; use super::{
AliasID, ShareID,
api::{FileID, NewShareRequest, Uri},
};
pub trait Client { pub trait Client {
fn share_create( fn share_create(
&self, &self,
uri: &Uri, uri: &Uri,
alias_id: &str, alias_id: &AliasID,
data: NewShareRequest, data: NewShareRequest,
) -> error::Result<String>; ) -> error::Result<ShareID>;
fn share_notify(&self, uri: &Uri, alias_id: &str, share_id: &str) -> error::Result<()>; fn share_notify(&self, uri: &Uri, alias_id: &AliasID, share_id: &ShareID) -> error::Result<()>;
fn file_create( fn file_create(
&self, &self,
uri: &Uri, uri: &Uri,
alias_id: &str, alias_id: &AliasID,
share_id: &str, share_id: &ShareID,
file: &file::Checked, file: &file::Checked,
) -> error::Result<FileID>; ) -> error::Result<FileID>;
fn file_patch( fn file_patch(
&self, &self,
uri: &Uri, uri: &Uri,
alias_id: &str, alias_id: &AliasID,
share_id: &str, share_id: &ShareID,
chunk: &file::Chunk, chunk: &file::Chunk,
) -> error::Result<()>; ) -> error::Result<()>;
} }

View file

@ -1,5 +1,7 @@
mod api; mod api;
mod client; mod client;
pub use api::{FileID, NewShareRequest, NewShareResponse, NotifyShareResponse, Uri}; pub use api::{
AliasID, FileID, NewShareRequest, NewShareResponse, NotifyShareResponse, ShareID, Uri,
};
pub use client::Client; pub use client::Client;