wip: main business logic
This commit is contained in:
parent
cf84eed717
commit
c246de0c99
2 changed files with 90 additions and 25 deletions
|
|
@ -4,7 +4,7 @@ use std::{
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use log::{debug, trace};
|
use log::{debug, error, trace};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
|
|
@ -14,46 +14,98 @@ use super::{
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
|
#[serde(skip)]
|
||||||
|
file_name: PathBuf,
|
||||||
|
|
||||||
alias: Alias,
|
alias: Alias,
|
||||||
share: Share,
|
share: Share,
|
||||||
checked: Vec<FileChecked>,
|
files: Vec<FileState>,
|
||||||
uploading: Vec<FileUploading>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_cachefile(args: &Cli) -> Option<PathBuf> {
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
let file_name: PathBuf = dirs_next::cache_dir()?
|
enum FileState {
|
||||||
.join("shrupl")
|
C(FileChecked),
|
||||||
.join(format!("{}.json", args.get_hash()));
|
U(FileUploading),
|
||||||
|
|
||||||
trace!("cachefile: {}", file_name.display());
|
|
||||||
|
|
||||||
Some(file_name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
|
fn cache_dir() -> Option<PathBuf> {
|
||||||
|
let dir_name = dirs_next::cache_dir()?.join("shrupl");
|
||||||
|
|
||||||
|
trace!("cachedir: {}", dir_name.display());
|
||||||
|
|
||||||
|
Some(dir_name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cache_file(args: &Cli) -> Option<PathBuf> {
|
||||||
|
let file_name = Self::cache_dir()?.join(format!("{}.json", args.get_hash()));
|
||||||
|
|
||||||
|
trace!("cachefile: {}", file_name.display());
|
||||||
|
|
||||||
|
Some(file_name)
|
||||||
|
}
|
||||||
|
|
||||||
fn load(file_name: impl AsRef<Path>) -> io::Result<Self> {
|
fn load(file_name: impl AsRef<Path>) -> io::Result<Self> {
|
||||||
let content = fs::read_to_string(file_name)?;
|
let content = fs::read_to_string(file_name)?;
|
||||||
serde_json::from_str(&content).map_err(io::Error::other)
|
serde_json::from_str(&content).map_err(io::Error::other)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save(&self, file_name: impl AsRef<Path>) -> io::Result<()> {
|
pub fn try_resume(args: &Cli) -> Option<Self> {
|
||||||
|
let file_name = Self::cache_file(args)?;
|
||||||
|
|
||||||
|
Self::load(&file_name)
|
||||||
|
.inspect_err(|e| debug!("could not resume from {}: {e}", file_name.display()))
|
||||||
|
.map(|state| {
|
||||||
|
debug!("successfully loaded AppState");
|
||||||
|
|
||||||
|
Self {
|
||||||
|
file_name,
|
||||||
|
alias: state.alias,
|
||||||
|
share: state.share,
|
||||||
|
files: state.files,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_args(args: Cli, agent: &ureq::Agent) -> Option<Self> {
|
||||||
|
let file_name = Self::cache_file(&args)?;
|
||||||
|
|
||||||
|
let alias = args.get_alias();
|
||||||
|
|
||||||
|
let share = Share::create(&agent, &alias, args.get_share_request())
|
||||||
|
.inspect_err(|e| error!("could not create Share: {e}"))
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
let files: Vec<_> = args.files.into_iter().map(|f| FileState::C(f)).collect();
|
||||||
|
|
||||||
|
Some(Self {
|
||||||
|
file_name,
|
||||||
|
alias,
|
||||||
|
share,
|
||||||
|
files,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save(&self) -> io::Result<()> {
|
||||||
|
let cache_dir = Self::cache_dir()
|
||||||
|
.ok_or_else(|| io::Error::other("could not determine cache directory"))?;
|
||||||
|
fs::create_dir_all(cache_dir)?;
|
||||||
|
|
||||||
let json = serde_json::to_string_pretty(self).map_err(io::Error::other)?;
|
let json = serde_json::to_string_pretty(self).map_err(io::Error::other)?;
|
||||||
let mut file = fs::File::create(file_name)?;
|
let mut file = fs::File::create(&self.file_name)?;
|
||||||
file.write_all(json.as_bytes())?;
|
file.write_all(json.as_bytes())?;
|
||||||
|
|
||||||
|
trace!("successfully saved AppState");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_resume(args: &Cli) -> Option<Self> {
|
pub fn delete(self) -> io::Result<()> {
|
||||||
let file_name = get_cachefile(args)?;
|
fs::remove_file(self.file_name)?;
|
||||||
|
|
||||||
// let content = fs::read_to_string(&file_name).ok()?;
|
trace!("successfully cleared AppState");
|
||||||
// serde_json::from_str(&content)
|
|
||||||
// .inspect_err(|e| debug!("could not resume from {}: {e}", &file_name.display()))
|
|
||||||
// .ok()
|
|
||||||
|
|
||||||
Self::load(&file_name)
|
Ok(())
|
||||||
.inspect_err(|e| debug!("could not resume from {}: {e}", file_name.display()))
|
|
||||||
.ok()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
src/main.rs
19
src/main.rs
|
|
@ -22,9 +22,22 @@ fn main() {
|
||||||
.build()
|
.build()
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
if let Some(state) = AppState::try_resume(&args) {
|
let state = AppState::try_resume(&args)
|
||||||
info!("state: {state:?}");
|
.and_then(|state| {
|
||||||
}
|
info!("loaded state: {state:?}");
|
||||||
|
|
||||||
|
Some(state)
|
||||||
|
})
|
||||||
|
.or_else(|| AppState::from_args(args, &agent))
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
std::process::exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
info!("continuing with state: {state:?}");
|
||||||
|
state.save().unwrap();
|
||||||
|
// state.delete().unwrap();
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
let alias = args.get_alias();
|
let alias = args.get_alias();
|
||||||
let share = Share::create(&agent, &alias, args.get_share_request()).unwrap();
|
let share = Share::create(&agent, &alias, args.get_share_request()).unwrap();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue