2025-06-12 22:26:48 +00:00
|
|
|
use std::{
|
|
|
|
|
cell::{Ref, RefCell},
|
|
|
|
|
fmt, io,
|
|
|
|
|
time::Duration,
|
|
|
|
|
};
|
2025-06-02 23:57:17 +00:00
|
|
|
|
2025-06-05 17:37:35 +00:00
|
|
|
use console::style;
|
|
|
|
|
use indicatif::{ProgressBar, ProgressStyle};
|
2025-06-12 23:28:42 +00:00
|
|
|
use log::{debug, warn};
|
2025-06-02 23:57:17 +00:00
|
|
|
|
2025-06-18 13:04:04 +00:00
|
|
|
use crate::{
|
2025-06-12 16:07:54 +00:00
|
|
|
cachefile::CacheFile,
|
2025-06-02 23:57:17 +00:00
|
|
|
cli::Cli,
|
2025-06-13 23:00:36 +00:00
|
|
|
file::{self, Chunk, FileTrait},
|
2025-06-13 17:03:25 +00:00
|
|
|
sharry::{self, Client},
|
2025-06-02 23:57:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub struct AppState {
|
2025-06-12 23:43:03 +00:00
|
|
|
progress: RefCell<Option<ProgressBar>>,
|
2025-06-12 23:01:16 +00:00
|
|
|
http: ureq::Agent,
|
2025-06-12 16:07:54 +00:00
|
|
|
inner: CacheFile,
|
2025-06-04 13:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-11 00:28:02 +00:00
|
|
|
impl fmt::Debug for AppState {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.debug_struct("AppState")
|
2025-06-12 09:26:11 +00:00
|
|
|
.field("inner", &self.inner)
|
2025-06-12 16:25:15 +00:00
|
|
|
.finish_non_exhaustive()
|
2025-06-11 00:28:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 23:01:16 +00:00
|
|
|
fn new_http(timeout: Option<Duration>) -> ureq::Agent {
|
|
|
|
|
ureq::Agent::config_builder()
|
|
|
|
|
.timeout_global(timeout)
|
|
|
|
|
.build()
|
|
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-02 23:57:17 +00:00
|
|
|
impl AppState {
|
2025-06-13 22:27:15 +00:00
|
|
|
fn new(http: ureq::Agent, inner: CacheFile) -> Self {
|
2025-06-11 00:28:02 +00:00
|
|
|
Self {
|
2025-06-13 22:27:15 +00:00
|
|
|
progress: RefCell::new(None),
|
2025-06-12 23:01:16 +00:00
|
|
|
http,
|
2025-06-12 09:26:11 +00:00
|
|
|
inner,
|
2025-06-11 00:28:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-04 21:02:35 +00:00
|
|
|
pub fn try_resume(args: &Cli) -> Option<Self> {
|
2025-06-12 16:07:54 +00:00
|
|
|
let inner = CacheFile::try_resume(args)
|
|
|
|
|
.inspect_err(|e| debug!("could not resume from hash {:?}: {e}", args.get_hash()))
|
2025-06-12 09:26:11 +00:00
|
|
|
.ok()?;
|
|
|
|
|
|
2025-06-13 22:27:15 +00:00
|
|
|
Some(Self::new(new_http(args.get_timeout()), inner))
|
2025-06-04 21:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 23:01:16 +00:00
|
|
|
pub fn from_args(args: &Cli) -> sharry::Result<Self> {
|
|
|
|
|
let http = new_http(args.get_timeout());
|
|
|
|
|
|
2025-06-10 18:20:52 +00:00
|
|
|
let share_id = http.share_create(
|
2025-06-12 16:07:54 +00:00
|
|
|
&args.get_uri().endpoint("alias/upload/new"),
|
2025-06-12 09:26:11 +00:00
|
|
|
&args.alias,
|
2025-06-10 18:20:52 +00:00
|
|
|
args.get_share_request(),
|
|
|
|
|
)?;
|
2025-06-04 21:02:35 +00:00
|
|
|
|
2025-06-13 22:27:15 +00:00
|
|
|
Ok(Self::new(http, CacheFile::from_args(args, share_id)))
|
2025-06-04 21:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 22:26:48 +00:00
|
|
|
fn get_or_create_progressbar(&self, uploading: &file::Uploading) -> Ref<'_, ProgressBar> {
|
2025-06-12 23:43:03 +00:00
|
|
|
let mut slot = self.progress.borrow_mut();
|
2025-06-12 22:26:48 +00:00
|
|
|
if slot.is_none() {
|
2025-06-06 23:42:18 +00:00
|
|
|
let bar = ProgressBar::new(uploading.get_size())
|
2025-06-05 21:36:41 +00:00
|
|
|
.with_style(
|
|
|
|
|
ProgressStyle::with_template(&format!(
|
|
|
|
|
concat!(
|
2025-06-13 23:50:42 +00:00
|
|
|
"{{bar:50.cyan/blue}} {{msg:.magenta}}: ",
|
|
|
|
|
"{{binary_bytes:.yellow}}{}{{binary_total_bytes:.yellow}} ",
|
2025-06-05 21:36:41 +00:00
|
|
|
"({{eta}})",
|
|
|
|
|
),
|
|
|
|
|
style("/").magenta(),
|
|
|
|
|
))
|
2025-06-12 23:28:42 +00:00
|
|
|
.expect("style template is not valid"),
|
2025-06-05 21:36:41 +00:00
|
|
|
)
|
2025-06-12 22:26:48 +00:00
|
|
|
.with_position(uploading.get_offset())
|
|
|
|
|
.with_message(uploading.get_name().to_owned());
|
2025-06-05 21:36:41 +00:00
|
|
|
|
|
|
|
|
bar.enable_steady_tick(Duration::from_millis(100));
|
2025-06-12 22:26:48 +00:00
|
|
|
*slot = Some(bar);
|
|
|
|
|
}
|
|
|
|
|
drop(slot);
|
|
|
|
|
|
2025-06-12 23:43:03 +00:00
|
|
|
Ref::map(self.progress.borrow(), |opt| {
|
2025-06-12 23:28:42 +00:00
|
|
|
opt.as_ref().expect("somehow the slot holds None")
|
|
|
|
|
})
|
2025-06-12 22:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-13 16:02:37 +00:00
|
|
|
fn finish_progressbar(&self) {
|
2025-06-12 23:43:03 +00:00
|
|
|
let mut slot = self.progress.borrow_mut();
|
|
|
|
|
if let Some(bar) = slot.as_ref() {
|
2025-06-12 22:26:48 +00:00
|
|
|
bar.finish();
|
|
|
|
|
*slot = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 22:27:15 +00:00
|
|
|
fn next_chunk<'t>(&mut self, buffer: &'t mut [u8]) -> io::Result<Option<Chunk<'t>>> {
|
2025-06-12 23:01:16 +00:00
|
|
|
let Some(mut uploading) = self.inner.pop_file(&self.http) else {
|
2025-06-12 23:28:42 +00:00
|
|
|
self.inner
|
|
|
|
|
.share_notify(&self.http)
|
|
|
|
|
.unwrap_or_else(|e| warn!("Failed to notify the share: {e}"));
|
2025-06-12 22:26:48 +00:00
|
|
|
|
2025-06-13 17:03:25 +00:00
|
|
|
return Ok(None);
|
2025-06-12 22:26:48 +00:00
|
|
|
};
|
2025-06-13 16:02:37 +00:00
|
|
|
debug!("{uploading:?}");
|
2025-06-12 22:26:48 +00:00
|
|
|
|
2025-06-13 16:02:37 +00:00
|
|
|
self.get_or_create_progressbar(&uploading);
|
2025-06-05 17:37:35 +00:00
|
|
|
|
2025-06-13 22:27:15 +00:00
|
|
|
let chunk_res = uploading.read(buffer);
|
2025-06-13 17:03:25 +00:00
|
|
|
self.inner.push_file(uploading);
|
2025-06-10 23:39:08 +00:00
|
|
|
|
2025-06-13 22:27:15 +00:00
|
|
|
let chunk = chunk_res?;
|
2025-06-13 16:02:37 +00:00
|
|
|
debug!("{chunk:?}");
|
|
|
|
|
|
2025-06-13 17:03:25 +00:00
|
|
|
Ok(Some(chunk))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_done(&mut self) -> bool {
|
|
|
|
|
let Some(uploading) = self.inner.pop_file(&self.http) else {
|
|
|
|
|
return true;
|
|
|
|
|
};
|
2025-06-10 23:39:08 +00:00
|
|
|
|
|
|
|
|
match uploading.check_eof() {
|
|
|
|
|
Ok(uploading) => {
|
2025-06-12 22:26:48 +00:00
|
|
|
let bar = self.get_or_create_progressbar(&uploading);
|
2025-06-10 23:39:08 +00:00
|
|
|
bar.set_position(uploading.get_offset());
|
2025-06-12 22:26:48 +00:00
|
|
|
// BUG in `indicatif` crate?
|
2025-06-13 22:27:15 +00:00
|
|
|
// `set_position` does not force an immediate redraw, so we also call `inc_length` here
|
2025-06-12 22:26:48 +00:00
|
|
|
bar.inc_length(0);
|
|
|
|
|
drop(bar);
|
|
|
|
|
|
2025-06-12 09:26:11 +00:00
|
|
|
self.inner.push_file(uploading);
|
2025-06-05 01:13:54 +00:00
|
|
|
}
|
2025-06-10 23:39:08 +00:00
|
|
|
Err(path) => {
|
2025-06-05 01:13:54 +00:00
|
|
|
debug!("Finished {:?}!", path.display());
|
2025-06-13 16:02:37 +00:00
|
|
|
self.finish_progressbar();
|
2025-06-05 01:13:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-06-13 17:03:25 +00:00
|
|
|
|
|
|
|
|
self.inner.is_empty()
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 22:27:15 +00:00
|
|
|
pub fn upload_chunk(&mut self, buffer: &mut [u8]) -> sharry::Result<bool> {
|
|
|
|
|
let Some(chunk) = self.next_chunk(buffer)? else {
|
2025-06-13 17:03:25 +00:00
|
|
|
return Ok(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.http.file_patch(
|
|
|
|
|
chunk.get_patch_uri(),
|
|
|
|
|
self.inner.alias_id(),
|
|
|
|
|
chunk.get_offset(),
|
2025-06-13 22:27:15 +00:00
|
|
|
chunk.get_data(),
|
2025-06-13 17:03:25 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
Ok(self.is_done())
|
2025-06-05 01:13:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-13 23:00:36 +00:00
|
|
|
pub fn rewind(mut self) -> Option<Self> {
|
|
|
|
|
let Some(uploading) = self.inner.pop_file(&self.http) else {
|
|
|
|
|
warn!("rewind called on empty queue");
|
|
|
|
|
return None;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let uploading = uploading.rewind()?;
|
|
|
|
|
self.inner.push_file(uploading);
|
|
|
|
|
|
|
|
|
|
Some(self)
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 16:07:54 +00:00
|
|
|
pub fn file_names(&self) -> Vec<&str> {
|
|
|
|
|
self.inner.file_names()
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-04 21:02:35 +00:00
|
|
|
pub fn save(&self) -> io::Result<()> {
|
2025-06-12 09:26:11 +00:00
|
|
|
self.inner.save()
|
2025-06-02 23:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-04 21:23:21 +00:00
|
|
|
pub fn clear(self) -> io::Result<()> {
|
2025-06-12 09:26:11 +00:00
|
|
|
self.inner.clear()
|
2025-06-02 23:57:17 +00:00
|
|
|
}
|
|
|
|
|
}
|