Skip to content

Commit

Permalink
feat(autonomi): add convenience method/rename
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed Dec 6, 2024
1 parent e0b4f17 commit 725b967
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
10 changes: 3 additions & 7 deletions ant-cli/src/commands/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,16 @@ pub async fn upload(file: &str, public: bool, peers: Vec<Multiaddr>) -> 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()
Expand Down
2 changes: 1 addition & 1 deletion autonomi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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?;
Expand Down
2 changes: 1 addition & 1 deletion autonomi/examples/put_and_dir_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// 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())
Expand Down
13 changes: 13 additions & 0 deletions autonomi/src/client/files/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PrivateArchiveAccess, UploadError> {
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(
Expand Down
32 changes: 20 additions & 12 deletions autonomi/src/client/files/fs_public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArchiveAddr, UploadError> {
) -> Result<PublicArchive, UploadError> {
info!("Uploading directory: {dir_path:?}");
let start = tokio::time::Instant::now();

Expand Down Expand Up @@ -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<ArchiveAddr, UploadError> {
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.
Expand Down
4 changes: 2 additions & 2 deletions autonomi/tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 725b967

Please sign in to comment.