2025-06-12 09:26:11 +00:00
|
|
|
use std::{
|
|
|
|
|
collections::VecDeque,
|
|
|
|
|
fs,
|
|
|
|
|
io::{self, Write},
|
2025-06-12 16:07:54 +00:00
|
|
|
path::PathBuf,
|
2025-06-12 09:26:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use log::trace;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2025-06-18 13:04:04 +00:00
|
|
|
use crate::{
|
2025-06-12 16:07:54 +00:00
|
|
|
cli::Cli,
|
2025-06-18 13:09:34 +00:00
|
|
|
file::{self, Chunk, FileTrait},
|
2025-06-12 09:26:11 +00:00
|
|
|
sharry::{self, Client, Uri},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
2025-06-12 16:07:54 +00:00
|
|
|
pub struct CacheFile {
|
2025-06-12 09:26:11 +00:00
|
|
|
#[serde(skip)]
|
|
|
|
|
file_name: PathBuf,
|
|
|
|
|
|
|
|
|
|
uri: Uri,
|
|
|
|
|
alias_id: String,
|
|
|
|
|
share_id: String,
|
2025-06-18 23:40:27 +00:00
|
|
|
|
|
|
|
|
uploading: Option<file::Uploading>,
|
|
|
|
|
files: VecDeque<file::Checked>,
|
2025-06-12 09:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 16:07:54 +00:00
|
|
|
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 {
|
2025-06-12 09:26:11 +00:00
|
|
|
Self {
|
2025-06-12 16:25:15 +00:00
|
|
|
file_name: Self::cache_file(args),
|
2025-06-12 16:07:54 +00:00
|
|
|
uri: args.get_uri(),
|
|
|
|
|
alias_id: args.alias.clone(),
|
2025-06-12 09:26:11 +00:00
|
|
|
share_id,
|
2025-06-18 23:40:27 +00:00
|
|
|
uploading: None,
|
|
|
|
|
files: args.files.clone().into(),
|
2025-06-12 09:26:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn file_names(&self) -> Vec<&str> {
|
2025-06-18 23:40:27 +00:00
|
|
|
let mut result = vec![];
|
2025-06-12 09:26:11 +00:00
|
|
|
|
2025-06-18 23:40:27 +00:00
|
|
|
if let Some(upl) = self.uploading.as_ref() {
|
|
|
|
|
result.push(upl.get_name());
|
|
|
|
|
}
|
|
|
|
|
self.files.iter().map(|chk| result.push(chk.get_name()));
|
|
|
|
|
|
|
|
|
|
result
|
2025-06-12 09:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-18 23:40:27 +00:00
|
|
|
pub fn start_upload(&mut self, client: &impl Client) -> sharry::Result<bool> {
|
|
|
|
|
if self.uploading.is_some() {
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(chk) = self.files.pop_front() {
|
|
|
|
|
self.uploading =
|
|
|
|
|
Some(chk.start_upload(client, &self.uri, &self.alias_id, &self.share_id)?);
|
|
|
|
|
|
|
|
|
|
Ok(true)
|
2025-06-12 09:26:11 +00:00
|
|
|
} else {
|
2025-06-18 23:40:27 +00:00
|
|
|
Ok(false)
|
2025-06-12 09:26:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 23:40:27 +00:00
|
|
|
pub fn uploading(&mut self) -> &mut Option<file::Uploading> {
|
|
|
|
|
&mut self.uploading
|
2025-06-12 09:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-18 23:40:27 +00:00
|
|
|
pub fn abort_upload(&mut self) {
|
|
|
|
|
let Some(upl) = self.uploading.take() else {
|
|
|
|
|
panic!("abort called while not uploading");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.files.push_front(upl.abort());
|
2025-06-18 19:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-18 13:09:34 +00:00
|
|
|
pub fn share_notify(&self, client: &impl Client) -> sharry::Result<()> {
|
|
|
|
|
client.share_notify(&self.uri, &self.alias_id, &self.share_id)
|
|
|
|
|
}
|
2025-06-12 16:07:54 +00:00
|
|
|
|
2025-06-18 13:09:34 +00:00
|
|
|
pub fn file_patch(&self, client: &impl Client, chunk: &Chunk) -> sharry::Result<()> {
|
|
|
|
|
client.file_patch(&self.uri, &self.alias_id, &self.share_id, chunk)
|
2025-06-12 16:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 09:26:11 +00:00
|
|
|
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()))
|
|
|
|
|
})?;
|
|
|
|
|
fs::create_dir_all(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(())
|
|
|
|
|
}
|
|
|
|
|
}
|