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-24 00:18:26 +00:00
|
|
|
file::{self, Chunk},
|
2025-06-12 09:26:11 +00:00
|
|
|
sharry::{self, Client, Uri},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
|
enum FileState {
|
|
|
|
|
C(file::Checked),
|
|
|
|
|
U(file::Uploading),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FileState {
|
|
|
|
|
fn start_upload(
|
|
|
|
|
self,
|
2025-06-18 13:09:34 +00:00
|
|
|
client: &impl sharry::Client,
|
|
|
|
|
uri: &sharry::Uri,
|
2025-06-12 09:26:11 +00:00
|
|
|
alias_id: &str,
|
2025-06-18 13:09:34 +00:00
|
|
|
share_id: &str,
|
2025-06-12 09:26:11 +00:00
|
|
|
) -> sharry::Result<file::Uploading> {
|
|
|
|
|
match self {
|
2025-06-18 13:09:34 +00:00
|
|
|
FileState::C(checked) => checked.start_upload(client, uri, alias_id, share_id),
|
2025-06-12 09:26:11 +00:00
|
|
|
FileState::U(uploading) => Ok(uploading),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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,
|
|
|
|
|
files: VecDeque<FileState>,
|
|
|
|
|
}
|
|
|
|
|
|
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-12 16:07:54 +00:00
|
|
|
files: args.files.clone().into_iter().map(FileState::C).collect(),
|
2025-06-12 09:26:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 16:02:37 +00:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
self.files.is_empty()
|
2025-06-12 09:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-18 13:09:34 +00:00
|
|
|
pub fn pop_file(&mut self, client: &impl Client) -> Option<file::Uploading> {
|
2025-06-12 09:26:11 +00:00
|
|
|
if let Some(state) = self.files.pop_front() {
|
2025-06-18 13:09:34 +00:00
|
|
|
// HACK unwrap
|
2025-06-13 23:50:42 +00:00
|
|
|
// TODO somehow retry
|
2025-06-18 13:09:34 +00:00
|
|
|
Some(
|
|
|
|
|
state
|
|
|
|
|
.start_upload(client, &self.uri, &self.alias_id, &self.share_id)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
)
|
2025-06-12 09:26:11 +00:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn push_file(&mut self, file: file::Uploading) {
|
|
|
|
|
self.files.push_front(FileState::U(file));
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 19:40:34 +00:00
|
|
|
pub fn requeue_file(&mut self, file: file::Checked) {
|
|
|
|
|
self.files.push_back(FileState::C(file));
|
|
|
|
|
}
|
|
|
|
|
|
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(())
|
|
|
|
|
}
|
|
|
|
|
}
|