shrupl/src/appstate.rs
2025-06-11 00:28:23 +00:00

252 lines
7 KiB
Rust

use std::{
collections::VecDeque,
fmt, fs,
io::{self, Write},
path::{Path, PathBuf},
time::Duration,
};
use console::style;
use indicatif::{ProgressBar, ProgressStyle};
use log::{debug, trace};
use serde::{Deserialize, Serialize};
use super::{
cli::Cli,
file::{self, FileTrait},
sharry::{self, Client, ClientError, Uri},
};
#[derive(Serialize, Deserialize)]
pub struct AppState {
#[serde(skip)]
file_name: PathBuf,
#[serde(skip)]
progress: Option<ProgressBar>,
#[serde(skip)]
buffer: Vec<u8>,
uri: Uri,
alias_id: String,
share_id: String,
files: VecDeque<FileState>,
}
impl fmt::Debug for AppState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AppState")
.field("file_name", &self.file_name)
.field("uri", &self.uri)
.field("alias_id", &self.alias_id)
.field("share_id", &self.share_id)
.field("files", &self.files)
.finish()
}
}
#[derive(Serialize, Deserialize, Debug)]
enum FileState {
C(file::Checked),
U(file::Uploading),
}
impl FileState {
fn file_name(&self) -> &str {
match self {
FileState::C(checked) => checked.get_name(),
FileState::U(uploading) => uploading.get_name(),
}
}
fn start_upload(
self,
http: &impl Client,
uri: &Uri,
alias_id: &str,
share_id: &str,
) -> sharry::Result<file::Uploading> {
match self {
FileState::C(checked) => {
let endpoint = &uri.endpoint(format!("alias/upload/{share_id}/files/tus"));
checked.start_upload(http, endpoint, alias_id)
}
FileState::U(uploading) => Ok(uploading),
}
}
}
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(
file_name: PathBuf,
chunk_size: usize,
uri: Uri,
alias_id: String,
share_id: String,
files: VecDeque<FileState>,
) -> Self {
Self {
file_name,
progress: None,
buffer: vec![0; chunk_size * 1024 * 1024],
uri,
alias_id,
share_id,
files,
}
}
fn load(file_name: &Path, chunk_size: usize) -> io::Result<Self> {
let file = fs::File::open(file_name)?;
serde_json::from_reader(io::BufReader::new(file))
.map_err(io::Error::other)
.map(|state: Self| {
debug!("successfully loaded AppState");
Self::new(
file_name.to_owned(),
chunk_size,
state.uri,
state.alias_id,
state.share_id,
state.files,
)
})
}
pub fn try_resume(args: &Cli) -> Option<Self> {
let file_name = Self::cache_file(args);
Self::load(&file_name, args.chunk_size)
.inspect_err(|e| debug!("could not resume from {:?}: {e}", file_name.display()))
.ok()
}
pub fn from_args(args: &Cli, http: &impl Client) -> sharry::Result<Self> {
let file_name = Self::cache_file(args);
let uri = args.get_uri();
let alias_id = args.alias.clone();
let share_id = http.share_create(
&uri.endpoint("alias/upload/new"),
&alias_id,
args.get_share_request(),
)?;
let files: VecDeque<_> = args.files.clone().into_iter().map(FileState::C).collect();
Ok(Self::new(
file_name,
args.chunk_size,
uri,
alias_id,
share_id,
files,
))
}
pub fn file_names(&self) -> Vec<&str> {
self.files.iter().map(FileState::file_name).collect()
}
pub fn upload_chunk(&mut self, http: &impl Client) -> sharry::Result<Option<()>> {
let mut uploading = if let Some(state) = self.files.pop_front() {
state
.start_upload(http, &self.uri, &self.alias_id, &self.share_id)
.unwrap() // HACK unwrap
} else {
return Ok(None);
};
debug!("{uploading} chunk {}", self.buffer.len());
// Initialize or fetch the existing ProgressBar in one call:
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::req_err)?;
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.files.push_front(FileState::U(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.files.front().map(drop))
}
}
}
pub fn save(&self) -> io::Result<()> {
fs::create_dir_all(Self::cache_dir())?;
let json = serde_json::to_string_pretty(self).map_err(io::Error::other)?;
let mut file = fs::File::create(&self.file_name)?;
file.write_all(json.as_bytes())?;
trace!("updated {:?}", self.file_name.display());
Ok(())
}
pub fn clear(self) -> io::Result<()> {
fs::remove_file(&self.file_name)?;
trace!("removed {:?}", self.file_name.display());
Ok(())
}
}