diff --git a/.vscode/settings.json b/.vscode/settings.json index 936ca88..4eb6d7e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,6 +12,37 @@ }, }, "rust-analyzer.imports.prefix": "plain", + "todo-tree.general.tags": [ + "BUG", + "HACK", + "FIXME", + "TODO", + "XXX", + "[ ]", + "[x]", + "BOOKMARK" + ], + "todo-tree.highlights.backgroundColourScheme": [ + "red", + "orange", + "yellow", + "green", + "blue", + "indigo", + "violet", + "yellow" + ], + "todo-tree.highlights.foregroundColourScheme": [ + "white", + "black", + "black", + "white", + "white", + "white", + "black", + "black" + ], + "todo-tree.highlights.useColourScheme": true, // // override the default setting (`cargo check --all-targets`) which produces the following error // // "can't find crate for `test`" when the default compilation target is a no_std target // "rust-analyzer.checkOnSave.allTargets": false, diff --git a/src/appstate.rs b/src/appstate.rs index a481007..f3bcbcf 100644 --- a/src/appstate.rs +++ b/src/appstate.rs @@ -37,8 +37,8 @@ enum FileState { impl FileState { fn file_name(&self) -> &str { match self { - FileState::C(checked) => checked.name(), - FileState::U(uploading) => uploading.name(), + FileState::C(checked) => checked.get_name(), + FileState::U(uploading) => uploading.get_name(), } } @@ -133,7 +133,7 @@ impl AppState { // Initialize or fetch the existing ProgressBar in one call: let bar = &*self.progress.get_or_insert_with(|| { // Create a new bar with style - let bar = ProgressBar::new(uploading.size()) + let bar = ProgressBar::new(uploading.get_size()) .with_style( ProgressStyle::with_template(&format!( concat!( @@ -145,8 +145,8 @@ impl AppState { )) .unwrap(), ) - .with_message(uploading.name().to_owned()) - .with_position(uploading.offset()); + .with_message(uploading.get_name().to_owned()) + .with_position(uploading.get_offset()); bar.enable_steady_tick(Duration::from_millis(100)); bar @@ -154,7 +154,7 @@ impl AppState { match uploading.upload_chunk(http, &self.alias, chunk_size) { ChunkState::Ok(upl) => { - bar.set_position(upl.offset()); + bar.set_position(upl.get_offset()); self.files.push_front(FileState::U(upl)); Ok(Some(())) } diff --git a/src/sharry/file/checked.rs b/src/sharry/file/checked.rs index 2d80026..05a9180 100644 --- a/src/sharry/file/checked.rs +++ b/src/sharry/file/checked.rs @@ -36,14 +36,14 @@ impl FileChecked { alias: &Alias, share: &Share, ) -> io::Result { - let size: usize = self.size(); + let size = self.get_size(); let res = { let endpoint = alias.get_endpoint(format!("alias/upload/{}/files/tus", share.id)); (http.post(endpoint)) .sharry_header(alias) - .header("Sharry-File-Name", self.name()) + .header("Sharry-File-Name", self.get_name()) .header("Upload-Length", size) .send_empty() .map_err(ureq::Error::into_io)? @@ -69,16 +69,11 @@ impl<'t> SharryFile<'t> for FileChecked { /// get a reference to the file's name /// /// Uses `SharryFile::extract_file_name`, which may **panic**! - fn name(&'t self) -> &'t str { + fn get_name(&'t self) -> &'t str { ::extract_file_name(&self.path) } - fn size(&self) -> T - where - T: TryFrom, - >::Error: std::fmt::Debug + std::fmt::Display, - { - let val = ::to_usize(fs::metadata(&self.path).unwrap().len()); - ::from_usize(val) + fn get_size(&self) -> u64 { + fs::metadata(&self.path).unwrap().len() } } diff --git a/src/sharry/file/filetrait.rs b/src/sharry/file/filetrait.rs index b466d58..e9a3be0 100644 --- a/src/sharry/file/filetrait.rs +++ b/src/sharry/file/filetrait.rs @@ -16,38 +16,7 @@ pub trait SharryFile<'t> { .expect("bad file name") } - fn from_usize(val: usize) -> T - where - T: TryFrom, - >::Error: Debug + Display, - { - val.try_into() - .inspect_err(|e| panic!("usize did not fit into other type: {e}")) - .unwrap() - } + fn get_name(&'t self) -> &'t str; - fn to_usize(val: T) -> usize - where - T: TryInto, - >::Error: Debug + Display, - { - val.try_into() - .inspect_err(|e| panic!("other type did not fit into usize: {e}")) - .unwrap() - } - - fn name(&'t self) -> &'t str; - - fn offset(&self) -> T - where - T: TryFrom, - >::Error: Debug + Display, - { - Self::from_usize(0) - } - - fn size(&self) -> T - where - T: TryFrom, - >::Error: Debug + Display; + fn get_size(&self) -> u64; } diff --git a/src/sharry/file/uploading.rs b/src/sharry/file/uploading.rs index d3898e2..00ba2d6 100644 --- a/src/sharry/file/uploading.rs +++ b/src/sharry/file/uploading.rs @@ -14,9 +14,9 @@ use super::{Alias, SharryAlias, SharryFile}; #[derive(Serialize, Deserialize, Debug)] pub struct FileUploading { path: PathBuf, - size: usize, + size: u64, uri: String, - offset: usize, + offset: u64, } #[derive(Debug)] @@ -46,7 +46,7 @@ impl std::fmt::Display for FileUploading { } impl FileUploading { - pub(super) fn new(path: PathBuf, size: usize, uri: String) -> Self { + pub(super) fn new(path: PathBuf, size: u64, uri: String) -> Self { Self { path, size, @@ -55,11 +55,13 @@ impl FileUploading { } } - fn read_chunk(&self, chunk_size: usize) -> io::Result> { - let offset = u64::try_from(self.offset).map_err(io::Error::other)?; + pub fn get_offset(&self) -> u64 { + self.offset + } + fn read_chunk(&self, chunk_size: usize) -> io::Result> { let mut f = fs::File::open(&self.path)?; - f.seek(SeekFrom::Start(offset))?; + f.seek(SeekFrom::Start(self.offset))?; let mut bytes = vec![0; chunk_size]; let read_len = f.read(&mut bytes)?; @@ -93,12 +95,20 @@ impl FileUploading { let Some(Ok(Ok(res_offset))) = (res.headers().get("Upload-Offset")) .map(HeaderValue::to_str) - .map(|v| v.map(str::parse::)) + .map(|v| v.map(str::parse::)) else { return ChunkState::Err(self, UploadError::ResponseOffset); }; - if self.offset + chunk.len() != res_offset { + // convert chunk.len() into an `u64` + // + // BOOKMARK this might panic on platforms where `usize` > 64 bit. + // Also whoa, you've sent more than 2 EiB ... in ONE chunk. + // Maybe just chill? + let chunk_len = u64::try_from(chunk.len()) + .unwrap_or_else(|e| panic!("usize={} did not fit into u64: {}", chunk.len(), e)); + + if self.offset + chunk_len != res_offset { return ChunkState::Err(self, UploadError::ResponseOffset); } @@ -113,23 +123,11 @@ impl FileUploading { } impl<'t> SharryFile<'t> for FileUploading { - fn name(&'t self) -> &'t str { + fn get_name(&'t self) -> &'t str { ::extract_file_name(&self.path) } - fn offset(&self) -> T - where - T: TryFrom, - >::Error: std::fmt::Debug + std::fmt::Display, - { - ::from_usize(self.offset) - } - - fn size(&self) -> T - where - T: TryFrom, - >::Error: std::fmt::Debug + std::fmt::Display, - { - ::from_usize(self.size) + fn get_size(&self) -> u64 { + self.size } }