implement better hashing

- call `file::Checked::hash` in `CacheFile::from_args` instead
This commit is contained in:
Jörn-Michael Miehe 2025-06-24 21:58:18 +00:00
parent ea5ef1fa10
commit 11a5106473
3 changed files with 47 additions and 53 deletions

View file

@ -1,13 +1,13 @@
use std::{fmt, io, time::Duration};
use console::style;
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
use indicatif::{ProgressBar, ProgressDrawTarget};
use log::{debug, warn};
use crate::{
cachefile::CacheFile,
cli::Cli,
file::{Chunk, FileTrait},
output::new_progressbar,
sharry::{self, Client},
};
@ -32,20 +32,6 @@ fn new_http(timeout: Option<Duration>) -> ureq::Agent {
.into()
}
fn new_progressbar() -> ProgressBar {
ProgressBar::hidden().with_style(
ProgressStyle::with_template(&format!(
concat!(
"{{bar:50.cyan/blue}} {{msg:.magenta}}: ",
"{{binary_bytes:.yellow}}{}{{binary_total_bytes:.yellow}} ",
"({{eta}})",
),
style("/").magenta(),
))
.expect("invalid style template"),
)
}
impl AppState {
fn new(http: ureq::Agent, inner: CacheFile) -> Self {
Self {
@ -64,34 +50,11 @@ impl AppState {
}
pub fn from_args(args: &Cli) -> sharry::Result<Self> {
let mut files = args.files.clone();
// TODO CLI switch begin
let bar = new_progressbar();
bar.set_draw_target(ProgressDrawTarget::stderr());
// BOOKMARK assumption: total file size < 2 EiB
bar.set_length(files.iter().map(|f| f.get_size()).sum());
bar.enable_steady_tick(Duration::from_millis(50));
for chk in &mut files {
bar.set_message(format!("hashing {:?}", chk.get_name()));
chk.hash(|bytes| bar.inc(bytes))?;
debug!("{chk:?}");
}
bar.finish();
// TODO CLI switch end
let http = new_http(args.get_timeout());
let share_id = http.share_create(&args.get_uri(), &args.alias, args.get_share_request())?;
Ok(Self::new(
http,
CacheFile::from_args(args, share_id).replace_files(files),
))
Ok(Self::new(http, CacheFile::from_args(args, share_id)?))
}
fn with_progressbar(&mut self, f: impl FnOnce(&ProgressBar), drop_bar: bool) {
@ -179,7 +142,7 @@ impl AppState {
self.http
.share_create(&args.get_uri(), &args.alias, args.get_share_request())?;
Ok(Self::new(self.http, CacheFile::from_args(args, share_id)))
Ok(Self::new(self.http, CacheFile::from_args(args, share_id)?))
}
pub fn save(&self) -> io::Result<()> {

View file

@ -3,14 +3,17 @@ use std::{
fs,
io::{self, Write},
path::PathBuf,
time::Duration,
};
use log::trace;
use indicatif::ProgressDrawTarget;
use log::{debug, trace};
use serde::{Deserialize, Serialize};
use crate::{
cli::Cli,
file::{self, Chunk},
file::{self, Chunk, FileTrait},
output::new_progressbar,
sharry::{self, Client, Uri},
};
@ -56,22 +59,35 @@ impl CacheFile {
Ok(Self { file_name, ..state })
}
pub fn from_args(args: &Cli, share_id: String) -> Self {
Self {
pub fn from_args(args: &Cli, share_id: String) -> io::Result<Self> {
// TODO CLI switch begin
let mut files = args.files.clone();
let bar = new_progressbar();
bar.set_draw_target(ProgressDrawTarget::stderr());
// BOOKMARK assumption: total file size < 2 EiB
bar.set_length(files.iter().map(|f| f.get_size()).sum());
bar.enable_steady_tick(Duration::from_millis(50));
for chk in &mut files {
bar.set_message(format!("hashing {:?}", chk.get_name()));
chk.hash(|bytes| bar.inc(bytes))?;
debug!("{chk:?}");
}
bar.finish_with_message("finished hashing files");
// TODO CLI switch end
Ok(Self {
file_name: Self::cache_file(args),
uri: args.get_uri(),
alias_id: args.alias.clone(),
share_id,
uploading: None,
files: args.files.clone().into(),
}
}
pub fn replace_files(self, files: Vec<file::Checked>) -> Self {
Self {
files: files.into(),
..self
}
})
}
pub fn queue_empty(&self) -> bool {

View file

@ -2,6 +2,7 @@ use std::{fmt, process, sync::LazyLock};
use console::{StyledObject, style};
use dialoguer::{Select, theme::ColorfulTheme};
use indicatif::{ProgressBar, ProgressStyle};
use log::{error, info};
use crate::sharry;
@ -43,6 +44,20 @@ where
strs.iter().map(|&s| f(style(s)).to_string()).collect()
}
pub fn new_progressbar() -> ProgressBar {
ProgressBar::hidden().with_style(
ProgressStyle::with_template(&format!(
concat!(
"{{bar:50.cyan/blue}} {{msg:.magenta}}: ",
"{{binary_bytes:.yellow}}{}{{binary_total_bytes:.yellow}} ",
"({{eta}})",
),
style("/").magenta(),
))
.expect("invalid style template"),
)
}
pub enum Log {}
impl Log {