2025-06-04 13:25:00 +00:00
|
|
|
use std::{
|
2025-06-05 22:08:24 +00:00
|
|
|
fs, io,
|
2025-06-04 13:25:00 +00:00
|
|
|
path::{Path, PathBuf},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2025-06-27 01:55:43 +00:00
|
|
|
use crate::sharry;
|
2025-06-10 18:20:52 +00:00
|
|
|
|
|
|
|
|
use super::{FileTrait, Uploading};
|
2025-06-04 13:25:00 +00:00
|
|
|
|
2025-06-15 00:44:28 +00:00
|
|
|
/// Description of an existing, regular file
|
|
|
|
|
///
|
2025-06-28 09:24:46 +00:00
|
|
|
/// - impl `Clone` for `clap` compatibility
|
|
|
|
|
/// - impl `serde` for cachefile handling
|
|
|
|
|
/// - impl `PartialEq..Ord` to handle multiple files given
|
|
|
|
|
/// - impl `AsRef<[u8]>` for hashing with `blake2b_simd`
|
2025-06-24 16:29:38 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
2025-06-10 18:20:52 +00:00
|
|
|
pub struct Checked {
|
2025-06-15 00:44:28 +00:00
|
|
|
/// canonical path to a regular file
|
2025-06-18 19:40:34 +00:00
|
|
|
pub(super) path: PathBuf,
|
2025-06-15 00:44:28 +00:00
|
|
|
/// size of that file
|
2025-06-18 19:40:34 +00:00
|
|
|
pub(super) size: u64,
|
2025-06-24 19:34:11 +00:00
|
|
|
/// hash of that file
|
|
|
|
|
pub(super) hash: Option<String>,
|
2025-06-04 13:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 16:29:38 +00:00
|
|
|
impl AsRef<[u8]> for Checked {
|
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
|
self.path.as_os_str().as_encoded_bytes()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-10 18:20:52 +00:00
|
|
|
impl Checked {
|
2025-06-15 00:44:28 +00:00
|
|
|
/// create a new checked file from some path reference
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
///
|
2025-06-15 00:56:58 +00:00
|
|
|
/// - from `fs::metadata(path)` or `fs::canonicalize`
|
|
|
|
|
/// - given path does not correspond to a regular file
|
2025-06-04 13:25:00 +00:00
|
|
|
pub fn new(value: impl AsRef<Path>) -> io::Result<Self> {
|
|
|
|
|
let meta = fs::metadata(&value)?;
|
|
|
|
|
if meta.is_file() {
|
|
|
|
|
Ok(Self {
|
|
|
|
|
path: fs::canonicalize(&value)?,
|
2025-06-10 01:17:17 +00:00
|
|
|
size: meta.len(),
|
2025-06-24 19:34:11 +00:00
|
|
|
hash: None,
|
2025-06-04 13:25:00 +00:00
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
Err(io::Error::new(
|
2025-06-05 22:08:24 +00:00
|
|
|
io::ErrorKind::InvalidInput,
|
2025-06-04 13:25:00 +00:00
|
|
|
"Not a regular file",
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-10 18:20:52 +00:00
|
|
|
|
2025-07-03 18:27:41 +00:00
|
|
|
/// calculate and store hash for this file
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
///
|
|
|
|
|
/// - from `file::compute_hash`
|
|
|
|
|
/// - Mismatch if file already hashed
|
2025-06-27 01:55:43 +00:00
|
|
|
pub fn hash(&mut self, f: impl Fn(u64)) -> crate::Result<()> {
|
2025-06-24 19:34:11 +00:00
|
|
|
if self.hash.is_some() {
|
2025-06-27 01:55:43 +00:00
|
|
|
return Err(crate::Error::mismatch("unhashed file", self.path.display()));
|
2025-06-24 19:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-03 15:39:29 +00:00
|
|
|
self.hash = Some(super::compute_hash(&self.path, self.size, f)?);
|
2025-06-24 19:34:11 +00:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-03 18:27:41 +00:00
|
|
|
/// starts uploading this file
|
2025-06-15 00:44:28 +00:00
|
|
|
///
|
2025-07-03 18:27:41 +00:00
|
|
|
/// - tries to create a new file using the client
|
2025-06-15 00:44:28 +00:00
|
|
|
/// - consumes `self` into a `file::Uploading` struct
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
///
|
2025-07-03 18:27:41 +00:00
|
|
|
/// - from `sharry::Client::file_create`
|
2025-06-10 18:20:52 +00:00
|
|
|
pub fn start_upload(
|
|
|
|
|
self,
|
|
|
|
|
client: &impl sharry::Client,
|
2025-06-18 13:09:34 +00:00
|
|
|
uri: &sharry::Uri,
|
2025-06-27 01:47:38 +00:00
|
|
|
alias_id: &sharry::AliasID,
|
|
|
|
|
share_id: &sharry::ShareID,
|
2025-06-27 01:55:43 +00:00
|
|
|
) -> crate::Result<Uploading> {
|
2025-06-18 13:09:34 +00:00
|
|
|
let file_id = client.file_create(uri, alias_id, share_id, &self)?;
|
2025-06-10 18:20:52 +00:00
|
|
|
|
2025-06-24 19:34:11 +00:00
|
|
|
Ok(Uploading::new(self.path, self.size, self.hash, file_id))
|
2025-06-10 18:20:52 +00:00
|
|
|
}
|
2025-06-04 13:25:00 +00:00
|
|
|
}
|
2025-06-06 15:21:49 +00:00
|
|
|
|
2025-07-03 18:35:50 +00:00
|
|
|
impl FileTrait for Checked {
|
|
|
|
|
fn get_name(&self) -> &str {
|
2025-06-10 18:20:52 +00:00
|
|
|
<Self as FileTrait>::extract_file_name(&self.path)
|
2025-06-06 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 23:42:18 +00:00
|
|
|
fn get_size(&self) -> u64 {
|
2025-06-10 01:17:17 +00:00
|
|
|
self.size
|
2025-06-06 15:21:49 +00:00
|
|
|
}
|
2025-06-24 19:34:11 +00:00
|
|
|
|
2025-07-03 18:27:41 +00:00
|
|
|
fn check_hash(&self, on_progress: impl FnMut(u64)) -> crate::Result<()> {
|
2025-07-03 16:18:46 +00:00
|
|
|
super::check_hash(
|
|
|
|
|
&self.path,
|
|
|
|
|
self.size,
|
|
|
|
|
self.hash.as_ref().map(String::as_str),
|
|
|
|
|
on_progress,
|
|
|
|
|
)
|
2025-06-24 19:34:11 +00:00
|
|
|
}
|
2025-06-06 15:21:49 +00:00
|
|
|
}
|