Compare commits
No commits in common. "9208a7dfcf0e04336325413974d757031ad0c2f3" and "28bb44aaf0dffc8929c1b4a8d383d0a61973b56c" have entirely different histories.
9208a7dfcf
...
28bb44aaf0
5 changed files with 36 additions and 65 deletions
|
|
@ -1,5 +1,4 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::VecDeque,
|
|
||||||
fs,
|
fs,
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
|
|
@ -10,7 +9,7 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
cli::Cli,
|
cli::Cli,
|
||||||
sharry::{Alias, ChunkState, FileChecked, FileUploading, Share, UploadError},
|
sharry::{Alias, FileChecked, FileUploading, Share},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
|
@ -20,7 +19,7 @@ pub struct AppState {
|
||||||
|
|
||||||
alias: Alias,
|
alias: Alias,
|
||||||
share: Share,
|
share: Share,
|
||||||
files: VecDeque<FileState>,
|
files: Vec<FileState>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
|
@ -69,15 +68,15 @@ impl AppState {
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_args(args: &Cli, http: &ureq::Agent) -> Option<Self> {
|
pub fn from_args(args: &Cli, agent: &ureq::Agent) -> Option<Self> {
|
||||||
let file_name = Self::cache_file(args);
|
let file_name = Self::cache_file(args);
|
||||||
let alias = args.get_alias();
|
let alias = args.get_alias();
|
||||||
|
|
||||||
let share = Share::create(http, &alias, args.get_share_request())
|
let share = Share::create(agent, &alias, args.get_share_request())
|
||||||
.inspect_err(|e| error!("could not create Share: {e}"))
|
.inspect_err(|e| error!("could not create Share: {e}"))
|
||||||
.ok()?;
|
.ok()?;
|
||||||
|
|
||||||
let files: VecDeque<_> = args.files.clone().into_iter().map(FileState::C).collect();
|
let files: Vec<_> = args.files.clone().into_iter().map(FileState::C).collect();
|
||||||
|
|
||||||
Some(Self {
|
Some(Self {
|
||||||
file_name,
|
file_name,
|
||||||
|
|
@ -87,41 +86,6 @@ impl AppState {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn upload_chunk(
|
|
||||||
&mut self,
|
|
||||||
http: &ureq::Agent,
|
|
||||||
chunk_size: usize,
|
|
||||||
) -> Result<Option<()>, UploadError> {
|
|
||||||
let uploading = match self.files.pop_front() {
|
|
||||||
Some(FileState::C(checked)) => checked
|
|
||||||
.start_upload(http, &self.alias, &self.share)
|
|
||||||
.unwrap(),
|
|
||||||
Some(FileState::U(uploading)) => uploading,
|
|
||||||
None => {
|
|
||||||
self.share.notify(http, &self.alias).unwrap();
|
|
||||||
|
|
||||||
return Ok(None);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
debug!("{uploading} chunk {chunk_size}");
|
|
||||||
|
|
||||||
match uploading.upload_chunk(http, &self.alias, chunk_size) {
|
|
||||||
ChunkState::Ok(upl) => {
|
|
||||||
self.files.push_front(FileState::U(upl));
|
|
||||||
Ok(Some(()))
|
|
||||||
}
|
|
||||||
ChunkState::Err(upl, e) => {
|
|
||||||
self.files.push_front(FileState::U(upl));
|
|
||||||
Err(e)
|
|
||||||
}
|
|
||||||
ChunkState::Finished(path) => {
|
|
||||||
debug!("Finished {:?}!", path.display());
|
|
||||||
Ok(Some(()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn save(&self) -> io::Result<()> {
|
pub fn save(&self) -> io::Result<()> {
|
||||||
fs::create_dir_all(Self::cache_dir())?;
|
fs::create_dir_all(Self::cache_dir())?;
|
||||||
|
|
||||||
|
|
|
||||||
36
src/main.rs
36
src/main.rs
|
|
@ -3,11 +3,12 @@ mod cli;
|
||||||
mod sharry;
|
mod sharry;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use log::{error, info};
|
use log::{debug, error, info};
|
||||||
use ureq::Agent;
|
use ureq::Agent;
|
||||||
|
|
||||||
use appstate::AppState;
|
use appstate::AppState;
|
||||||
use cli::Cli;
|
use cli::Cli;
|
||||||
|
use sharry::{ChunkState, Share};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
@ -21,7 +22,7 @@ fn main() {
|
||||||
.build()
|
.build()
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
let mut state = AppState::try_resume(&args)
|
let state = AppState::try_resume(&args)
|
||||||
.map(|state| {
|
.map(|state| {
|
||||||
info!("loaded state: {state:?}");
|
info!("loaded state: {state:?}");
|
||||||
|
|
||||||
|
|
@ -36,17 +37,34 @@ fn main() {
|
||||||
|
|
||||||
info!("continuing with state: {state:?}");
|
info!("continuing with state: {state:?}");
|
||||||
state.save().unwrap();
|
state.save().unwrap();
|
||||||
|
// state.clear().unwrap();
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
|
let alias = args.get_alias();
|
||||||
|
let share = Share::create(&agent, &alias, args.get_share_request()).unwrap();
|
||||||
|
info!("share: {share:?}");
|
||||||
|
|
||||||
|
for file in args.files {
|
||||||
|
let mut file = file.start_upload(&agent, &alias, &share).unwrap();
|
||||||
|
info!("file: {file:?}");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match state.upload_chunk(&agent, args.chunk_size * 1024 * 1024) {
|
match file.upload_chunk(&agent, &alias, args.chunk_size * 1024 * 1024) {
|
||||||
Ok(None) => break,
|
ChunkState::Ok(upl) => file = upl,
|
||||||
Err(e) => error!("error: {e:?}"),
|
ChunkState::Err(upl, e) => {
|
||||||
_ => (),
|
error!("error: {e:?}");
|
||||||
|
file = upl;
|
||||||
|
}
|
||||||
|
ChunkState::Finished(path) => {
|
||||||
|
info!("Finished {:?}!", path.display());
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
state.save().unwrap();
|
debug!("file: {file:?}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
info!("uploads done");
|
|
||||||
|
|
||||||
state.clear().unwrap();
|
share.notify(&agent, &alias).unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ impl Alias {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn get_endpoint(&self, endpoint: impl Display + Debug) -> String {
|
pub(super) fn get_endpoint(&self, endpoint: impl Display + Debug) -> String {
|
||||||
let uri = format!("{}/{}", self.uri, endpoint);
|
let uri = format!("{}/{}", self.uri.to_string(), endpoint);
|
||||||
debug!("endpoint uri: {uri:?}");
|
debug!("endpoint uri: {uri:?}");
|
||||||
|
|
||||||
uri
|
uri
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,6 @@ mod checked;
|
||||||
mod uploading;
|
mod uploading;
|
||||||
|
|
||||||
pub use checked::FileChecked;
|
pub use checked::FileChecked;
|
||||||
pub use uploading::{ChunkState, FileUploading, UploadError};
|
pub use uploading::{ChunkState, FileUploading};
|
||||||
|
|
||||||
use super::{Alias, Share, alias::SharryAlias};
|
use super::{Alias, Share, alias::SharryAlias};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use std::{
|
use std::{
|
||||||
fmt::Display,
|
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{self, Read, Seek, SeekFrom},
|
io::{self, Read, Seek, SeekFrom},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
|
|
@ -36,16 +35,6 @@ pub enum ChunkState {
|
||||||
Finished(PathBuf),
|
Finished(PathBuf),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for FileUploading {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"Uploading ({:?}, {}, {})",
|
|
||||||
self.path, self.size, self.offset
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FileUploading {
|
impl FileUploading {
|
||||||
pub(super) fn new(path: PathBuf, size: usize, uri: String) -> Self {
|
pub(super) fn new(path: PathBuf, size: usize, uri: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue