use std::{fmt, io, path::PathBuf, time::Duration}; use console::style; use indicatif::{ProgressBar, ProgressStyle}; use log::{debug, trace}; use serde::{Deserialize, Serialize}; use super::{ cli::Cli, file::FileTrait, savedstate::SavedState, sharry::{self, Client, ClientError}, }; #[derive(Serialize, Deserialize)] pub struct AppState { #[serde(skip)] progress: Option, #[serde(skip)] buffer: Vec, inner: SavedState, } impl fmt::Debug for AppState { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("AppState") .field("inner", &self.inner) .finish() } } impl AppState { fn cache_dir() -> PathBuf { let dir_name = dirs_next::cache_dir() .expect("could not determine cache directory") .join("shrupl"); trace!("cachedir: {:?}", dir_name.display()); dir_name } fn cache_file(args: &Cli) -> PathBuf { let file_name = Self::cache_dir().join(format!("{}.json", args.get_hash())); trace!("cachefile: {:?}", file_name.display()); file_name } fn new(chunk_size: usize, inner: SavedState) -> Self { Self { progress: None, buffer: vec![0; chunk_size * 1024 * 1024], inner, } } pub fn try_resume(args: &Cli) -> Option { let file_name = Self::cache_file(args); let inner = SavedState::load(&file_name) .inspect_err(|e| debug!("could not resume from {:?}: {e}", file_name.display())) .ok()?; Some(Self::new(args.chunk_size, inner)) } pub fn from_args(args: &Cli, http: &impl Client) -> sharry::Result { let uri = args.get_uri(); let share_id = http.share_create( &uri.endpoint("alias/upload/new"), &args.alias, args.get_share_request(), )?; Ok(Self::new( args.chunk_size, SavedState::new( Self::cache_file(&args), uri, args.alias.clone(), share_id, &args.files, ), )) } pub fn file_names(&self) -> Vec<&str> { self.inner.file_names() } pub fn upload_chunk(&mut self, http: &impl Client) -> sharry::Result> { let Some(mut uploading) = self.inner.pop_file(http) else { return Ok(None); }; debug!("{uploading} chunk {}", self.buffer.len()); // Initialize or fetch the existing ProgressBar let bar = &*self.progress.get_or_insert_with(|| { // Create a new bar with style let bar = ProgressBar::new(uploading.get_size()) .with_style( ProgressStyle::with_template(&format!( concat!( "{{msg:.yellow}}: {{bar:50.cyan/blue}} ", "{{binary_bytes:.magenta}}{}{{binary_total_bytes:.magenta}} ", "({{eta}})", ), style("/").magenta(), )) .unwrap(), ) .with_message(uploading.get_name().to_owned()) .with_position(uploading.get_offset()); bar.enable_steady_tick(Duration::from_millis(100)); bar }); let chunk = uploading .read(&mut self.buffer) .map_err(ClientError::from)?; if chunk.get_length() == 0 { return Err(ClientError::req_err("wtf")); } http.file_patch( chunk.get_patch_uri(), &self.alias_id, chunk.get_offset(), chunk.get_data(), )?; match uploading.check_eof() { Ok(uploading) => { bar.set_position(uploading.get_offset()); self.inner.push_file(uploading); Ok(Some(())) } Err(path) => { debug!("Finished {:?}!", path.display()); bar.finish(); self.progress = None; let endpoint = self .uri .endpoint(format!("alias/mail/notify/{}", self.share_id)); http.share_notify(&endpoint, &self.alias_id).unwrap(); // HACK unwrap Ok(self.inner.has_file().then_some(())) } } } pub fn save(&self) -> io::Result<()> { self.inner.save() } pub fn clear(self) -> io::Result<()> { self.inner.clear() } }