From 725b96792f27a3a0e9e79085978a375285d668de Mon Sep 17 00:00:00 2001 From: Benno Zeeman Date: Fri, 6 Dec 2024 14:24:35 +0100 Subject: [PATCH] feat(autonomi): add convenience method/rename --- ant-cli/src/commands/file.rs | 10 +++----- autonomi/README.md | 2 +- autonomi/examples/put_and_dir_upload.rs | 2 +- autonomi/src/client/files/fs.rs | 13 ++++++++++ autonomi/src/client/files/fs_public.rs | 32 +++++++++++++++---------- autonomi/tests/fs.rs | 4 ++-- 6 files changed, 40 insertions(+), 23 deletions(-) diff --git a/ant-cli/src/commands/file.rs b/ant-cli/src/commands/file.rs index b6b2e30623..146133e348 100644 --- a/ant-cli/src/commands/file.rs +++ b/ant-cli/src/commands/file.rs @@ -53,20 +53,16 @@ pub async fn upload(file: &str, public: bool, peers: Vec) -> Result<( let local_addr; let archive = if public { let xor_name = client - .dir_upload_public(dir_path, &wallet) + .dir_and_archive_upload_public(dir_path, &wallet) .await .wrap_err("Failed to upload file")?; local_addr = addr_to_str(xor_name); local_addr.clone() } else { - let private_archive = client - .dir_upload(dir_path, &wallet) - .await - .wrap_err("Failed to upload file")?; let private_data_access = client - .archive_put(private_archive, (&wallet).into()) + .dir_and_archive_upload(dir_path, &wallet) .await - .wrap_err("Failed to upload private archive")?; + .wrap_err("Failed to upload dir and archive")?; local_addr = private_data_access.address(); private_data_access.to_hex() diff --git a/autonomi/README.md b/autonomi/README.md index 3dbaf5f672..63235554a1 100644 --- a/autonomi/README.md +++ b/autonomi/README.md @@ -33,7 +33,7 @@ async fn main() -> Result<(), Box> { let _data_fetched = client.data_get_public(data_addr).await?; // Put and fetch directory from local file system. - let dir_addr = client.dir_upload_public("files/to/upload".into(), &wallet).await?; + let dir_addr = client.dir_and_archive_upload_public("files/to/upload".into(), &wallet).await?; client .dir_download_public(dir_addr, "files/downloaded".into()) .await?; diff --git a/autonomi/examples/put_and_dir_upload.rs b/autonomi/examples/put_and_dir_upload.rs index 45ebc96627..874ca57980 100644 --- a/autonomi/examples/put_and_dir_upload.rs +++ b/autonomi/examples/put_and_dir_upload.rs @@ -16,7 +16,7 @@ async fn main() -> Result<(), Box> { // Put and fetch directory from local file system. let dir_addr = client - .dir_upload_public("files/to/upload".into(), &wallet) + .dir_and_archive_upload_public("files/to/upload".into(), &wallet) .await?; client .dir_download_public(dir_addr, "files/downloaded".into()) diff --git a/autonomi/src/client/files/fs.rs b/autonomi/src/client/files/fs.rs index e278a1b38f..37df1aa84f 100644 --- a/autonomi/src/client/files/fs.rs +++ b/autonomi/src/client/files/fs.rs @@ -162,6 +162,19 @@ impl Client { Ok(archive) } + /// Same as [`Client::dir_upload`] but also uploads the archive (privately) to the network. + /// + /// Returns the [`PrivateArchiveAccess`] allowing the private archive to be downloaded from the network. + pub async fn dir_and_archive_upload( + &self, + dir_path: PathBuf, + wallet: &EvmWallet, + ) -> Result { + let archive = self.dir_upload(dir_path, wallet).await?; + let archive_addr = self.archive_put(archive, wallet.into()).await?; + Ok(archive_addr) + } + /// Upload a private file to the network. /// Reads file, splits into chunks, uploads chunks, uploads datamap, returns [`DataMapChunk`] (pointing to the datamap) async fn file_upload( diff --git a/autonomi/src/client/files/fs_public.rs b/autonomi/src/client/files/fs_public.rs index d140f873c0..fd9cad51ba 100644 --- a/autonomi/src/client/files/fs_public.rs +++ b/autonomi/src/client/files/fs_public.rs @@ -54,13 +54,16 @@ impl Client { Ok(()) } - /// 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) + /// Upload a directory to the network. The directory is recursively walked and each file is uploaded to the network. + /// + /// The data maps of these files are uploaded on the network, making the individual files publicly available. + /// + /// This returns, but does not upload (!),the [`PublicArchive`] containing the data maps of the uploaded files. pub async fn dir_upload_public( &self, dir_path: PathBuf, wallet: &EvmWallet, - ) -> Result { + ) -> Result { info!("Uploading directory: {dir_path:?}"); let start = tokio::time::Instant::now(); @@ -99,17 +102,22 @@ impl Client { } } - // upload archive - let archive_serialized = archive.into_bytes()?; - let arch_addr = self - .data_put_public(archive_serialized, wallet.into()) - .await?; - - info!("Complete archive upload completed in {:?}", start.elapsed()); #[cfg(feature = "loud")] println!("Upload completed in {:?}", start.elapsed()); - debug!("Directory uploaded to the network at {arch_addr:?}"); - Ok(arch_addr) + Ok(archive) + } + + /// Same as [`Client::dir_upload_public`] but also uploads the archive to the network. + /// + /// Returns the [`ArchiveAddr`] of the uploaded archive. + pub async fn dir_and_archive_upload_public( + &self, + dir_path: PathBuf, + wallet: &EvmWallet, + ) -> Result { + let archive = self.dir_upload_public(dir_path, wallet).await?; + let archive_addr = self.archive_put_public(archive, wallet).await?; + Ok(archive_addr) } /// Upload a file to the network. diff --git a/autonomi/tests/fs.rs b/autonomi/tests/fs.rs index e9a8f77729..1b8b59f801 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_public("tests/file/test_dir".into(), &wallet) + .dir_and_archive_upload_public("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_public("tests/file/test_dir".into(), &wallet) + .dir_and_archive_upload_public("tests/file/test_dir".into(), &wallet) .await?; sleep(Duration::from_secs(2)).await;