Compare commits
2 commits
cf84eed717
...
fd79455de4
| Author | SHA1 | Date | |
|---|---|---|---|
| fd79455de4 | |||
| c246de0c99 |
2 changed files with 90 additions and 28 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,93 @@ 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() -> 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 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<()> {
|
|
||||||
let json = serde_json::to_string_pretty(self).map_err(io::Error::other)?;
|
|
||||||
let mut file = fs::File::create(file_name)?;
|
|
||||||
file.write_all(json.as_bytes())?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn try_resume(args: &Cli) -> Option<Self> {
|
pub fn try_resume(args: &Cli) -> Option<Self> {
|
||||||
let file_name = get_cachefile(args)?;
|
let file_name = Self::cache_file(args);
|
||||||
|
|
||||||
// let content = fs::read_to_string(&file_name).ok()?;
|
|
||||||
// serde_json::from_str(&content)
|
|
||||||
// .inspect_err(|e| debug!("could not resume from {}: {e}", &file_name.display()))
|
|
||||||
// .ok()
|
|
||||||
|
|
||||||
Self::load(&file_name)
|
Self::load(&file_name)
|
||||||
.inspect_err(|e| debug!("could not resume from {}: {e}", file_name.display()))
|
.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()
|
.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.clone().into_iter().map(FileState::C).collect();
|
||||||
|
|
||||||
|
Some(Self {
|
||||||
|
file_name,
|
||||||
|
alias,
|
||||||
|
share,
|
||||||
|
files,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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!("successfully saved AppState");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(self) -> io::Result<()> {
|
||||||
|
fs::remove_file(self.file_name)?;
|
||||||
|
|
||||||
|
trace!("successfully cleared AppState");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
21
src/main.rs
21
src/main.rs
|
|
@ -22,9 +22,24 @@ fn main() {
|
||||||
.build()
|
.build()
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
if let Some(state) = AppState::try_resume(&args) {
|
let state = AppState::try_resume(&args)
|
||||||
info!("state: {state:?}");
|
.map(|state| {
|
||||||
}
|
info!("loaded state: {state:?}");
|
||||||
|
|
||||||
|
state
|
||||||
|
})
|
||||||
|
.or_else(|| AppState::from_args(&args, &agent))
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
error!("could not create new state from cli arguments: {args:?}");
|
||||||
|
|
||||||
|
std::process::exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
info!("continuing with state: {state:?}");
|
||||||
|
state.save().unwrap();
|
||||||
|
// state.clear().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