Compare commits
2 commits
09d22a0ad9
...
f05e112040
| Author | SHA1 | Date | |
|---|---|---|---|
| f05e112040 | |||
| 5556a658f5 |
5 changed files with 79 additions and 35 deletions
|
|
@ -8,11 +8,13 @@ use console::style;
|
||||||
use indicatif::{ProgressBar, ProgressStyle};
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
use log::{debug, warn};
|
use log::{debug, warn};
|
||||||
|
|
||||||
|
use crate::file::Chunk;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
cachefile::CacheFile,
|
cachefile::CacheFile,
|
||||||
cli::Cli,
|
cli::Cli,
|
||||||
file::{self, FileTrait},
|
file::{self, FileTrait},
|
||||||
sharry::{self, Client, ClientError},
|
sharry::{self, Client},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
|
|
@ -104,7 +106,7 @@ impl AppState {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finish_bar(&self) {
|
fn finish_progressbar(&self) {
|
||||||
let mut slot = self.progress.borrow_mut();
|
let mut slot = self.progress.borrow_mut();
|
||||||
if let Some(bar) = slot.as_ref() {
|
if let Some(bar) = slot.as_ref() {
|
||||||
bar.finish();
|
bar.finish();
|
||||||
|
|
@ -112,7 +114,7 @@ impl AppState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn upload_chunk(&mut self) -> sharry::Result<Option<()>> {
|
fn next_chunk(&mut self) -> io::Result<Option<Chunk>> {
|
||||||
let Some(mut uploading) = self.inner.pop_file(&self.http) else {
|
let Some(mut uploading) = self.inner.pop_file(&self.http) else {
|
||||||
self.inner
|
self.inner
|
||||||
.share_notify(&self.http)
|
.share_notify(&self.http)
|
||||||
|
|
@ -121,20 +123,23 @@ impl AppState {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
debug!("{uploading:?}");
|
||||||
|
|
||||||
self.get_or_create_progressbar(&uploading);
|
self.get_or_create_progressbar(&uploading);
|
||||||
|
|
||||||
debug!("{uploading} chunk {}", self.buffer.len());
|
let chunk = uploading.read(&mut self.buffer);
|
||||||
|
self.inner.push_file(uploading);
|
||||||
|
let chunk = chunk?;
|
||||||
|
|
||||||
let chunk = uploading
|
debug!("{chunk:?}");
|
||||||
.read(&mut self.buffer)
|
|
||||||
.map_err(ClientError::from)?;
|
|
||||||
|
|
||||||
self.http.file_patch(
|
Ok(Some(chunk))
|
||||||
chunk.get_patch_uri(),
|
}
|
||||||
self.inner.alias_id(),
|
|
||||||
chunk.get_offset(),
|
fn is_done(&mut self) -> bool {
|
||||||
chunk.get_data(),
|
let Some(uploading) = self.inner.pop_file(&self.http) else {
|
||||||
)?;
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
match uploading.check_eof() {
|
match uploading.check_eof() {
|
||||||
Ok(uploading) => {
|
Ok(uploading) => {
|
||||||
|
|
@ -146,15 +151,29 @@ impl AppState {
|
||||||
drop(bar);
|
drop(bar);
|
||||||
|
|
||||||
self.inner.push_file(uploading);
|
self.inner.push_file(uploading);
|
||||||
Ok(Some(()))
|
|
||||||
}
|
}
|
||||||
Err(path) => {
|
Err(path) => {
|
||||||
debug!("Finished {:?}!", path.display());
|
debug!("Finished {:?}!", path.display());
|
||||||
self.finish_bar();
|
self.finish_progressbar();
|
||||||
|
|
||||||
Ok(self.inner.has_file().then_some(()))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.inner.is_empty()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn upload_chunk(&mut self) -> sharry::Result<bool> {
|
||||||
|
let Some(chunk) = self.next_chunk()? else {
|
||||||
|
return Ok(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
self.http.file_patch(
|
||||||
|
chunk.get_patch_uri(),
|
||||||
|
self.inner.alias_id(),
|
||||||
|
chunk.get_offset(),
|
||||||
|
unsafe { chunk.get_data() },
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(self.is_done())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn file_names(&self) -> Vec<&str> {
|
pub fn file_names(&self) -> Vec<&str> {
|
||||||
|
|
|
||||||
|
|
@ -99,8 +99,8 @@ impl CacheFile {
|
||||||
self.files.iter().map(FileState::file_name).collect()
|
self.files.iter().map(FileState::file_name).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_file(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
!self.files.is_empty()
|
self.files.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pop_file(&mut self, http: &impl Client) -> Option<file::Uploading> {
|
pub fn pop_file(&mut self, http: &impl Client) -> Option<file::Uploading> {
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,25 @@
|
||||||
pub struct Chunk<'t> {
|
use std::fmt;
|
||||||
data: &'t [u8],
|
|
||||||
patch_uri: &'t str,
|
pub struct Chunk {
|
||||||
|
data: *const [u8],
|
||||||
|
patch_uri: String,
|
||||||
offset: u64,
|
offset: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'t> Chunk<'t> {
|
impl fmt::Debug for Chunk {
|
||||||
pub fn new(data: &'t [u8], patch_uri: &'t str, offset: u64) -> Self {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.debug_struct("Chunk")
|
||||||
|
.field("patch_uri", &self.patch_uri)
|
||||||
|
.field("offset", &self.offset)
|
||||||
|
.field("data_len", &self.data.len())
|
||||||
|
.finish_non_exhaustive()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Chunk {
|
||||||
|
pub fn new(data: &[u8], patch_uri: String, offset: u64) -> Self {
|
||||||
|
let data: *const [u8] = data as *const [u8];
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
data,
|
data,
|
||||||
patch_uri,
|
patch_uri,
|
||||||
|
|
@ -13,8 +27,8 @@ impl<'t> Chunk<'t> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_data(&self) -> &[u8] {
|
pub unsafe fn get_data(&self) -> &[u8] {
|
||||||
self.data
|
unsafe { &*self.data }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_length(&self) -> u64 {
|
pub fn get_length(&self) -> u64 {
|
||||||
|
|
@ -27,7 +41,7 @@ impl<'t> Chunk<'t> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_patch_uri(&self) -> &str {
|
pub fn get_patch_uri(&self) -> &str {
|
||||||
self.patch_uri
|
&self.patch_uri
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_offset(&self) -> u64 {
|
pub fn get_offset(&self) -> u64 {
|
||||||
|
|
|
||||||
|
|
@ -8,15 +8,16 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use super::{Chunk, FileTrait};
|
use super::{Chunk, FileTrait};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Uploading {
|
pub struct Uploading {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
size: u64,
|
size: u64,
|
||||||
patch_uri: String,
|
patch_uri: String,
|
||||||
|
last_offset: Option<u64>,
|
||||||
offset: u64,
|
offset: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Uploading {
|
impl fmt::Debug for Uploading {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
|
|
@ -34,6 +35,7 @@ impl Uploading {
|
||||||
path,
|
path,
|
||||||
size,
|
size,
|
||||||
patch_uri,
|
patch_uri,
|
||||||
|
last_offset: None,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -42,7 +44,15 @@ impl Uploading {
|
||||||
self.offset
|
self.offset
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read<'t>(&'t mut self, buf: &'t mut [u8]) -> io::Result<Chunk<'t>> {
|
pub fn rewind(self) -> Option<Self> {
|
||||||
|
self.last_offset.map(|last_offset| Self {
|
||||||
|
last_offset: None,
|
||||||
|
offset: last_offset,
|
||||||
|
..self
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read(&mut self, buf: &mut [u8]) -> io::Result<Chunk> {
|
||||||
let mut f = fs::File::open(&self.path)?;
|
let mut f = fs::File::open(&self.path)?;
|
||||||
|
|
||||||
f.seek(SeekFrom::Start(self.offset))?;
|
f.seek(SeekFrom::Start(self.offset))?;
|
||||||
|
|
@ -55,7 +65,8 @@ impl Uploading {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let chunk = Chunk::new(&buf[..read_len], &self.patch_uri, self.offset);
|
let chunk = Chunk::new(&buf[..read_len], self.patch_uri.clone(), self.offset);
|
||||||
|
self.last_offset = Some(self.offset);
|
||||||
self.offset += chunk.get_length();
|
self.offset += chunk.get_length();
|
||||||
|
|
||||||
Ok(chunk)
|
Ok(chunk)
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,7 @@ fn main() {
|
||||||
"{} Failed to save {} state: {e}",
|
"{} Failed to save {} state: {e}",
|
||||||
style("Warning:").red().bold(),
|
style("Warning:").red().bold(),
|
||||||
style("ShrUpl").yellow().bold(),
|
style("ShrUpl").yellow().bold(),
|
||||||
)
|
);
|
||||||
});
|
});
|
||||||
state
|
state
|
||||||
}
|
}
|
||||||
|
|
@ -116,14 +116,14 @@ fn main() {
|
||||||
loop {
|
loop {
|
||||||
match state.upload_chunk() {
|
match state.upload_chunk() {
|
||||||
Err(e) => error!("error: {e:?}"), // HACK handle errors better
|
Err(e) => error!("error: {e:?}"), // HACK handle errors better
|
||||||
Ok(None) => {
|
Ok(true) => {
|
||||||
info!("all uploads done");
|
info!("all uploads done");
|
||||||
state.clear().unwrap_or_else(|e| {
|
state.clear().unwrap_or_else(|e| {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"{} Failed to remove {} state: {e}",
|
"{} Failed to remove {} state: {e}",
|
||||||
style("Warning:").red().bold(),
|
style("Warning:").red().bold(),
|
||||||
style("ShrUpl").yellow().bold(),
|
style("ShrUpl").yellow().bold(),
|
||||||
)
|
);
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -135,7 +135,7 @@ fn main() {
|
||||||
"{} Failed to save {} state: {e}",
|
"{} Failed to save {} state: {e}",
|
||||||
style("Warning:").red().bold(),
|
style("Warning:").red().bold(),
|
||||||
style("ShrUpl").yellow().bold(),
|
style("ShrUpl").yellow().bold(),
|
||||||
)
|
);
|
||||||
});
|
});
|
||||||
check_ctrlc();
|
check_ctrlc();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue