use std::{fmt, io, time::Duration}; use indicatif::ProgressBar; use log::{debug, warn}; use crate::{ cachefile::CacheFile, cli::Cli, file::{Chunk, FileTrait}, output::new_progressbar, sharry::Client, }; pub struct AppState { progress: Option, 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) -> crate::Result { 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) -> crate::Result { Ok(Self::new(new_http(args), CacheFile::try_resume(args)?)) } pub fn from_args(args: &Cli) -> crate::Result { 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_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 like e.g. `inc_length` } 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]) -> crate::Result>> { 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]) -> crate::Result { 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.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) -> crate::Result { 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); } }