Compare commits

..

No commits in common. "2edc690331632d52792ec3a9493f3f11196c371f" and "fb06725f0575e3a97809ea1116555819ef78895a" have entirely different histories.

3 changed files with 85 additions and 70 deletions

View file

@ -1,33 +1,53 @@
use std::{fmt, io, time::Duration};
use std::{fmt, io, path::PathBuf, time::Duration};
use console::style;
use indicatif::{ProgressBar, ProgressStyle};
use log::debug;
use log::{debug, trace};
use serde::{Deserialize, Serialize};
use super::{
cachefile::CacheFile,
cli::Cli,
file::FileTrait,
savedstate::SavedState,
sharry::{self, Client, ClientError},
};
#[derive(Serialize, Deserialize)]
pub struct AppState {
#[serde(skip)]
progress: Option<ProgressBar>,
#[serde(skip)]
buffer: Vec<u8>,
inner: CacheFile,
inner: SavedState,
}
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()
.finish()
}
}
impl AppState {
fn new(chunk_size: usize, inner: CacheFile) -> Self {
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(chunk_size: usize, inner: SavedState) -> Self {
Self {
progress: None,
buffer: vec![0; chunk_size * 1024 * 1024],
@ -36,26 +56,38 @@ impl AppState {
}
pub fn try_resume(args: &Cli) -> Option<Self> {
let inner = CacheFile::try_resume(args)
.inspect_err(|e| debug!("could not resume from hash {:?}: {e}", args.get_hash()))
let file_name = Self::cache_file(args);
let inner = SavedState::load(&file_name)
.inspect_err(|e| debug!("could not resume from {:?}: {e}", file_name.display()))
.ok()?;
Some(Self::new(args.chunk_size, inner))
}
pub fn from_args(args: &Cli, http: &impl Client) -> sharry::Result<Self> {
let uri = args.get_uri();
let share_id = http.share_create(
&args.get_uri().endpoint("alias/upload/new"),
&uri.endpoint("alias/upload/new"),
&args.alias,
args.get_share_request(),
)?;
Ok(Self::new(
args.chunk_size,
CacheFile::from_args(args, share_id),
SavedState::new(
Self::cache_file(&args),
uri,
args.alias.clone(),
share_id,
&args.files,
),
))
}
pub fn file_names(&self) -> Vec<&str> {
self.inner.file_names()
}
pub fn upload_chunk(&mut self, http: &impl Client) -> sharry::Result<Option<()>> {
let Some(mut uploading) = self.inner.pop_file(http) else {
return Ok(None);
@ -94,7 +126,7 @@ impl AppState {
http.file_patch(
chunk.get_patch_uri(),
self.inner.alias_id(),
&self.alias_id,
chunk.get_offset(),
chunk.get_data(),
)?;
@ -110,17 +142,16 @@ impl AppState {
bar.finish();
self.progress = None;
self.inner.share_notify(http).unwrap(); // HACK unwrap
let endpoint = self
.uri
.endpoint(format!("alias/mail/notify/{}", self.share_id));
http.share_notify(&endpoint, &self.alias_id).unwrap(); // HACK unwrap
Ok(self.inner.has_file().then_some(()))
}
}
}
pub fn file_names(&self) -> Vec<&str> {
self.inner.file_names()
}
pub fn save(&self) -> io::Result<()> {
self.inner.save()
}

View file

@ -1,7 +1,7 @@
mod appstate;
mod cachefile;
mod cli;
mod file;
mod savedstate;
mod sharry;
use std::{

View file

@ -2,14 +2,13 @@ use std::{
collections::VecDeque,
fs,
io::{self, Write},
path::PathBuf,
path::{Path, PathBuf},
};
use log::trace;
use serde::{Deserialize, Serialize};
use super::{
cli::Cli,
file::{self, FileTrait},
sharry::{self, Client, Uri},
};
@ -31,18 +30,22 @@ impl FileState {
fn start_upload(
self,
http: &impl Client,
endpoint: &str,
uri: &Uri,
alias_id: &str,
share_id: &str,
) -> sharry::Result<file::Uploading> {
match self {
FileState::C(checked) => checked.start_upload(http, endpoint, alias_id),
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),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CacheFile {
pub struct SavedState {
#[serde(skip)]
file_name: PathBuf,
@ -52,47 +55,35 @@ pub struct CacheFile {
files: VecDeque<FileState>,
}
impl CacheFile {
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
}
pub fn try_resume(args: &Cli) -> io::Result<Self> {
let file_name = Self::cache_file(args);
let state: Self = {
let file = fs::File::open(&file_name)?;
let reader = io::BufReader::new(file);
serde_json::from_reader(reader).map_err(io::Error::other)?
};
Ok(Self { file_name, ..state })
}
pub fn from_args(args: &Cli, share_id: String) -> Self {
impl SavedState {
pub fn new(
file_name: PathBuf,
uri: Uri,
alias_id: String,
share_id: String,
files: &Vec<file::Checked>,
) -> Self {
Self {
file_name: Self::cache_file(args),
uri: args.get_uri(),
alias_id: args.alias.clone(),
file_name,
uri,
alias_id,
share_id,
files: args.files.clone().into_iter().map(FileState::C).collect(),
files: files.clone().into_iter().map(FileState::C).collect(),
}
}
pub fn alias_id(&self) -> &str {
&self.alias_id
pub fn load(file_name: &Path) -> io::Result<Self> {
let file = fs::File::open(file_name)?;
let state: Self =
serde_json::from_reader(io::BufReader::new(file)).map_err(io::Error::other)?;
Ok(Self {
file_name: file_name.to_owned(),
uri: state.uri,
alias_id: state.alias_id,
share_id: state.share_id,
files: state.files,
})
}
pub fn file_names(&self) -> Vec<&str> {
@ -105,10 +96,11 @@ impl CacheFile {
pub fn pop_file(&mut self, http: &impl Client) -> Option<file::Uploading> {
if let Some(state) = self.files.pop_front() {
let endpoint = self
.uri
.endpoint(format!("alias/upload/{}/files/tus", self.share_id));
Some(state.start_upload(http, &endpoint, &self.alias_id).unwrap()) // HACK unwrap
Some(
state
.start_upload(http, &self.uri, &self.alias_id, &self.share_id)
.unwrap(),
) // HACK unwrap
} else {
None
}
@ -118,14 +110,6 @@ impl CacheFile {
self.files.push_front(FileState::U(file));
}
pub fn share_notify(&self, http: &impl Client) -> sharry::Result<()> {
let endpoint = self
.uri
.endpoint(format!("alias/mail/notify/{}", self.share_id));
http.share_notify(&endpoint, &self.alias_id)
}
pub fn save(&self) -> io::Result<()> {
let cache_dir = self.file_name.parent().ok_or_else(|| {
io::Error::other(format!("orphan file {:?}", self.file_name.display()))