shrupl/src/appstate.rs

154 lines
3.9 KiB
Rust
Raw Normal View History

2025-06-24 01:35:21 +00:00
use std::{fmt, io, time::Duration};
2025-06-02 23:57:17 +00:00
2025-06-26 00:08:18 +00:00
use indicatif::ProgressBar;
use log::{debug, warn};
2025-06-02 23:57:17 +00:00
use crate::{
cachefile::CacheFile,
2025-06-02 23:57:17 +00:00
cli::Cli,
file::{Chunk, FileTrait},
output::new_progressbar,
sharry::Client,
2025-06-02 23:57:17 +00:00
};
pub struct AppState {
2025-06-24 01:35:21 +00:00
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()
}
2025-06-27 01:55:43 +00:00
fn new_share(args: &Cli) -> crate::Result<String> {
new_http(args).share_create(&args.get_uri(), &args.alias, args.get_share_request())
}
2025-06-02 23:57:17 +00:00
impl AppState {
fn new(http: ureq::Agent, inner: CacheFile) -> Self {
Self {
2025-06-24 01:35:21 +00:00
progress: None,
http,
inner,
}
}
2025-06-27 01:55:43 +00:00
pub fn try_resume(args: &Cli) -> crate::Result<Self> {
Ok(Self::new(new_http(args), CacheFile::try_resume(args)?))
2025-06-04 21:02:35 +00:00
}
2025-06-27 01:55:43 +00:00
pub fn from_args(args: &Cli) -> crate::Result<Self> {
Ok(Self::new(
new_http(args),
CacheFile::from_args(args, new_share)?,
))
2025-06-04 21:02:35 +00:00
}
2025-06-24 01:35:21 +00:00
fn with_progressbar(&mut self, f: impl FnOnce(&ProgressBar), drop_bar: bool) {
let bar = &*self.progress.get_or_insert_with(new_progressbar);
2025-06-24 01:11:11 +00:00
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?
2025-06-26 00:08:18 +00:00
// `set_position` does not force an immediate redraw like e.g. `inc_length`
}
f(bar);
if drop_bar {
2025-06-24 01:35:21 +00:00
self.progress = None;
}
}
2025-06-24 01:35:21 +00:00
fn touch_progressbar(&mut self) {
self.with_progressbar(|_| (), false);
}
fn drop_progressbar(&mut self, f: impl FnOnce(&ProgressBar)) {
self.with_progressbar(f, true);
}
2025-06-27 01:55:43 +00:00
fn next_chunk<'t>(&mut self, buffer: &'t mut [u8]) -> crate::Result<Option<Chunk<'t>>> {
if self.inner.get_uploading(&self.http)?.is_none() {
return Ok(None);
}
self.touch_progressbar();
2025-06-05 17:37:35 +00:00
2025-06-24 02:05:27 +00:00
let uploading = self.inner.expect_uploading();
debug!("{uploading:?}");
let chunk = uploading.read(buffer)?;
debug!("{chunk:?}");
Ok(Some(chunk))
}
2025-06-27 01:55:43 +00:00
pub fn upload_chunk(&mut self, buffer: &mut [u8]) -> crate::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())
2025-06-05 01:13:54 +00:00
}
2025-06-25 16:34:32 +00:00
#[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();
2025-06-24 01:35:21 +00:00
self.drop_progressbar(ProgressBar::abandon);
2025-06-18 19:40:34 +00:00
}
2025-06-27 01:55:43 +00:00
pub fn rebuild_share(self, args: &Cli) -> crate::Result<Self> {
Ok(Self::new(self.http, CacheFile::from_args(args, new_share)?))
}
2025-06-04 21:02:35 +00:00
pub fn save(&self) -> io::Result<()> {
self.inner.save()
2025-06-02 23:57:17 +00:00
}
pub fn discard(self) -> io::Result<()> {
self.inner.discard()
2025-06-02 23:57:17 +00:00
}
pub fn clear_any(args: &Cli) {
CacheFile::clear_any(args);
2025-06-02 23:57:17 +00:00
}
}