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,
file::{Chunk, FileTrait},
output::new_progressbar,
sharry::Client,
sharry::{Client, ShareID},
};
pub struct AppState {
@ -33,7 +33,7 @@ fn new_http(args: &Cli) -> ureq::Agent {
.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())
}

View file

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

View file

@ -11,7 +11,7 @@ use log::LevelFilter;
use crate::{
file::{Checked, FileTrait},
sharry::{NewShareRequest, Uri},
sharry::{AliasID, NewShareRequest, Uri},
};
#[derive(Parser)]
@ -69,7 +69,7 @@ pub struct Cli {
url: String,
/// ID of a public alias to use
pub alias: String,
pub alias: AliasID,
/// 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_bytes());
hasher.update(self.alias.as_ref());
for chk in sorted(&self.files) {
hasher.update(chk.as_ref());

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -33,15 +33,15 @@ impl Uri {
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}"))
}
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"))
}
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}"))
}
}

View file

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

View file

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