Compare commits

...

1 commit

Author SHA1 Message Date
351596d56d implement better hashing
- call `file::Checked::hash` in `CacheFile::from_args` instead
2025-06-24 21:58:18 +00:00
3 changed files with 49 additions and 53 deletions

View file

@ -1,13 +1,13 @@
use std::{fmt, io, time::Duration}; use std::{fmt, io, time::Duration};
use console::style; use indicatif::{ProgressBar, ProgressDrawTarget};
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
use log::{debug, warn}; use log::{debug, warn};
use crate::{ use crate::{
cachefile::CacheFile, cachefile::CacheFile,
cli::Cli, cli::Cli,
file::{Chunk, FileTrait}, file::{Chunk, FileTrait},
output::new_progressbar,
sharry::{self, Client}, sharry::{self, Client},
}; };
@ -32,20 +32,6 @@ fn new_http(timeout: Option<Duration>) -> ureq::Agent {
.into() .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 { impl AppState {
fn new(http: ureq::Agent, inner: CacheFile) -> Self { fn new(http: ureq::Agent, inner: CacheFile) -> Self {
Self { Self {
@ -60,38 +46,17 @@ impl AppState {
.inspect_err(|e| debug!("could not resume from hash {:?}: {e}", args.get_hash())) .inspect_err(|e| debug!("could not resume from hash {:?}: {e}", args.get_hash()))
.ok()?; .ok()?;
inner.peek_uploading();
Some(Self::new(new_http(args.get_timeout()), inner)) Some(Self::new(new_http(args.get_timeout()), inner))
} }
pub fn from_args(args: &Cli) -> sharry::Result<Self> { 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 http = new_http(args.get_timeout());
let share_id = http.share_create(&args.get_uri(), &args.alias, args.get_share_request())?; let share_id = http.share_create(&args.get_uri(), &args.alias, args.get_share_request())?;
Ok(Self::new( Ok(Self::new(http, CacheFile::from_args(args, share_id)?))
http,
CacheFile::from_args(args, share_id).replace_files(files),
))
} }
fn with_progressbar(&mut self, f: impl FnOnce(&ProgressBar), drop_bar: bool) { fn with_progressbar(&mut self, f: impl FnOnce(&ProgressBar), drop_bar: bool) {
@ -179,7 +144,7 @@ impl AppState {
self.http self.http
.share_create(&args.get_uri(), &args.alias, args.get_share_request())?; .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<()> { pub fn save(&self) -> io::Result<()> {

View file

@ -3,14 +3,17 @@ use std::{
fs, fs,
io::{self, Write}, io::{self, Write},
path::PathBuf, path::PathBuf,
time::Duration,
}; };
use log::trace; use indicatif::ProgressDrawTarget;
use log::{debug, trace};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
cli::Cli, cli::Cli,
file::{self, Chunk}, file::{self, Chunk, FileTrait},
output::new_progressbar,
sharry::{self, Client, Uri}, sharry::{self, Client, Uri},
}; };
@ -56,22 +59,35 @@ impl CacheFile {
Ok(Self { file_name, ..state }) Ok(Self { file_name, ..state })
} }
pub fn from_args(args: &Cli, share_id: String) -> Self { pub fn from_args(args: &Cli, share_id: String) -> io::Result<Self> {
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), file_name: Self::cache_file(args),
uri: args.get_uri(), uri: args.get_uri(),
alias_id: args.alias.clone(), alias_id: args.alias.clone(),
share_id, share_id,
uploading: None, uploading: None,
files: args.files.clone().into(),
}
}
pub fn replace_files(self, files: Vec<file::Checked>) -> Self {
Self {
files: files.into(), files: files.into(),
..self })
}
} }
pub fn queue_empty(&self) -> bool { pub fn queue_empty(&self) -> bool {

View file

@ -2,6 +2,7 @@ use std::{fmt, process, sync::LazyLock};
use console::{StyledObject, style}; use console::{StyledObject, style};
use dialoguer::{Select, theme::ColorfulTheme}; use dialoguer::{Select, theme::ColorfulTheme};
use indicatif::{ProgressBar, ProgressStyle};
use log::{error, info}; use log::{error, info};
use crate::sharry; use crate::sharry;
@ -43,6 +44,20 @@ where
strs.iter().map(|&s| f(style(s)).to_string()).collect() 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 {} pub enum Log {}
impl Log { impl Log {