From fa9056ea1dc8b0e42ee0fcfde739e9d45565e161 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 18 Nov 2024 11:32:15 +0000 Subject: [PATCH] fix(api): Fix #2434 and make dir_upload() methods more granular --- autonomi-cli/src/commands/file.rs | 2 +- autonomi/src/client/fs.rs | 31 +++++++++++++++++++++++++------ autonomi/tests/fs.rs | 4 ++-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/autonomi-cli/src/commands/file.rs b/autonomi-cli/src/commands/file.rs index 6d3f051015..94d31d77ac 100644 --- a/autonomi-cli/src/commands/file.rs +++ b/autonomi-cli/src/commands/file.rs @@ -53,7 +53,7 @@ pub async fn upload(file: &str, public: bool, peers: Vec) -> Result<( let local_addr; let archive = if public { let xor_name = client - .dir_upload(dir_path, &wallet) + .dir_and_archive_upload(dir_path, &wallet) .await .wrap_err("Failed to upload file")?; local_addr = addr_to_str(xor_name); diff --git a/autonomi/src/client/fs.rs b/autonomi/src/client/fs.rs index c1505224bc..fe7d1d37bc 100644 --- a/autonomi/src/client/fs.rs +++ b/autonomi/src/client/fs.rs @@ -109,11 +109,24 @@ impl Client { /// Upload a directory to the network. The directory is recursively walked. /// Reads all files, splits into chunks, uploads chunks, uploads datamaps, uploads archive, returns ArchiveAddr (pointing to the archive) - pub async fn dir_upload( + pub async fn dir_and_archive_upload( &self, dir_path: PathBuf, wallet: &EvmWallet, ) -> Result { + match self.dir_upload(dir_path.clone(), wallet).await { + Ok(archive) => Ok(self.archive_upload(&archive, wallet).await?), + Err(e) => Err(e), + } + } + + /// Upload a directory to the network. The directory is recursively walked. + /// Reads all files, splits into chunks, uploads chunks, uploads datamaps, uploads archive, returns Archive but does not upload that to the network. + pub async fn dir_upload( + &self, + dir_path: PathBuf, + wallet: &EvmWallet, + ) -> Result { info!("Uploading directory: {dir_path:?}"); let start = tokio::time::Instant::now(); @@ -152,14 +165,20 @@ impl Client { } } - // upload archive - let archive_serialized = archive.into_bytes()?; - let arch_addr = self.data_put(archive_serialized, wallet).await?; - info!("Complete archive upload completed in {:?}", start.elapsed()); #[cfg(feature = "loud")] println!("Upload completed in {:?}", start.elapsed()); - Ok(arch_addr) + Ok(archive) + } + + /// Upload a directory Archive to the network + pub async fn archive_upload( + &self, + archive: &Archive, + wallet: &EvmWallet, + ) -> Result { + let archive_serialized = archive.into_bytes()?; + Ok(self.data_put(archive_serialized, wallet).await?) } /// Upload a file to the network. diff --git a/autonomi/tests/fs.rs b/autonomi/tests/fs.rs index b952852bc2..b689066a53 100644 --- a/autonomi/tests/fs.rs +++ b/autonomi/tests/fs.rs @@ -30,7 +30,7 @@ async fn dir_upload_download() -> Result<()> { let wallet = get_funded_wallet(); let addr = client - .dir_upload("tests/file/test_dir".into(), &wallet) + .dir_and_archive_upload("tests/file/test_dir".into(), &wallet) .await?; sleep(Duration::from_secs(10)).await; @@ -86,7 +86,7 @@ async fn file_into_vault() -> Result<()> { let client_sk = bls::SecretKey::random(); let addr = client - .dir_upload("tests/file/test_dir".into(), &wallet) + .dir_and_archive_upload("tests/file/test_dir".into(), &wallet) .await?; sleep(Duration::from_secs(2)).await;