shrupl/src/appstate.rs
Jörn-Michael Miehe 465c857126 implement better hashing
- move hashing back into `cachefile` to correctly handle the `rebuild_share` case
- add `-n` / `--no-hash` CLI switch to explicitly skip hashing
2025-06-25 22:47:55 +00:00

156 lines
4.1 KiB
Rust

use std::{fmt, io, time::Duration};
use indicatif::{ProgressBar, ProgressDrawTarget};
use log::{debug, warn};
use crate::{
cachefile::CacheFile,
cli::Cli,
error,
file::{Chunk, FileTrait},
output::new_progressbar,
sharry::Client,
};
pub struct AppState {
progress: Option<ProgressBar>,
http: ureq::Agent,
inner: CacheFile,
}
impl fmt::Debug for AppState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AppState")
.field("inner", &self.inner)
.finish_non_exhaustive()
}
}
fn new_http(args: &Cli) -> ureq::Agent {
ureq::Agent::config_builder()
.timeout_global(args.get_timeout())
.build()
.into()
}
fn new_share(args: &Cli) -> error::Result<String> {
new_http(args).share_create(&args.get_uri(), &args.alias, args.get_share_request())
}
impl AppState {
fn new(http: ureq::Agent, inner: CacheFile) -> Self {
Self {
progress: None,
http,
inner,
}
}
pub fn try_resume(args: &Cli) -> error::Result<Self> {
Ok(Self::new(new_http(args), CacheFile::try_resume(args)?))
}
pub fn from_args(args: &Cli) -> error::Result<Self> {
Ok(Self::new(
new_http(args),
CacheFile::from_args(args, new_share)?,
))
}
fn with_progressbar(&mut self, f: impl FnOnce(&ProgressBar), drop_bar: bool) {
let bar = &*self.progress.get_or_insert_with(new_progressbar);
if let Some(upl) = self.inner.peek_uploading() {
if bar.length().is_none() {
bar.set_draw_target(ProgressDrawTarget::stderr());
bar.set_length(upl.get_size());
bar.set_message(upl.get_name().to_owned());
bar.enable_steady_tick(Duration::from_millis(100));
}
bar.set_position(upl.get_offset());
// BUG in `indicatif` crate?
// `set_position` does not force an immediate redraw, so we also call `inc_length` here
bar.inc_length(0);
}
f(bar);
if drop_bar {
self.progress = None;
}
}
fn touch_progressbar(&mut self) {
self.with_progressbar(|_| (), false);
}
fn drop_progressbar(&mut self, f: impl FnOnce(&ProgressBar)) {
self.with_progressbar(f, true);
}
fn next_chunk<'t>(&mut self, buffer: &'t mut [u8]) -> error::Result<Option<Chunk<'t>>> {
if self.inner.get_uploading(&self.http)?.is_none() {
return Ok(None);
}
self.touch_progressbar();
let uploading = self.inner.expect_uploading();
debug!("{uploading:?}");
let chunk = uploading.read(buffer)?;
debug!("{chunk:?}");
Ok(Some(chunk))
}
pub fn upload_chunk(&mut self, buffer: &mut [u8]) -> error::Result<bool> {
let Some(chunk) = self.next_chunk(buffer)? else {
self.inner
.share_notify(&self.http)
.unwrap_or_else(|e| warn!("Failed to notify the share: {e}"));
return Ok(true);
};
self.inner.file_patch(&self.http, &chunk)?;
self.touch_progressbar();
if let Some(path) = self.inner.check_eof() {
debug!("Finished {:?}!", path.display());
self.drop_progressbar(ProgressBar::finish);
}
Ok(self.inner.peek_uploading().is_none() && self.inner.queue().is_empty())
}
#[must_use]
pub fn rewind_chunk(mut self) -> Option<Self> {
self.inner = self.inner.rewind_chunk()?;
Some(self)
}
pub fn abort_upload(&mut self) {
self.inner.abort_upload();
self.drop_progressbar(ProgressBar::abandon);
}
pub fn rebuild_share(self, args: &Cli) -> error::Result<Self> {
Ok(Self::new(self.http, CacheFile::from_args(args, new_share)?))
}
pub fn save(&self) -> io::Result<()> {
self.inner.save()
}
pub fn discard(self) -> io::Result<()> {
self.inner.discard()
}
pub fn clear_any(args: &Cli) {
CacheFile::clear_any(args);
}
}